Statistics
| Branch: | Revision:

root / hw / ne2000.c @ 8ca209ad

History | View | Annotate | Download (22.7 kB)

1 80cabfad bellard
/*
2 80cabfad bellard
 * QEMU NE2000 emulation
3 5fafdf24 ths
 *
4 80cabfad bellard
 * Copyright (c) 2003-2004 Fabrice Bellard
5 5fafdf24 ths
 *
6 80cabfad bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 80cabfad bellard
 * of this software and associated documentation files (the "Software"), to deal
8 80cabfad bellard
 * in the Software without restriction, including without limitation the rights
9 80cabfad bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 80cabfad bellard
 * copies of the Software, and to permit persons to whom the Software is
11 80cabfad bellard
 * furnished to do so, subject to the following conditions:
12 80cabfad bellard
 *
13 80cabfad bellard
 * The above copyright notice and this permission notice shall be included in
14 80cabfad bellard
 * all copies or substantial portions of the Software.
15 80cabfad bellard
 *
16 80cabfad bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 80cabfad bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 80cabfad bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 80cabfad bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 80cabfad bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 80cabfad bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 80cabfad bellard
 * THE SOFTWARE.
23 80cabfad bellard
 */
24 87ecb68b pbrook
#include "hw.h"
25 87ecb68b pbrook
#include "pci.h"
26 87ecb68b pbrook
#include "net.h"
27 9453c5bc Gerd Hoffmann
#include "ne2000.h"
28 a783cc3e Gerd Hoffmann
#include "loader.h"
29 80cabfad bellard
30 80cabfad bellard
/* debug NE2000 card */
31 80cabfad bellard
//#define DEBUG_NE2000
32 80cabfad bellard
33 b41a2cd1 bellard
#define MAX_ETH_FRAME_SIZE 1514
34 80cabfad bellard
35 80cabfad bellard
#define E8390_CMD        0x00  /* The command register (for all pages) */
36 80cabfad bellard
/* Page 0 register offsets. */
37 80cabfad bellard
#define EN0_CLDALO        0x01        /* Low byte of current local dma addr  RD */
38 80cabfad bellard
#define EN0_STARTPG        0x01        /* Starting page of ring bfr WR */
39 80cabfad bellard
#define EN0_CLDAHI        0x02        /* High byte of current local dma addr  RD */
40 80cabfad bellard
#define EN0_STOPPG        0x02        /* Ending page +1 of ring bfr WR */
41 80cabfad bellard
#define EN0_BOUNDARY        0x03        /* Boundary page of ring bfr RD WR */
42 80cabfad bellard
#define EN0_TSR                0x04        /* Transmit status reg RD */
43 80cabfad bellard
#define EN0_TPSR        0x04        /* Transmit starting page WR */
44 80cabfad bellard
#define EN0_NCR                0x05        /* Number of collision reg RD */
45 80cabfad bellard
#define EN0_TCNTLO        0x05        /* Low  byte of tx byte count WR */
46 80cabfad bellard
#define EN0_FIFO        0x06        /* FIFO RD */
47 80cabfad bellard
#define EN0_TCNTHI        0x06        /* High byte of tx byte count WR */
48 80cabfad bellard
#define EN0_ISR                0x07        /* Interrupt status reg RD WR */
49 80cabfad bellard
#define EN0_CRDALO        0x08        /* low byte of current remote dma address RD */
50 80cabfad bellard
#define EN0_RSARLO        0x08        /* Remote start address reg 0 */
51 80cabfad bellard
#define EN0_CRDAHI        0x09        /* high byte, current remote dma address RD */
52 80cabfad bellard
#define EN0_RSARHI        0x09        /* Remote start address reg 1 */
53 80cabfad bellard
#define EN0_RCNTLO        0x0a        /* Remote byte count reg WR */
54 089af991 bellard
#define EN0_RTL8029ID0        0x0a        /* Realtek ID byte #1 RD */
55 80cabfad bellard
#define EN0_RCNTHI        0x0b        /* Remote byte count reg WR */
56 089af991 bellard
#define EN0_RTL8029ID1        0x0b        /* Realtek ID byte #2 RD */
57 80cabfad bellard
#define EN0_RSR                0x0c        /* rx status reg RD */
58 80cabfad bellard
#define EN0_RXCR        0x0c        /* RX configuration reg WR */
59 80cabfad bellard
#define EN0_TXCR        0x0d        /* TX configuration reg WR */
60 80cabfad bellard
#define EN0_COUNTER0        0x0d        /* Rcv alignment error counter RD */
61 80cabfad bellard
#define EN0_DCFG        0x0e        /* Data configuration reg WR */
62 80cabfad bellard
#define EN0_COUNTER1        0x0e        /* Rcv CRC error counter RD */
63 80cabfad bellard
#define EN0_IMR                0x0f        /* Interrupt mask reg WR */
64 80cabfad bellard
#define EN0_COUNTER2        0x0f        /* Rcv missed frame error counter RD */
65 80cabfad bellard
66 80cabfad bellard
#define EN1_PHYS        0x11
67 80cabfad bellard
#define EN1_CURPAG      0x17
68 80cabfad bellard
#define EN1_MULT        0x18
69 80cabfad bellard
70 a343df16 bellard
#define EN2_STARTPG        0x21        /* Starting page of ring bfr RD */
71 a343df16 bellard
#define EN2_STOPPG        0x22        /* Ending page +1 of ring bfr RD */
72 a343df16 bellard
73 089af991 bellard
#define EN3_CONFIG0        0x33
74 089af991 bellard
#define EN3_CONFIG1        0x34
75 089af991 bellard
#define EN3_CONFIG2        0x35
76 089af991 bellard
#define EN3_CONFIG3        0x36
77 089af991 bellard
78 80cabfad bellard
/*  Register accessed at EN_CMD, the 8390 base addr.  */
79 80cabfad bellard
#define E8390_STOP        0x01        /* Stop and reset the chip */
80 80cabfad bellard
#define E8390_START        0x02        /* Start the chip, clear reset */
81 80cabfad bellard
#define E8390_TRANS        0x04        /* Transmit a frame */
82 80cabfad bellard
#define E8390_RREAD        0x08        /* Remote read */
83 80cabfad bellard
#define E8390_RWRITE        0x10        /* Remote write  */
84 80cabfad bellard
#define E8390_NODMA        0x20        /* Remote DMA */
85 80cabfad bellard
#define E8390_PAGE0        0x00        /* Select page chip registers */
86 80cabfad bellard
#define E8390_PAGE1        0x40        /* using the two high-order bits */
87 80cabfad bellard
#define E8390_PAGE2        0x80        /* Page 3 is invalid. */
88 80cabfad bellard
89 80cabfad bellard
/* Bits in EN0_ISR - Interrupt status register */
90 80cabfad bellard
#define ENISR_RX        0x01        /* Receiver, no error */
91 80cabfad bellard
#define ENISR_TX        0x02        /* Transmitter, no error */
92 80cabfad bellard
#define ENISR_RX_ERR        0x04        /* Receiver, with error */
93 80cabfad bellard
#define ENISR_TX_ERR        0x08        /* Transmitter, with error */
94 80cabfad bellard
#define ENISR_OVER        0x10        /* Receiver overwrote the ring */
95 80cabfad bellard
#define ENISR_COUNTERS        0x20        /* Counters need emptying */
96 80cabfad bellard
#define ENISR_RDC        0x40        /* remote dma complete */
97 80cabfad bellard
#define ENISR_RESET        0x80        /* Reset completed */
98 80cabfad bellard
#define ENISR_ALL        0x3f        /* Interrupts we will enable */
99 80cabfad bellard
100 80cabfad bellard
/* Bits in received packet status byte and EN0_RSR*/
101 80cabfad bellard
#define ENRSR_RXOK        0x01        /* Received a good packet */
102 80cabfad bellard
#define ENRSR_CRC        0x02        /* CRC error */
103 80cabfad bellard
#define ENRSR_FAE        0x04        /* frame alignment error */
104 80cabfad bellard
#define ENRSR_FO        0x08        /* FIFO overrun */
105 80cabfad bellard
#define ENRSR_MPA        0x10        /* missed pkt */
106 80cabfad bellard
#define ENRSR_PHY        0x20        /* physical/multicast address */
107 80cabfad bellard
#define ENRSR_DIS        0x40        /* receiver disable. set in monitor mode */
108 80cabfad bellard
#define ENRSR_DEF        0x80        /* deferring */
109 80cabfad bellard
110 80cabfad bellard
/* Transmitted packet status, EN0_TSR. */
111 80cabfad bellard
#define ENTSR_PTX 0x01        /* Packet transmitted without error */
112 80cabfad bellard
#define ENTSR_ND  0x02        /* The transmit wasn't deferred. */
113 80cabfad bellard
#define ENTSR_COL 0x04        /* The transmit collided at least once. */
114 80cabfad bellard
#define ENTSR_ABT 0x08  /* The transmit collided 16 times, and was deferred. */
115 80cabfad bellard
#define ENTSR_CRS 0x10        /* The carrier sense was lost. */
116 80cabfad bellard
#define ENTSR_FU  0x20  /* A "FIFO underrun" occurred during transmit. */
117 80cabfad bellard
#define ENTSR_CDH 0x40        /* The collision detect "heartbeat" signal was lost. */
118 80cabfad bellard
#define ENTSR_OWC 0x80  /* There was an out-of-window collision. */
119 80cabfad bellard
120 2b7a050a Juan Quintela
typedef struct PCINE2000State {
121 2b7a050a Juan Quintela
    PCIDevice dev;
122 2b7a050a Juan Quintela
    NE2000State ne2000;
123 2b7a050a Juan Quintela
} PCINE2000State;
124 2b7a050a Juan Quintela
125 9453c5bc Gerd Hoffmann
void ne2000_reset(NE2000State *s)
126 80cabfad bellard
{
127 80cabfad bellard
    int i;
128 80cabfad bellard
129 80cabfad bellard
    s->isr = ENISR_RESET;
130 93db6685 Gerd Hoffmann
    memcpy(s->mem, &s->c.macaddr, 6);
131 80cabfad bellard
    s->mem[14] = 0x57;
132 80cabfad bellard
    s->mem[15] = 0x57;
133 80cabfad bellard
134 80cabfad bellard
    /* duplicate prom data */
135 80cabfad bellard
    for(i = 15;i >= 0; i--) {
136 80cabfad bellard
        s->mem[2 * i] = s->mem[i];
137 80cabfad bellard
        s->mem[2 * i + 1] = s->mem[i];
138 80cabfad bellard
    }
139 80cabfad bellard
}
140 80cabfad bellard
141 80cabfad bellard
static void ne2000_update_irq(NE2000State *s)
142 80cabfad bellard
{
143 80cabfad bellard
    int isr;
144 a343df16 bellard
    isr = (s->isr & s->imr) & 0x7f;
145 a541f297 bellard
#if defined(DEBUG_NE2000)
146 d537cf6c pbrook
    printf("NE2000: Set IRQ to %d (%02x %02x)\n",
147 d537cf6c pbrook
           isr ? 1 : 0, s->isr, s->imr);
148 a541f297 bellard
#endif
149 d537cf6c pbrook
    qemu_set_irq(s->irq, (isr != 0));
150 80cabfad bellard
}
151 80cabfad bellard
152 7c9d8e07 bellard
#define POLYNOMIAL 0x04c11db6
153 7c9d8e07 bellard
154 7c9d8e07 bellard
/* From FreeBSD */
155 7c9d8e07 bellard
/* XXX: optimize */
156 7c9d8e07 bellard
static int compute_mcast_idx(const uint8_t *ep)
157 7c9d8e07 bellard
{
158 7c9d8e07 bellard
    uint32_t crc;
159 7c9d8e07 bellard
    int carry, i, j;
160 7c9d8e07 bellard
    uint8_t b;
161 7c9d8e07 bellard
162 7c9d8e07 bellard
    crc = 0xffffffff;
163 7c9d8e07 bellard
    for (i = 0; i < 6; i++) {
164 7c9d8e07 bellard
        b = *ep++;
165 7c9d8e07 bellard
        for (j = 0; j < 8; j++) {
166 7c9d8e07 bellard
            carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
167 7c9d8e07 bellard
            crc <<= 1;
168 7c9d8e07 bellard
            b >>= 1;
169 7c9d8e07 bellard
            if (carry)
170 7c9d8e07 bellard
                crc = ((crc ^ POLYNOMIAL) | carry);
171 7c9d8e07 bellard
        }
172 7c9d8e07 bellard
    }
173 7c9d8e07 bellard
    return (crc >> 26);
174 7c9d8e07 bellard
}
175 7c9d8e07 bellard
176 d861b05e pbrook
static int ne2000_buffer_full(NE2000State *s)
177 80cabfad bellard
{
178 80cabfad bellard
    int avail, index, boundary;
179 d861b05e pbrook
180 80cabfad bellard
    index = s->curpag << 8;
181 80cabfad bellard
    boundary = s->boundary << 8;
182 28c1c656 ths
    if (index < boundary)
183 80cabfad bellard
        avail = boundary - index;
184 80cabfad bellard
    else
185 80cabfad bellard
        avail = (s->stop - s->start) - (index - boundary);
186 80cabfad bellard
    if (avail < (MAX_ETH_FRAME_SIZE + 4))
187 d861b05e pbrook
        return 1;
188 d861b05e pbrook
    return 0;
189 d861b05e pbrook
}
190 d861b05e pbrook
191 1c2045b5 Mark McLoughlin
int ne2000_can_receive(VLANClientState *nc)
192 d861b05e pbrook
{
193 1c2045b5 Mark McLoughlin
    NE2000State *s = DO_UPCAST(NICState, nc, nc)->opaque;
194 3b46e624 ths
195 d861b05e pbrook
    if (s->cmd & E8390_STOP)
196 e89f00e6 aurel32
        return 1;
197 d861b05e pbrook
    return !ne2000_buffer_full(s);
198 80cabfad bellard
}
199 80cabfad bellard
200 b41a2cd1 bellard
#define MIN_BUF_SIZE 60
201 b41a2cd1 bellard
202 1c2045b5 Mark McLoughlin
ssize_t ne2000_receive(VLANClientState *nc, const uint8_t *buf, size_t size_)
203 80cabfad bellard
{
204 1c2045b5 Mark McLoughlin
    NE2000State *s = DO_UPCAST(NICState, nc, nc)->opaque;
205 4f1c942b Mark McLoughlin
    int size = size_;
206 80cabfad bellard
    uint8_t *p;
207 0ae045ae ths
    unsigned int total_len, next, avail, len, index, mcast_idx;
208 b41a2cd1 bellard
    uint8_t buf1[60];
209 5fafdf24 ths
    static const uint8_t broadcast_macaddr[6] =
210 7c9d8e07 bellard
        { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
211 3b46e624 ths
212 80cabfad bellard
#if defined(DEBUG_NE2000)
213 80cabfad bellard
    printf("NE2000: received len=%d\n", size);
214 80cabfad bellard
#endif
215 80cabfad bellard
216 d861b05e pbrook
    if (s->cmd & E8390_STOP || ne2000_buffer_full(s))
217 4f1c942b Mark McLoughlin
        return -1;
218 3b46e624 ths
219 7c9d8e07 bellard
    /* XXX: check this */
220 7c9d8e07 bellard
    if (s->rxcr & 0x10) {
221 7c9d8e07 bellard
        /* promiscuous: receive all */
222 7c9d8e07 bellard
    } else {
223 7c9d8e07 bellard
        if (!memcmp(buf,  broadcast_macaddr, 6)) {
224 7c9d8e07 bellard
            /* broadcast address */
225 7c9d8e07 bellard
            if (!(s->rxcr & 0x04))
226 4f1c942b Mark McLoughlin
                return size;
227 7c9d8e07 bellard
        } else if (buf[0] & 0x01) {
228 7c9d8e07 bellard
            /* multicast */
229 7c9d8e07 bellard
            if (!(s->rxcr & 0x08))
230 4f1c942b Mark McLoughlin
                return size;
231 7c9d8e07 bellard
            mcast_idx = compute_mcast_idx(buf);
232 7c9d8e07 bellard
            if (!(s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))))
233 4f1c942b Mark McLoughlin
                return size;
234 7c9d8e07 bellard
        } else if (s->mem[0] == buf[0] &&
235 3b46e624 ths
                   s->mem[2] == buf[1] &&
236 3b46e624 ths
                   s->mem[4] == buf[2] &&
237 3b46e624 ths
                   s->mem[6] == buf[3] &&
238 3b46e624 ths
                   s->mem[8] == buf[4] &&
239 7c9d8e07 bellard
                   s->mem[10] == buf[5]) {
240 7c9d8e07 bellard
            /* match */
241 7c9d8e07 bellard
        } else {
242 4f1c942b Mark McLoughlin
            return size;
243 7c9d8e07 bellard
        }
244 7c9d8e07 bellard
    }
245 7c9d8e07 bellard
246 7c9d8e07 bellard
247 b41a2cd1 bellard
    /* if too small buffer, then expand it */
248 b41a2cd1 bellard
    if (size < MIN_BUF_SIZE) {
249 b41a2cd1 bellard
        memcpy(buf1, buf, size);
250 b41a2cd1 bellard
        memset(buf1 + size, 0, MIN_BUF_SIZE - size);
251 b41a2cd1 bellard
        buf = buf1;
252 b41a2cd1 bellard
        size = MIN_BUF_SIZE;
253 b41a2cd1 bellard
    }
254 b41a2cd1 bellard
255 80cabfad bellard
    index = s->curpag << 8;
256 80cabfad bellard
    /* 4 bytes for header */
257 80cabfad bellard
    total_len = size + 4;
258 80cabfad bellard
    /* address for next packet (4 bytes for CRC) */
259 80cabfad bellard
    next = index + ((total_len + 4 + 255) & ~0xff);
260 80cabfad bellard
    if (next >= s->stop)
261 80cabfad bellard
        next -= (s->stop - s->start);
262 80cabfad bellard
    /* prepare packet header */
263 80cabfad bellard
    p = s->mem + index;
264 8d6c7eb8 bellard
    s->rsr = ENRSR_RXOK; /* receive status */
265 8d6c7eb8 bellard
    /* XXX: check this */
266 8d6c7eb8 bellard
    if (buf[0] & 0x01)
267 8d6c7eb8 bellard
        s->rsr |= ENRSR_PHY;
268 8d6c7eb8 bellard
    p[0] = s->rsr;
269 80cabfad bellard
    p[1] = next >> 8;
270 80cabfad bellard
    p[2] = total_len;
271 80cabfad bellard
    p[3] = total_len >> 8;
272 80cabfad bellard
    index += 4;
273 80cabfad bellard
274 80cabfad bellard
    /* write packet data */
275 80cabfad bellard
    while (size > 0) {
276 0ae045ae ths
        if (index <= s->stop)
277 0ae045ae ths
            avail = s->stop - index;
278 0ae045ae ths
        else
279 0ae045ae ths
            avail = 0;
280 80cabfad bellard
        len = size;
281 80cabfad bellard
        if (len > avail)
282 80cabfad bellard
            len = avail;
283 80cabfad bellard
        memcpy(s->mem + index, buf, len);
284 80cabfad bellard
        buf += len;
285 80cabfad bellard
        index += len;
286 80cabfad bellard
        if (index == s->stop)
287 80cabfad bellard
            index = s->start;
288 80cabfad bellard
        size -= len;
289 80cabfad bellard
    }
290 80cabfad bellard
    s->curpag = next >> 8;
291 8d6c7eb8 bellard
292 9f083493 ths
    /* now we can signal we have received something */
293 80cabfad bellard
    s->isr |= ENISR_RX;
294 80cabfad bellard
    ne2000_update_irq(s);
295 4f1c942b Mark McLoughlin
296 4f1c942b Mark McLoughlin
    return size_;
297 80cabfad bellard
}
298 80cabfad bellard
299 9453c5bc Gerd Hoffmann
void ne2000_ioport_write(void *opaque, uint32_t addr, uint32_t val)
300 80cabfad bellard
{
301 b41a2cd1 bellard
    NE2000State *s = opaque;
302 40545f84 bellard
    int offset, page, index;
303 80cabfad bellard
304 80cabfad bellard
    addr &= 0xf;
305 80cabfad bellard
#ifdef DEBUG_NE2000
306 80cabfad bellard
    printf("NE2000: write addr=0x%x val=0x%02x\n", addr, val);
307 80cabfad bellard
#endif
308 80cabfad bellard
    if (addr == E8390_CMD) {
309 80cabfad bellard
        /* control register */
310 80cabfad bellard
        s->cmd = val;
311 a343df16 bellard
        if (!(val & E8390_STOP)) { /* START bit makes no sense on RTL8029... */
312 ee9dbb29 bellard
            s->isr &= ~ENISR_RESET;
313 e91c8a77 ths
            /* test specific case: zero length transfer */
314 80cabfad bellard
            if ((val & (E8390_RREAD | E8390_RWRITE)) &&
315 80cabfad bellard
                s->rcnt == 0) {
316 80cabfad bellard
                s->isr |= ENISR_RDC;
317 80cabfad bellard
                ne2000_update_irq(s);
318 80cabfad bellard
            }
319 80cabfad bellard
            if (val & E8390_TRANS) {
320 40545f84 bellard
                index = (s->tpsr << 8);
321 5fafdf24 ths
                /* XXX: next 2 lines are a hack to make netware 3.11 work */
322 40545f84 bellard
                if (index >= NE2000_PMEM_END)
323 40545f84 bellard
                    index -= NE2000_PMEM_SIZE;
324 40545f84 bellard
                /* fail safe: check range on the transmitted length  */
325 40545f84 bellard
                if (index + s->tcnt <= NE2000_PMEM_END) {
326 1c2045b5 Mark McLoughlin
                    qemu_send_packet(&s->nic->nc, s->mem + index, s->tcnt);
327 40545f84 bellard
                }
328 e91c8a77 ths
                /* signal end of transfer */
329 80cabfad bellard
                s->tsr = ENTSR_PTX;
330 80cabfad bellard
                s->isr |= ENISR_TX;
331 5fafdf24 ths
                s->cmd &= ~E8390_TRANS;
332 80cabfad bellard
                ne2000_update_irq(s);
333 80cabfad bellard
            }
334 80cabfad bellard
        }
335 80cabfad bellard
    } else {
336 80cabfad bellard
        page = s->cmd >> 6;
337 80cabfad bellard
        offset = addr | (page << 4);
338 80cabfad bellard
        switch(offset) {
339 80cabfad bellard
        case EN0_STARTPG:
340 80cabfad bellard
            s->start = val << 8;
341 80cabfad bellard
            break;
342 80cabfad bellard
        case EN0_STOPPG:
343 80cabfad bellard
            s->stop = val << 8;
344 80cabfad bellard
            break;
345 80cabfad bellard
        case EN0_BOUNDARY:
346 80cabfad bellard
            s->boundary = val;
347 80cabfad bellard
            break;
348 80cabfad bellard
        case EN0_IMR:
349 80cabfad bellard
            s->imr = val;
350 80cabfad bellard
            ne2000_update_irq(s);
351 80cabfad bellard
            break;
352 80cabfad bellard
        case EN0_TPSR:
353 80cabfad bellard
            s->tpsr = val;
354 80cabfad bellard
            break;
355 80cabfad bellard
        case EN0_TCNTLO:
356 80cabfad bellard
            s->tcnt = (s->tcnt & 0xff00) | val;
357 80cabfad bellard
            break;
358 80cabfad bellard
        case EN0_TCNTHI:
359 80cabfad bellard
            s->tcnt = (s->tcnt & 0x00ff) | (val << 8);
360 80cabfad bellard
            break;
361 80cabfad bellard
        case EN0_RSARLO:
362 80cabfad bellard
            s->rsar = (s->rsar & 0xff00) | val;
363 80cabfad bellard
            break;
364 80cabfad bellard
        case EN0_RSARHI:
365 80cabfad bellard
            s->rsar = (s->rsar & 0x00ff) | (val << 8);
366 80cabfad bellard
            break;
367 80cabfad bellard
        case EN0_RCNTLO:
368 80cabfad bellard
            s->rcnt = (s->rcnt & 0xff00) | val;
369 80cabfad bellard
            break;
370 80cabfad bellard
        case EN0_RCNTHI:
371 80cabfad bellard
            s->rcnt = (s->rcnt & 0x00ff) | (val << 8);
372 80cabfad bellard
            break;
373 7c9d8e07 bellard
        case EN0_RXCR:
374 7c9d8e07 bellard
            s->rxcr = val;
375 7c9d8e07 bellard
            break;
376 80cabfad bellard
        case EN0_DCFG:
377 80cabfad bellard
            s->dcfg = val;
378 80cabfad bellard
            break;
379 80cabfad bellard
        case EN0_ISR:
380 ee9dbb29 bellard
            s->isr &= ~(val & 0x7f);
381 80cabfad bellard
            ne2000_update_irq(s);
382 80cabfad bellard
            break;
383 80cabfad bellard
        case EN1_PHYS ... EN1_PHYS + 5:
384 80cabfad bellard
            s->phys[offset - EN1_PHYS] = val;
385 80cabfad bellard
            break;
386 80cabfad bellard
        case EN1_CURPAG:
387 80cabfad bellard
            s->curpag = val;
388 80cabfad bellard
            break;
389 80cabfad bellard
        case EN1_MULT ... EN1_MULT + 7:
390 80cabfad bellard
            s->mult[offset - EN1_MULT] = val;
391 80cabfad bellard
            break;
392 80cabfad bellard
        }
393 80cabfad bellard
    }
394 80cabfad bellard
}
395 80cabfad bellard
396 9453c5bc Gerd Hoffmann
uint32_t ne2000_ioport_read(void *opaque, uint32_t addr)
397 80cabfad bellard
{
398 b41a2cd1 bellard
    NE2000State *s = opaque;
399 80cabfad bellard
    int offset, page, ret;
400 80cabfad bellard
401 80cabfad bellard
    addr &= 0xf;
402 80cabfad bellard
    if (addr == E8390_CMD) {
403 80cabfad bellard
        ret = s->cmd;
404 80cabfad bellard
    } else {
405 80cabfad bellard
        page = s->cmd >> 6;
406 80cabfad bellard
        offset = addr | (page << 4);
407 80cabfad bellard
        switch(offset) {
408 80cabfad bellard
        case EN0_TSR:
409 80cabfad bellard
            ret = s->tsr;
410 80cabfad bellard
            break;
411 80cabfad bellard
        case EN0_BOUNDARY:
412 80cabfad bellard
            ret = s->boundary;
413 80cabfad bellard
            break;
414 80cabfad bellard
        case EN0_ISR:
415 80cabfad bellard
            ret = s->isr;
416 80cabfad bellard
            break;
417 ee9dbb29 bellard
        case EN0_RSARLO:
418 ee9dbb29 bellard
            ret = s->rsar & 0x00ff;
419 ee9dbb29 bellard
            break;
420 ee9dbb29 bellard
        case EN0_RSARHI:
421 ee9dbb29 bellard
            ret = s->rsar >> 8;
422 ee9dbb29 bellard
            break;
423 80cabfad bellard
        case EN1_PHYS ... EN1_PHYS + 5:
424 80cabfad bellard
            ret = s->phys[offset - EN1_PHYS];
425 80cabfad bellard
            break;
426 80cabfad bellard
        case EN1_CURPAG:
427 80cabfad bellard
            ret = s->curpag;
428 80cabfad bellard
            break;
429 80cabfad bellard
        case EN1_MULT ... EN1_MULT + 7:
430 80cabfad bellard
            ret = s->mult[offset - EN1_MULT];
431 80cabfad bellard
            break;
432 8d6c7eb8 bellard
        case EN0_RSR:
433 8d6c7eb8 bellard
            ret = s->rsr;
434 8d6c7eb8 bellard
            break;
435 a343df16 bellard
        case EN2_STARTPG:
436 a343df16 bellard
            ret = s->start >> 8;
437 a343df16 bellard
            break;
438 a343df16 bellard
        case EN2_STOPPG:
439 a343df16 bellard
            ret = s->stop >> 8;
440 a343df16 bellard
            break;
441 089af991 bellard
        case EN0_RTL8029ID0:
442 089af991 bellard
            ret = 0x50;
443 089af991 bellard
            break;
444 089af991 bellard
        case EN0_RTL8029ID1:
445 089af991 bellard
            ret = 0x43;
446 089af991 bellard
            break;
447 089af991 bellard
        case EN3_CONFIG0:
448 089af991 bellard
            ret = 0;                /* 10baseT media */
449 089af991 bellard
            break;
450 089af991 bellard
        case EN3_CONFIG2:
451 089af991 bellard
            ret = 0x40;                /* 10baseT active */
452 089af991 bellard
            break;
453 089af991 bellard
        case EN3_CONFIG3:
454 089af991 bellard
            ret = 0x40;                /* Full duplex */
455 089af991 bellard
            break;
456 80cabfad bellard
        default:
457 80cabfad bellard
            ret = 0x00;
458 80cabfad bellard
            break;
459 80cabfad bellard
        }
460 80cabfad bellard
    }
461 80cabfad bellard
#ifdef DEBUG_NE2000
462 80cabfad bellard
    printf("NE2000: read addr=0x%x val=%02x\n", addr, ret);
463 80cabfad bellard
#endif
464 80cabfad bellard
    return ret;
465 80cabfad bellard
}
466 80cabfad bellard
467 5fafdf24 ths
static inline void ne2000_mem_writeb(NE2000State *s, uint32_t addr,
468 69b91039 bellard
                                     uint32_t val)
469 ee9dbb29 bellard
{
470 5fafdf24 ths
    if (addr < 32 ||
471 ee9dbb29 bellard
        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
472 ee9dbb29 bellard
        s->mem[addr] = val;
473 ee9dbb29 bellard
    }
474 ee9dbb29 bellard
}
475 ee9dbb29 bellard
476 5fafdf24 ths
static inline void ne2000_mem_writew(NE2000State *s, uint32_t addr,
477 ee9dbb29 bellard
                                     uint32_t val)
478 ee9dbb29 bellard
{
479 ee9dbb29 bellard
    addr &= ~1; /* XXX: check exact behaviour if not even */
480 5fafdf24 ths
    if (addr < 32 ||
481 ee9dbb29 bellard
        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
482 69b91039 bellard
        *(uint16_t *)(s->mem + addr) = cpu_to_le16(val);
483 69b91039 bellard
    }
484 69b91039 bellard
}
485 69b91039 bellard
486 5fafdf24 ths
static inline void ne2000_mem_writel(NE2000State *s, uint32_t addr,
487 69b91039 bellard
                                     uint32_t val)
488 69b91039 bellard
{
489 57ccbabe bellard
    addr &= ~1; /* XXX: check exact behaviour if not even */
490 5fafdf24 ths
    if (addr < 32 ||
491 69b91039 bellard
        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
492 57ccbabe bellard
        cpu_to_le32wu((uint32_t *)(s->mem + addr), val);
493 ee9dbb29 bellard
    }
494 ee9dbb29 bellard
}
495 ee9dbb29 bellard
496 ee9dbb29 bellard
static inline uint32_t ne2000_mem_readb(NE2000State *s, uint32_t addr)
497 ee9dbb29 bellard
{
498 5fafdf24 ths
    if (addr < 32 ||
499 ee9dbb29 bellard
        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
500 ee9dbb29 bellard
        return s->mem[addr];
501 ee9dbb29 bellard
    } else {
502 ee9dbb29 bellard
        return 0xff;
503 ee9dbb29 bellard
    }
504 ee9dbb29 bellard
}
505 ee9dbb29 bellard
506 ee9dbb29 bellard
static inline uint32_t ne2000_mem_readw(NE2000State *s, uint32_t addr)
507 ee9dbb29 bellard
{
508 ee9dbb29 bellard
    addr &= ~1; /* XXX: check exact behaviour if not even */
509 5fafdf24 ths
    if (addr < 32 ||
510 ee9dbb29 bellard
        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
511 69b91039 bellard
        return le16_to_cpu(*(uint16_t *)(s->mem + addr));
512 ee9dbb29 bellard
    } else {
513 ee9dbb29 bellard
        return 0xffff;
514 ee9dbb29 bellard
    }
515 ee9dbb29 bellard
}
516 ee9dbb29 bellard
517 69b91039 bellard
static inline uint32_t ne2000_mem_readl(NE2000State *s, uint32_t addr)
518 69b91039 bellard
{
519 57ccbabe bellard
    addr &= ~1; /* XXX: check exact behaviour if not even */
520 5fafdf24 ths
    if (addr < 32 ||
521 69b91039 bellard
        (addr >= NE2000_PMEM_START && addr < NE2000_MEM_SIZE)) {
522 57ccbabe bellard
        return le32_to_cpupu((uint32_t *)(s->mem + addr));
523 69b91039 bellard
    } else {
524 69b91039 bellard
        return 0xffffffff;
525 69b91039 bellard
    }
526 69b91039 bellard
}
527 69b91039 bellard
528 3df3f6fd bellard
static inline void ne2000_dma_update(NE2000State *s, int len)
529 3df3f6fd bellard
{
530 3df3f6fd bellard
    s->rsar += len;
531 3df3f6fd bellard
    /* wrap */
532 3df3f6fd bellard
    /* XXX: check what to do if rsar > stop */
533 3df3f6fd bellard
    if (s->rsar == s->stop)
534 3df3f6fd bellard
        s->rsar = s->start;
535 3df3f6fd bellard
536 3df3f6fd bellard
    if (s->rcnt <= len) {
537 3df3f6fd bellard
        s->rcnt = 0;
538 e91c8a77 ths
        /* signal end of transfer */
539 3df3f6fd bellard
        s->isr |= ENISR_RDC;
540 3df3f6fd bellard
        ne2000_update_irq(s);
541 3df3f6fd bellard
    } else {
542 3df3f6fd bellard
        s->rcnt -= len;
543 3df3f6fd bellard
    }
544 3df3f6fd bellard
}
545 3df3f6fd bellard
546 9453c5bc Gerd Hoffmann
void ne2000_asic_ioport_write(void *opaque, uint32_t addr, uint32_t val)
547 80cabfad bellard
{
548 b41a2cd1 bellard
    NE2000State *s = opaque;
549 80cabfad bellard
550 80cabfad bellard
#ifdef DEBUG_NE2000
551 80cabfad bellard
    printf("NE2000: asic write val=0x%04x\n", val);
552 80cabfad bellard
#endif
553 ee9dbb29 bellard
    if (s->rcnt == 0)
554 3df3f6fd bellard
        return;
555 80cabfad bellard
    if (s->dcfg & 0x01) {
556 80cabfad bellard
        /* 16 bit access */
557 ee9dbb29 bellard
        ne2000_mem_writew(s, s->rsar, val);
558 3df3f6fd bellard
        ne2000_dma_update(s, 2);
559 80cabfad bellard
    } else {
560 80cabfad bellard
        /* 8 bit access */
561 ee9dbb29 bellard
        ne2000_mem_writeb(s, s->rsar, val);
562 3df3f6fd bellard
        ne2000_dma_update(s, 1);
563 80cabfad bellard
    }
564 80cabfad bellard
}
565 80cabfad bellard
566 9453c5bc Gerd Hoffmann
uint32_t ne2000_asic_ioport_read(void *opaque, uint32_t addr)
567 80cabfad bellard
{
568 b41a2cd1 bellard
    NE2000State *s = opaque;
569 80cabfad bellard
    int ret;
570 80cabfad bellard
571 80cabfad bellard
    if (s->dcfg & 0x01) {
572 80cabfad bellard
        /* 16 bit access */
573 ee9dbb29 bellard
        ret = ne2000_mem_readw(s, s->rsar);
574 3df3f6fd bellard
        ne2000_dma_update(s, 2);
575 80cabfad bellard
    } else {
576 80cabfad bellard
        /* 8 bit access */
577 ee9dbb29 bellard
        ret = ne2000_mem_readb(s, s->rsar);
578 3df3f6fd bellard
        ne2000_dma_update(s, 1);
579 80cabfad bellard
    }
580 80cabfad bellard
#ifdef DEBUG_NE2000
581 80cabfad bellard
    printf("NE2000: asic read val=0x%04x\n", ret);
582 80cabfad bellard
#endif
583 80cabfad bellard
    return ret;
584 80cabfad bellard
}
585 80cabfad bellard
586 69b91039 bellard
static void ne2000_asic_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
587 69b91039 bellard
{
588 69b91039 bellard
    NE2000State *s = opaque;
589 69b91039 bellard
590 69b91039 bellard
#ifdef DEBUG_NE2000
591 69b91039 bellard
    printf("NE2000: asic writel val=0x%04x\n", val);
592 69b91039 bellard
#endif
593 69b91039 bellard
    if (s->rcnt == 0)
594 3df3f6fd bellard
        return;
595 69b91039 bellard
    /* 32 bit access */
596 69b91039 bellard
    ne2000_mem_writel(s, s->rsar, val);
597 3df3f6fd bellard
    ne2000_dma_update(s, 4);
598 69b91039 bellard
}
599 69b91039 bellard
600 69b91039 bellard
static uint32_t ne2000_asic_ioport_readl(void *opaque, uint32_t addr)
601 69b91039 bellard
{
602 69b91039 bellard
    NE2000State *s = opaque;
603 69b91039 bellard
    int ret;
604 69b91039 bellard
605 69b91039 bellard
    /* 32 bit access */
606 69b91039 bellard
    ret = ne2000_mem_readl(s, s->rsar);
607 3df3f6fd bellard
    ne2000_dma_update(s, 4);
608 69b91039 bellard
#ifdef DEBUG_NE2000
609 69b91039 bellard
    printf("NE2000: asic readl val=0x%04x\n", ret);
610 69b91039 bellard
#endif
611 69b91039 bellard
    return ret;
612 69b91039 bellard
}
613 69b91039 bellard
614 9453c5bc Gerd Hoffmann
void ne2000_reset_ioport_write(void *opaque, uint32_t addr, uint32_t val)
615 80cabfad bellard
{
616 80cabfad bellard
    /* nothing to do (end of reset pulse) */
617 80cabfad bellard
}
618 80cabfad bellard
619 9453c5bc Gerd Hoffmann
uint32_t ne2000_reset_ioport_read(void *opaque, uint32_t addr)
620 80cabfad bellard
{
621 b41a2cd1 bellard
    NE2000State *s = opaque;
622 80cabfad bellard
    ne2000_reset(s);
623 80cabfad bellard
    return 0;
624 80cabfad bellard
}
625 80cabfad bellard
626 7c131dd5 Juan Quintela
static int ne2000_post_load(void* opaque, int version_id)
627 30ca2aab bellard
{
628 7c131dd5 Juan Quintela
    NE2000State* s = opaque;
629 a60380a5 Juan Quintela
630 7c131dd5 Juan Quintela
    if (version_id < 2) {
631 7c131dd5 Juan Quintela
        s->rxcr = 0x0c;
632 7c131dd5 Juan Quintela
    }
633 7c131dd5 Juan Quintela
    return 0;
634 a60380a5 Juan Quintela
}
635 a60380a5 Juan Quintela
636 7c131dd5 Juan Quintela
const VMStateDescription vmstate_ne2000 = {
637 7c131dd5 Juan Quintela
    .name = "ne2000",
638 7c131dd5 Juan Quintela
    .version_id = 2,
639 7c131dd5 Juan Quintela
    .minimum_version_id = 0,
640 7c131dd5 Juan Quintela
    .minimum_version_id_old = 0,
641 7c131dd5 Juan Quintela
    .post_load = ne2000_post_load,
642 7c131dd5 Juan Quintela
    .fields      = (VMStateField []) {
643 7c131dd5 Juan Quintela
        VMSTATE_UINT8_V(rxcr, NE2000State, 2),
644 7c131dd5 Juan Quintela
        VMSTATE_UINT8(cmd, NE2000State),
645 7c131dd5 Juan Quintela
        VMSTATE_UINT32(start, NE2000State),
646 7c131dd5 Juan Quintela
        VMSTATE_UINT32(stop, NE2000State),
647 7c131dd5 Juan Quintela
        VMSTATE_UINT8(boundary, NE2000State),
648 7c131dd5 Juan Quintela
        VMSTATE_UINT8(tsr, NE2000State),
649 7c131dd5 Juan Quintela
        VMSTATE_UINT8(tpsr, NE2000State),
650 7c131dd5 Juan Quintela
        VMSTATE_UINT16(tcnt, NE2000State),
651 7c131dd5 Juan Quintela
        VMSTATE_UINT16(rcnt, NE2000State),
652 7c131dd5 Juan Quintela
        VMSTATE_UINT32(rsar, NE2000State),
653 7c131dd5 Juan Quintela
        VMSTATE_UINT8(rsr, NE2000State),
654 7c131dd5 Juan Quintela
        VMSTATE_UINT8(isr, NE2000State),
655 7c131dd5 Juan Quintela
        VMSTATE_UINT8(dcfg, NE2000State),
656 7c131dd5 Juan Quintela
        VMSTATE_UINT8(imr, NE2000State),
657 7c131dd5 Juan Quintela
        VMSTATE_BUFFER(phys, NE2000State),
658 7c131dd5 Juan Quintela
        VMSTATE_UINT8(curpag, NE2000State),
659 7c131dd5 Juan Quintela
        VMSTATE_BUFFER(mult, NE2000State),
660 7c131dd5 Juan Quintela
        VMSTATE_UNUSED(4), /* was irq */
661 7c131dd5 Juan Quintela
        VMSTATE_BUFFER(mem, NE2000State),
662 7c131dd5 Juan Quintela
        VMSTATE_END_OF_LIST()
663 7c131dd5 Juan Quintela
    }
664 7c131dd5 Juan Quintela
};
665 a60380a5 Juan Quintela
666 d05ac8fa Blue Swirl
static const VMStateDescription vmstate_pci_ne2000 = {
667 7c131dd5 Juan Quintela
    .name = "ne2000",
668 7c131dd5 Juan Quintela
    .version_id = 3,
669 7c131dd5 Juan Quintela
    .minimum_version_id = 3,
670 7c131dd5 Juan Quintela
    .minimum_version_id_old = 3,
671 7c131dd5 Juan Quintela
    .fields      = (VMStateField []) {
672 7c131dd5 Juan Quintela
        VMSTATE_PCI_DEVICE(dev, PCINE2000State),
673 7c131dd5 Juan Quintela
        VMSTATE_STRUCT(ne2000, PCINE2000State, 0, vmstate_ne2000, NE2000State),
674 7c131dd5 Juan Quintela
        VMSTATE_END_OF_LIST()
675 7c131dd5 Juan Quintela
    }
676 7c131dd5 Juan Quintela
};
677 a60380a5 Juan Quintela
678 69b91039 bellard
/***********************************************************/
679 69b91039 bellard
/* PCI NE2000 definitions */
680 69b91039 bellard
681 5fafdf24 ths
static void ne2000_map(PCIDevice *pci_dev, int region_num,
682 6e355d90 Isaku Yamahata
                       pcibus_t addr, pcibus_t size, int type)
683 69b91039 bellard
{
684 377a7f06 Juan Quintela
    PCINE2000State *d = DO_UPCAST(PCINE2000State, dev, pci_dev);
685 69b91039 bellard
    NE2000State *s = &d->ne2000;
686 69b91039 bellard
687 69b91039 bellard
    register_ioport_write(addr, 16, 1, ne2000_ioport_write, s);
688 69b91039 bellard
    register_ioport_read(addr, 16, 1, ne2000_ioport_read, s);
689 69b91039 bellard
690 69b91039 bellard
    register_ioport_write(addr + 0x10, 1, 1, ne2000_asic_ioport_write, s);
691 69b91039 bellard
    register_ioport_read(addr + 0x10, 1, 1, ne2000_asic_ioport_read, s);
692 69b91039 bellard
    register_ioport_write(addr + 0x10, 2, 2, ne2000_asic_ioport_write, s);
693 69b91039 bellard
    register_ioport_read(addr + 0x10, 2, 2, ne2000_asic_ioport_read, s);
694 69b91039 bellard
    register_ioport_write(addr + 0x10, 4, 4, ne2000_asic_ioport_writel, s);
695 69b91039 bellard
    register_ioport_read(addr + 0x10, 4, 4, ne2000_asic_ioport_readl, s);
696 69b91039 bellard
697 69b91039 bellard
    register_ioport_write(addr + 0x1f, 1, 1, ne2000_reset_ioport_write, s);
698 69b91039 bellard
    register_ioport_read(addr + 0x1f, 1, 1, ne2000_reset_ioport_read, s);
699 69b91039 bellard
}
700 69b91039 bellard
701 1c2045b5 Mark McLoughlin
static void ne2000_cleanup(VLANClientState *nc)
702 b946a153 aliguori
{
703 1c2045b5 Mark McLoughlin
    NE2000State *s = DO_UPCAST(NICState, nc, nc)->opaque;
704 b946a153 aliguori
705 1c2045b5 Mark McLoughlin
    s->nic = NULL;
706 b946a153 aliguori
}
707 b946a153 aliguori
708 1c2045b5 Mark McLoughlin
static NetClientInfo net_ne2000_info = {
709 1c2045b5 Mark McLoughlin
    .type = NET_CLIENT_TYPE_NIC,
710 1c2045b5 Mark McLoughlin
    .size = sizeof(NICState),
711 1c2045b5 Mark McLoughlin
    .can_receive = ne2000_can_receive,
712 1c2045b5 Mark McLoughlin
    .receive = ne2000_receive,
713 1c2045b5 Mark McLoughlin
    .cleanup = ne2000_cleanup,
714 1c2045b5 Mark McLoughlin
};
715 1c2045b5 Mark McLoughlin
716 81a322d4 Gerd Hoffmann
static int pci_ne2000_init(PCIDevice *pci_dev)
717 69b91039 bellard
{
718 377a7f06 Juan Quintela
    PCINE2000State *d = DO_UPCAST(PCINE2000State, dev, pci_dev);
719 69b91039 bellard
    NE2000State *s;
720 69b91039 bellard
    uint8_t *pci_conf;
721 3b46e624 ths
722 69b91039 bellard
    pci_conf = d->dev.config;
723 deb54399 aliguori
    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_REALTEK);
724 a770dc7e aliguori
    pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_REALTEK_8029);
725 173a543b blueswir1
    pci_config_set_class(pci_conf, PCI_CLASS_NETWORK_ETHERNET);
726 e6c4cfd5 Michael S. Tsirkin
    /* TODO: RST# value should be 0. PCI spec 6.2.4 */
727 e6c4cfd5 Michael S. Tsirkin
    pci_conf[PCI_INTERRUPT_PIN] = 1; // interrupt pin 0
728 3b46e624 ths
729 28c2c264 Avi Kivity
    pci_register_bar(&d->dev, 0, 0x100,
730 0392a017 Isaku Yamahata
                           PCI_BASE_ADDRESS_SPACE_IO, ne2000_map);
731 69b91039 bellard
    s = &d->ne2000;
732 d537cf6c pbrook
    s->irq = d->dev.irq[0];
733 a783cc3e Gerd Hoffmann
734 a783cc3e Gerd Hoffmann
    qemu_macaddr_default_if_unset(&s->c.macaddr);
735 69b91039 bellard
    ne2000_reset(s);
736 1c2045b5 Mark McLoughlin
737 1c2045b5 Mark McLoughlin
    s->nic = qemu_new_nic(&net_ne2000_info, &s->c,
738 1c2045b5 Mark McLoughlin
                          pci_dev->qdev.info->name, pci_dev->qdev.id, s);
739 1c2045b5 Mark McLoughlin
    qemu_format_nic_info_str(&s->nic->nc, s->c.macaddr.a);
740 3b46e624 ths
741 a783cc3e Gerd Hoffmann
    if (!pci_dev->qdev.hotplugged) {
742 a783cc3e Gerd Hoffmann
        static int loaded = 0;
743 a783cc3e Gerd Hoffmann
        if (!loaded) {
744 a783cc3e Gerd Hoffmann
            rom_add_option("pxe-ne2k_pci.bin");
745 a783cc3e Gerd Hoffmann
            loaded = 1;
746 a783cc3e Gerd Hoffmann
        }
747 a783cc3e Gerd Hoffmann
    }
748 a783cc3e Gerd Hoffmann
749 81a322d4 Gerd Hoffmann
    return 0;
750 9d07d757 Paul Brook
}
751 72da4208 aliguori
752 a783cc3e Gerd Hoffmann
static int pci_ne2000_exit(PCIDevice *pci_dev)
753 a783cc3e Gerd Hoffmann
{
754 a783cc3e Gerd Hoffmann
    PCINE2000State *d = DO_UPCAST(PCINE2000State, dev, pci_dev);
755 a783cc3e Gerd Hoffmann
    NE2000State *s = &d->ne2000;
756 a783cc3e Gerd Hoffmann
757 1c2045b5 Mark McLoughlin
    qemu_del_vlan_client(&s->nic->nc);
758 a783cc3e Gerd Hoffmann
    return 0;
759 a783cc3e Gerd Hoffmann
}
760 a783cc3e Gerd Hoffmann
761 0aab0d3a Gerd Hoffmann
static PCIDeviceInfo ne2000_info = {
762 a783cc3e Gerd Hoffmann
    .qdev.name  = "ne2k_pci",
763 a783cc3e Gerd Hoffmann
    .qdev.size  = sizeof(PCINE2000State),
764 be73cfe2 Juan Quintela
    .qdev.vmsd  = &vmstate_pci_ne2000,
765 a783cc3e Gerd Hoffmann
    .init       = pci_ne2000_init,
766 a783cc3e Gerd Hoffmann
    .exit       = pci_ne2000_exit,
767 a783cc3e Gerd Hoffmann
    .qdev.props = (Property[]) {
768 a783cc3e Gerd Hoffmann
        DEFINE_NIC_PROPERTIES(PCINE2000State, ne2000.c),
769 a783cc3e Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
770 a783cc3e Gerd Hoffmann
    }
771 0aab0d3a Gerd Hoffmann
};
772 0aab0d3a Gerd Hoffmann
773 9d07d757 Paul Brook
static void ne2000_register_devices(void)
774 9d07d757 Paul Brook
{
775 0aab0d3a Gerd Hoffmann
    pci_qdev_register(&ne2000_info);
776 69b91039 bellard
}
777 9d07d757 Paul Brook
778 9d07d757 Paul Brook
device_init(ne2000_register_devices)