]> git.proxmox.com Git - mirror_qemu.git/blame - hw/net/e1000.c
e1000: set RX descriptor status in a separate operation
[mirror_qemu.git] / hw / net / e1000.c
CommitLineData
7c23b892
AZ
1/*
2 * QEMU e1000 emulation
3 *
2758aa52
MT
4 * Software developer's manual:
5 * http://download.intel.com/design/network/manuals/8254x_GBe_SDM.pdf
6 *
7c23b892
AZ
7 * Nir Peleg, Tutis Systems Ltd. for Qumranet Inc.
8 * Copyright (c) 2008 Qumranet
9 * Based on work done by:
10 * Copyright (c) 2007 Dan Aloni
11 * Copyright (c) 2004 Antony T Curtis
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
61f3c91a 16 * version 2.1 of the License, or (at your option) any later version.
7c23b892
AZ
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
8167ee88 24 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
7c23b892
AZ
25 */
26
27
e8d40465 28#include "qemu/osdep.h"
83c9f4ca 29#include "hw/pci/pci.h"
a27bd6c7 30#include "hw/qdev-properties.h"
d6454270 31#include "migration/vmstate.h"
a1d7e475 32#include "net/eth.h"
1422e32d 33#include "net/net.h"
7200ac3c 34#include "net/checksum.h"
9c17d615
PB
35#include "sysemu/sysemu.h"
36#include "sysemu/dma.h"
97410dde 37#include "qemu/iov.h"
0b8fa32f 38#include "qemu/module.h"
20302e71 39#include "qemu/range.h"
7c23b892 40
093454e2 41#include "e1000x_common.h"
1001cf45 42#include "trace.h"
db1015e9 43#include "qom/object.h"
7c23b892 44
3b274301
LB
45static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
46
b4053c64 47/* #define E1000_DEBUG */
7c23b892 48
27124888 49#ifdef E1000_DEBUG
7c23b892 50enum {
20f3e863
LB
51 DEBUG_GENERAL, DEBUG_IO, DEBUG_MMIO, DEBUG_INTERRUPT,
52 DEBUG_RX, DEBUG_TX, DEBUG_MDIC, DEBUG_EEPROM,
53 DEBUG_UNKNOWN, DEBUG_TXSUM, DEBUG_TXERR, DEBUG_RXERR,
f9c1cdf4 54 DEBUG_RXFILTER, DEBUG_PHY, DEBUG_NOTYET,
7c23b892 55};
20f3e863 56#define DBGBIT(x) (1<<DEBUG_##x)
7c23b892
AZ
57static int debugflags = DBGBIT(TXERR) | DBGBIT(GENERAL);
58
20f3e863 59#define DBGOUT(what, fmt, ...) do { \
7c23b892 60 if (debugflags & DBGBIT(what)) \
6c7f4b47 61 fprintf(stderr, "e1000: " fmt, ## __VA_ARGS__); \
7c23b892
AZ
62 } while (0)
63#else
20f3e863 64#define DBGOUT(what, fmt, ...) do {} while (0)
7c23b892
AZ
65#endif
66
67#define IOPORT_SIZE 0x40
e94bbefe 68#define PNPMMIO_SIZE 0x20000
78aeb23e 69#define MIN_BUF_SIZE 60 /* Min. octets in an ethernet frame sans FCS */
7c23b892 70
97410dde
VM
71#define MAXIMUM_ETHERNET_HDR_LEN (14+4)
72
7c23b892
AZ
73/*
74 * HW models:
8597f2e1 75 * E1000_DEV_ID_82540EM works with Windows, Linux, and OS X <= 10.8
7c23b892 76 * E1000_DEV_ID_82544GC_COPPER appears to work; not well tested
8597f2e1 77 * E1000_DEV_ID_82545EM_COPPER works with Linux and OS X >= 10.6
7c23b892
AZ
78 * Others never tested
79 */
7c23b892 80
db1015e9 81struct E1000State_st {
b08340d5
AF
82 /*< private >*/
83 PCIDevice parent_obj;
84 /*< public >*/
85
a03e2aec 86 NICState *nic;
fbdaa002 87 NICConf conf;
ad00a9b9
AK
88 MemoryRegion mmio;
89 MemoryRegion io;
7c23b892
AZ
90
91 uint32_t mac_reg[0x8000];
92 uint16_t phy_reg[0x20];
93 uint16_t eeprom_data[64];
94
95 uint32_t rxbuf_size;
96 uint32_t rxbuf_min_shift;
7c23b892
AZ
97 struct e1000_tx {
98 unsigned char header[256];
8f2e8d1f 99 unsigned char vlan_header[4];
b10fec9b 100 /* Fields vlan and data must not be reordered or separated. */
8f2e8d1f 101 unsigned char vlan[4];
7c23b892
AZ
102 unsigned char data[0x10000];
103 uint16_t size;
8f2e8d1f 104 unsigned char vlan_needed;
7d08c73e
ES
105 unsigned char sum_needed;
106 bool cptse;
093454e2 107 e1000x_txd_props props;
d62644b4 108 e1000x_txd_props tso_props;
7c23b892 109 uint16_t tso_frames;
25ddb946 110 bool busy;
7c23b892
AZ
111 } tx;
112
113 struct {
20f3e863 114 uint32_t val_in; /* shifted in from guest driver */
7c23b892
AZ
115 uint16_t bitnum_in;
116 uint16_t bitnum_out;
117 uint16_t reading;
118 uint32_t old_eecd;
119 } eecd_state;
b9d03e35
JW
120
121 QEMUTimer *autoneg_timer;
2af234e6 122
e9845f09
VM
123 QEMUTimer *mit_timer; /* Mitigation timer. */
124 bool mit_timer_on; /* Mitigation timer is running. */
125 bool mit_irq_level; /* Tracks interrupt pin level. */
126 uint32_t mit_ide; /* Tracks E1000_TXD_CMD_IDE bit. */
127
157628d0
YCL
128 QEMUTimer *flush_queue_timer;
129
2af234e6
MT
130/* Compatibility flags for migration to/from qemu 1.3.0 and older */
131#define E1000_FLAG_AUTONEG_BIT 0
e9845f09 132#define E1000_FLAG_MIT_BIT 1
9e117734 133#define E1000_FLAG_MAC_BIT 2
46f2a9ec 134#define E1000_FLAG_TSO_BIT 3
a1d7e475 135#define E1000_FLAG_VET_BIT 4
2af234e6 136#define E1000_FLAG_AUTONEG (1 << E1000_FLAG_AUTONEG_BIT)
e9845f09 137#define E1000_FLAG_MIT (1 << E1000_FLAG_MIT_BIT)
9e117734 138#define E1000_FLAG_MAC (1 << E1000_FLAG_MAC_BIT)
46f2a9ec 139#define E1000_FLAG_TSO (1 << E1000_FLAG_TSO_BIT)
a1d7e475
CW
140#define E1000_FLAG_VET (1 << E1000_FLAG_VET_BIT)
141
2af234e6 142 uint32_t compat_flags;
3c4053c5 143 bool received_tx_tso;
ff214d42 144 bool use_tso_for_migration;
59354484 145 e1000x_txd_props mig_props;
db1015e9
EH
146};
147typedef struct E1000State_st E1000State;
7c23b892 148
bc0f0674
LB
149#define chkflag(x) (s->compat_flags & E1000_FLAG_##x)
150
db1015e9 151struct E1000BaseClass {
8597f2e1
GS
152 PCIDeviceClass parent_class;
153 uint16_t phy_id2;
db1015e9
EH
154};
155typedef struct E1000BaseClass E1000BaseClass;
8597f2e1
GS
156
157#define TYPE_E1000_BASE "e1000-base"
567a3c9e 158
8110fa1d
EH
159DECLARE_OBJ_CHECKERS(E1000State, E1000BaseClass,
160 E1000, TYPE_E1000_BASE)
8597f2e1 161
567a3c9e 162
71aadd3c 163static void
093454e2 164e1000_link_up(E1000State *s)
71aadd3c 165{
093454e2
DF
166 e1000x_update_regs_on_link_up(s->mac_reg, s->phy_reg);
167
168 /* E1000_STATUS_LU is tested by e1000_can_receive() */
169 qemu_flush_queued_packets(qemu_get_queue(s->nic));
71aadd3c
JW
170}
171
172static void
093454e2 173e1000_autoneg_done(E1000State *s)
71aadd3c 174{
093454e2 175 e1000x_update_regs_on_autoneg_done(s->mac_reg, s->phy_reg);
5df6a185
SH
176
177 /* E1000_STATUS_LU is tested by e1000_can_receive() */
178 qemu_flush_queued_packets(qemu_get_queue(s->nic));
71aadd3c
JW
179}
180
1195fed9
GS
181static bool
182have_autoneg(E1000State *s)
183{
bc0f0674 184 return chkflag(AUTONEG) && (s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN);
1195fed9
GS
185}
186
b9d03e35
JW
187static void
188set_phy_ctrl(E1000State *s, int index, uint16_t val)
189{
1195fed9
GS
190 /* bits 0-5 reserved; MII_CR_[RESTART_AUTO_NEG,RESET] are self clearing */
191 s->phy_reg[PHY_CTRL] = val & ~(0x3f |
192 MII_CR_RESET |
193 MII_CR_RESTART_AUTO_NEG);
194
2af234e6
MT
195 /*
196 * QEMU 1.3 does not support link auto-negotiation emulation, so if we
197 * migrate during auto negotiation, after migration the link will be
198 * down.
199 */
1195fed9 200 if (have_autoneg(s) && (val & MII_CR_RESTART_AUTO_NEG)) {
093454e2 201 e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer);
b9d03e35
JW
202 }
203}
204
b9d03e35
JW
205static void (*phyreg_writeops[])(E1000State *, int, uint16_t) = {
206 [PHY_CTRL] = set_phy_ctrl,
207};
208
209enum { NPHYWRITEOPS = ARRAY_SIZE(phyreg_writeops) };
210
7c23b892 211enum { PHY_R = 1, PHY_W = 2, PHY_RW = PHY_R | PHY_W };
88b4e9db 212static const char phy_regcap[0x20] = {
20f3e863
LB
213 [PHY_STATUS] = PHY_R, [M88E1000_EXT_PHY_SPEC_CTRL] = PHY_RW,
214 [PHY_ID1] = PHY_R, [M88E1000_PHY_SPEC_CTRL] = PHY_RW,
215 [PHY_CTRL] = PHY_RW, [PHY_1000T_CTRL] = PHY_RW,
216 [PHY_LP_ABILITY] = PHY_R, [PHY_1000T_STATUS] = PHY_R,
217 [PHY_AUTONEG_ADV] = PHY_RW, [M88E1000_RX_ERR_CNTR] = PHY_R,
218 [PHY_ID2] = PHY_R, [M88E1000_PHY_SPEC_STATUS] = PHY_R,
6883b591 219 [PHY_AUTONEG_EXP] = PHY_R,
7c23b892
AZ
220};
221
8597f2e1 222/* PHY_ID2 documented in 8254x_GBe_SDM.pdf, pp. 250 */
814cd3ac 223static const uint16_t phy_reg_init[] = {
20f3e863 224 [PHY_CTRL] = MII_CR_SPEED_SELECT_MSB |
9616c290
GS
225 MII_CR_FULL_DUPLEX |
226 MII_CR_AUTO_NEG_EN,
227
228 [PHY_STATUS] = MII_SR_EXTENDED_CAPS |
229 MII_SR_LINK_STATUS | /* link initially up */
230 MII_SR_AUTONEG_CAPS |
231 /* MII_SR_AUTONEG_COMPLETE: initially NOT completed */
232 MII_SR_PREAMBLE_SUPPRESS |
233 MII_SR_EXTENDED_STATUS |
234 MII_SR_10T_HD_CAPS |
235 MII_SR_10T_FD_CAPS |
236 MII_SR_100X_HD_CAPS |
237 MII_SR_100X_FD_CAPS,
238
239 [PHY_ID1] = 0x141,
240 /* [PHY_ID2] configured per DevId, from e1000_reset() */
241 [PHY_AUTONEG_ADV] = 0xde1,
242 [PHY_LP_ABILITY] = 0x1e0,
243 [PHY_1000T_CTRL] = 0x0e00,
244 [PHY_1000T_STATUS] = 0x3c00,
245 [M88E1000_PHY_SPEC_CTRL] = 0x360,
814cd3ac 246 [M88E1000_PHY_SPEC_STATUS] = 0xac00,
9616c290 247 [M88E1000_EXT_PHY_SPEC_CTRL] = 0x0d60,
814cd3ac
MT
248};
249
250static const uint32_t mac_reg_init[] = {
20f3e863
LB
251 [PBA] = 0x00100030,
252 [LEDCTL] = 0x602,
253 [CTRL] = E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN0 |
814cd3ac 254 E1000_CTRL_SPD_1000 | E1000_CTRL_SLU,
20f3e863 255 [STATUS] = 0x80000000 | E1000_STATUS_GIO_MASTER_ENABLE |
814cd3ac
MT
256 E1000_STATUS_ASDV | E1000_STATUS_MTXCKOK |
257 E1000_STATUS_SPEED_1000 | E1000_STATUS_FD |
258 E1000_STATUS_LU,
20f3e863 259 [MANC] = E1000_MANC_EN_MNG2HOST | E1000_MANC_RCV_TCO_EN |
814cd3ac
MT
260 E1000_MANC_ARP_EN | E1000_MANC_0298_EN |
261 E1000_MANC_RMCP_EN,
262};
263
e9845f09
VM
264/* Helper function, *curr == 0 means the value is not set */
265static inline void
266mit_update_delay(uint32_t *curr, uint32_t value)
267{
268 if (value && (*curr == 0 || value < *curr)) {
269 *curr = value;
270 }
271}
272
7c23b892
AZ
273static void
274set_interrupt_cause(E1000State *s, int index, uint32_t val)
275{
b08340d5 276 PCIDevice *d = PCI_DEVICE(s);
e9845f09
VM
277 uint32_t pending_ints;
278 uint32_t mit_delay;
b08340d5 279
7c23b892 280 s->mac_reg[ICR] = val;
a52a8841
MT
281
282 /*
283 * Make sure ICR and ICS registers have the same value.
284 * The spec says that the ICS register is write-only. However in practice,
285 * on real hardware ICS is readable, and for reads it has the same value as
286 * ICR (except that ICS does not have the clear on read behaviour of ICR).
287 *
288 * The VxWorks PRO/1000 driver uses this behaviour.
289 */
b1332393 290 s->mac_reg[ICS] = val;
a52a8841 291
e9845f09
VM
292 pending_ints = (s->mac_reg[IMS] & s->mac_reg[ICR]);
293 if (!s->mit_irq_level && pending_ints) {
294 /*
295 * Here we detect a potential raising edge. We postpone raising the
296 * interrupt line if we are inside the mitigation delay window
297 * (s->mit_timer_on == 1).
298 * We provide a partial implementation of interrupt mitigation,
299 * emulating only RADV, TADV and ITR (lower 16 bits, 1024ns units for
300 * RADV and TADV, 256ns units for ITR). RDTR is only used to enable
301 * RADV; relative timers based on TIDV and RDTR are not implemented.
302 */
303 if (s->mit_timer_on) {
304 return;
305 }
bc0f0674 306 if (chkflag(MIT)) {
e9845f09
VM
307 /* Compute the next mitigation delay according to pending
308 * interrupts and the current values of RADV (provided
309 * RDTR!=0), TADV and ITR.
310 * Then rearm the timer.
311 */
312 mit_delay = 0;
313 if (s->mit_ide &&
314 (pending_ints & (E1000_ICR_TXQE | E1000_ICR_TXDW))) {
315 mit_update_delay(&mit_delay, s->mac_reg[TADV] * 4);
316 }
317 if (s->mac_reg[RDTR] && (pending_ints & E1000_ICS_RXT0)) {
318 mit_update_delay(&mit_delay, s->mac_reg[RADV] * 4);
319 }
320 mit_update_delay(&mit_delay, s->mac_reg[ITR]);
321
74004e8c
SJ
322 /*
323 * According to e1000 SPEC, the Ethernet controller guarantees
324 * a maximum observable interrupt rate of 7813 interrupts/sec.
325 * Thus if mit_delay < 500 then the delay should be set to the
326 * minimum delay possible which is 500.
327 */
328 mit_delay = (mit_delay < 500) ? 500 : mit_delay;
329
b92233b3
SJ
330 s->mit_timer_on = 1;
331 timer_mod(s->mit_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
332 mit_delay * 256);
e9845f09
VM
333 s->mit_ide = 0;
334 }
335 }
336
337 s->mit_irq_level = (pending_ints != 0);
9e64f8a3 338 pci_set_irq(d, s->mit_irq_level);
e9845f09
VM
339}
340
341static void
342e1000_mit_timer(void *opaque)
343{
344 E1000State *s = opaque;
345
346 s->mit_timer_on = 0;
347 /* Call set_interrupt_cause to update the irq level (if necessary). */
348 set_interrupt_cause(s, 0, s->mac_reg[ICR]);
7c23b892
AZ
349}
350
351static void
352set_ics(E1000State *s, int index, uint32_t val)
353{
354 DBGOUT(INTERRUPT, "set_ics %x, ICR %x, IMR %x\n", val, s->mac_reg[ICR],
355 s->mac_reg[IMS]);
356 set_interrupt_cause(s, 0, val | s->mac_reg[ICR]);
357}
358
d52aec95
GS
359static void
360e1000_autoneg_timer(void *opaque)
361{
362 E1000State *s = opaque;
363 if (!qemu_get_queue(s->nic)->link_down) {
093454e2 364 e1000_autoneg_done(s);
d52aec95
GS
365 set_ics(s, 0, E1000_ICS_LSC); /* signal link status change to guest */
366 }
367}
368
a1d7e475
CW
369static bool e1000_vet_init_need(void *opaque)
370{
371 E1000State *s = opaque;
372
373 return chkflag(VET);
374}
375
814cd3ac
MT
376static void e1000_reset(void *opaque)
377{
378 E1000State *d = opaque;
c51325d8 379 E1000BaseClass *edc = E1000_GET_CLASS(d);
372254c6 380 uint8_t *macaddr = d->conf.macaddr.a;
814cd3ac 381
bc72ad67 382 timer_del(d->autoneg_timer);
e9845f09 383 timer_del(d->mit_timer);
157628d0 384 timer_del(d->flush_queue_timer);
e9845f09
VM
385 d->mit_timer_on = 0;
386 d->mit_irq_level = 0;
387 d->mit_ide = 0;
814cd3ac
MT
388 memset(d->phy_reg, 0, sizeof d->phy_reg);
389 memmove(d->phy_reg, phy_reg_init, sizeof phy_reg_init);
8597f2e1 390 d->phy_reg[PHY_ID2] = edc->phy_id2;
814cd3ac
MT
391 memset(d->mac_reg, 0, sizeof d->mac_reg);
392 memmove(d->mac_reg, mac_reg_init, sizeof mac_reg_init);
393 d->rxbuf_min_shift = 1;
394 memset(&d->tx, 0, sizeof d->tx);
395
b356f76d 396 if (qemu_get_queue(d->nic)->link_down) {
093454e2 397 e1000x_update_regs_on_link_down(d->mac_reg, d->phy_reg);
814cd3ac 398 }
372254c6 399
093454e2 400 e1000x_reset_mac_addr(d->nic, d->mac_reg, macaddr);
a1d7e475
CW
401
402 if (e1000_vet_init_need(d)) {
403 d->mac_reg[VET] = ETH_P_VLAN;
404 }
814cd3ac
MT
405}
406
cab3c825
KW
407static void
408set_ctrl(E1000State *s, int index, uint32_t val)
409{
410 /* RST is self clearing */
411 s->mac_reg[CTRL] = val & ~E1000_CTRL_RST;
412}
413
157628d0
YCL
414static void
415e1000_flush_queue_timer(void *opaque)
416{
417 E1000State *s = opaque;
418
419 qemu_flush_queued_packets(qemu_get_queue(s->nic));
420}
421
7c23b892
AZ
422static void
423set_rx_control(E1000State *s, int index, uint32_t val)
424{
425 s->mac_reg[RCTL] = val;
093454e2 426 s->rxbuf_size = e1000x_rxbufsize(val);
7c23b892
AZ
427 s->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1;
428 DBGOUT(RX, "RCTL: %d, mac_reg[RCTL] = 0x%x\n", s->mac_reg[RDT],
429 s->mac_reg[RCTL]);
157628d0
YCL
430 timer_mod(s->flush_queue_timer,
431 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1000);
7c23b892
AZ
432}
433
434static void
435set_mdic(E1000State *s, int index, uint32_t val)
436{
437 uint32_t data = val & E1000_MDIC_DATA_MASK;
438 uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT);
439
440 if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) // phy #
441 val = s->mac_reg[MDIC] | E1000_MDIC_ERROR;
442 else if (val & E1000_MDIC_OP_READ) {
443 DBGOUT(MDIC, "MDIC read reg 0x%x\n", addr);
444 if (!(phy_regcap[addr] & PHY_R)) {
445 DBGOUT(MDIC, "MDIC read reg %x unhandled\n", addr);
446 val |= E1000_MDIC_ERROR;
447 } else
448 val = (val ^ data) | s->phy_reg[addr];
449 } else if (val & E1000_MDIC_OP_WRITE) {
450 DBGOUT(MDIC, "MDIC write reg 0x%x, value 0x%x\n", addr, data);
451 if (!(phy_regcap[addr] & PHY_W)) {
452 DBGOUT(MDIC, "MDIC write reg %x unhandled\n", addr);
453 val |= E1000_MDIC_ERROR;
b9d03e35
JW
454 } else {
455 if (addr < NPHYWRITEOPS && phyreg_writeops[addr]) {
456 phyreg_writeops[addr](s, index, data);
1195fed9
GS
457 } else {
458 s->phy_reg[addr] = data;
b9d03e35 459 }
b9d03e35 460 }
7c23b892
AZ
461 }
462 s->mac_reg[MDIC] = val | E1000_MDIC_READY;
17fbbb0b
JW
463
464 if (val & E1000_MDIC_INT_EN) {
465 set_ics(s, 0, E1000_ICR_MDAC);
466 }
7c23b892
AZ
467}
468
469static uint32_t
470get_eecd(E1000State *s, int index)
471{
472 uint32_t ret = E1000_EECD_PRES|E1000_EECD_GNT | s->eecd_state.old_eecd;
473
474 DBGOUT(EEPROM, "reading eeprom bit %d (reading %d)\n",
475 s->eecd_state.bitnum_out, s->eecd_state.reading);
476 if (!s->eecd_state.reading ||
477 ((s->eeprom_data[(s->eecd_state.bitnum_out >> 4) & 0x3f] >>
478 ((s->eecd_state.bitnum_out & 0xf) ^ 0xf))) & 1)
479 ret |= E1000_EECD_DO;
480 return ret;
481}
482
483static void
484set_eecd(E1000State *s, int index, uint32_t val)
485{
486 uint32_t oldval = s->eecd_state.old_eecd;
487
488 s->eecd_state.old_eecd = val & (E1000_EECD_SK | E1000_EECD_CS |
489 E1000_EECD_DI|E1000_EECD_FWE_MASK|E1000_EECD_REQ);
20f3e863
LB
490 if (!(E1000_EECD_CS & val)) { /* CS inactive; nothing to do */
491 return;
492 }
493 if (E1000_EECD_CS & (val ^ oldval)) { /* CS rise edge; reset state */
494 s->eecd_state.val_in = 0;
495 s->eecd_state.bitnum_in = 0;
496 s->eecd_state.bitnum_out = 0;
497 s->eecd_state.reading = 0;
9651ac55 498 }
20f3e863 499 if (!(E1000_EECD_SK & (val ^ oldval))) { /* no clock edge */
7c23b892 500 return;
20f3e863
LB
501 }
502 if (!(E1000_EECD_SK & val)) { /* falling edge */
7c23b892
AZ
503 s->eecd_state.bitnum_out++;
504 return;
505 }
7c23b892
AZ
506 s->eecd_state.val_in <<= 1;
507 if (val & E1000_EECD_DI)
508 s->eecd_state.val_in |= 1;
509 if (++s->eecd_state.bitnum_in == 9 && !s->eecd_state.reading) {
510 s->eecd_state.bitnum_out = ((s->eecd_state.val_in & 0x3f)<<4)-1;
511 s->eecd_state.reading = (((s->eecd_state.val_in >> 6) & 7) ==
512 EEPROM_READ_OPCODE_MICROWIRE);
513 }
514 DBGOUT(EEPROM, "eeprom bitnum in %d out %d, reading %d\n",
515 s->eecd_state.bitnum_in, s->eecd_state.bitnum_out,
516 s->eecd_state.reading);
517}
518
519static uint32_t
520flash_eerd_read(E1000State *s, int x)
521{
522 unsigned int index, r = s->mac_reg[EERD] & ~E1000_EEPROM_RW_REG_START;
523
b1332393
BP
524 if ((s->mac_reg[EERD] & E1000_EEPROM_RW_REG_START) == 0)
525 return (s->mac_reg[EERD]);
526
7c23b892 527 if ((index = r >> E1000_EEPROM_RW_ADDR_SHIFT) > EEPROM_CHECKSUM_REG)
b1332393
BP
528 return (E1000_EEPROM_RW_REG_DONE | r);
529
530 return ((s->eeprom_data[index] << E1000_EEPROM_RW_REG_DATA) |
531 E1000_EEPROM_RW_REG_DONE | r);
7c23b892
AZ
532}
533
7c23b892
AZ
534static void
535putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t css, uint32_t cse)
536{
c6a6a5e3
AL
537 uint32_t sum;
538
7c23b892
AZ
539 if (cse && cse < n)
540 n = cse + 1;
c6a6a5e3
AL
541 if (sloc < n-1) {
542 sum = net_checksum_add(n-css, data+css);
0dacea92 543 stw_be_p(data + sloc, net_checksum_finish_nozero(sum));
c6a6a5e3 544 }
7c23b892
AZ
545}
546
3b274301
LB
547static inline void
548inc_tx_bcast_or_mcast_count(E1000State *s, const unsigned char *arr)
549{
550 if (!memcmp(arr, bcast, sizeof bcast)) {
093454e2 551 e1000x_inc_reg_if_not_full(s->mac_reg, BPTC);
3b274301 552 } else if (arr[0] & 1) {
093454e2 553 e1000x_inc_reg_if_not_full(s->mac_reg, MPTC);
3b274301
LB
554 }
555}
556
93e37d76
JW
557static void
558e1000_send_packet(E1000State *s, const uint8_t *buf, int size)
559{
3b274301
LB
560 static const int PTCregs[6] = { PTC64, PTC127, PTC255, PTC511,
561 PTC1023, PTC1522 };
562
b356f76d 563 NetClientState *nc = qemu_get_queue(s->nic);
93e37d76 564 if (s->phy_reg[PHY_CTRL] & MII_CR_LOOPBACK) {
1caff034 565 qemu_receive_packet(nc, buf, size);
93e37d76 566 } else {
b356f76d 567 qemu_send_packet(nc, buf, size);
93e37d76 568 }
3b274301 569 inc_tx_bcast_or_mcast_count(s, buf);
093454e2 570 e1000x_increase_size_stats(s->mac_reg, PTCregs, size);
93e37d76
JW
571}
572
7c23b892
AZ
573static void
574xmit_seg(E1000State *s)
575{
14e60aae 576 uint16_t len;
45e93764 577 unsigned int frames = s->tx.tso_frames, css, sofar;
7c23b892 578 struct e1000_tx *tp = &s->tx;
d62644b4 579 struct e1000x_txd_props *props = tp->cptse ? &tp->tso_props : &tp->props;
7c23b892 580
d62644b4
ES
581 if (tp->cptse) {
582 css = props->ipcss;
7c23b892
AZ
583 DBGOUT(TXSUM, "frames %d size %d ipcss %d\n",
584 frames, tp->size, css);
d62644b4 585 if (props->ip) { /* IPv4 */
d8ee2591
PM
586 stw_be_p(tp->data+css+2, tp->size - css);
587 stw_be_p(tp->data+css+4,
14e60aae 588 lduw_be_p(tp->data + css + 4) + frames);
20f3e863 589 } else { /* IPv6 */
d8ee2591 590 stw_be_p(tp->data+css+4, tp->size - css);
20f3e863 591 }
d62644b4 592 css = props->tucss;
7c23b892 593 len = tp->size - css;
d62644b4
ES
594 DBGOUT(TXSUM, "tcp %d tucss %d len %d\n", props->tcp, css, len);
595 if (props->tcp) {
596 sofar = frames * props->mss;
6bd194ab 597 stl_be_p(tp->data+css+4, ldl_be_p(tp->data+css+4)+sofar); /* seq */
d62644b4 598 if (props->paylen - sofar > props->mss) {
20f3e863 599 tp->data[css + 13] &= ~9; /* PSH, FIN */
3b274301 600 } else if (frames) {
093454e2 601 e1000x_inc_reg_if_not_full(s->mac_reg, TSCTC);
3b274301 602 }
d62644b4 603 } else { /* UDP */
d8ee2591 604 stw_be_p(tp->data+css+4, len);
d62644b4 605 }
7d08c73e 606 if (tp->sum_needed & E1000_TXD_POPTS_TXSM) {
e685b4eb 607 unsigned int phsum;
7c23b892 608 // add pseudo-header length before checksum calculation
d62644b4 609 void *sp = tp->data + props->tucso;
14e60aae
PM
610
611 phsum = lduw_be_p(sp) + len;
e685b4eb 612 phsum = (phsum >> 16) + (phsum & 0xffff);
d8ee2591 613 stw_be_p(sp, phsum);
7c23b892
AZ
614 }
615 tp->tso_frames++;
616 }
617
7d08c73e 618 if (tp->sum_needed & E1000_TXD_POPTS_TXSM) {
d62644b4 619 putsum(tp->data, tp->size, props->tucso, props->tucss, props->tucse);
093454e2 620 }
7d08c73e 621 if (tp->sum_needed & E1000_TXD_POPTS_IXSM) {
d62644b4 622 putsum(tp->data, tp->size, props->ipcso, props->ipcss, props->ipcse);
093454e2 623 }
8f2e8d1f 624 if (tp->vlan_needed) {
b10fec9b
SW
625 memmove(tp->vlan, tp->data, 4);
626 memmove(tp->data, tp->data + 4, 8);
8f2e8d1f 627 memcpy(tp->data + 8, tp->vlan_header, 4);
93e37d76 628 e1000_send_packet(s, tp->vlan, tp->size + 4);
20f3e863 629 } else {
93e37d76 630 e1000_send_packet(s, tp->data, tp->size);
20f3e863
LB
631 }
632
093454e2
DF
633 e1000x_inc_reg_if_not_full(s->mac_reg, TPT);
634 e1000x_grow_8reg_if_not_full(s->mac_reg, TOTL, s->tx.size);
1f67f92c 635 s->mac_reg[GPTC] = s->mac_reg[TPT];
3b274301
LB
636 s->mac_reg[GOTCL] = s->mac_reg[TOTL];
637 s->mac_reg[GOTCH] = s->mac_reg[TOTH];
7c23b892
AZ
638}
639
640static void
641process_tx_desc(E1000State *s, struct e1000_tx_desc *dp)
642{
b08340d5 643 PCIDevice *d = PCI_DEVICE(s);
7c23b892
AZ
644 uint32_t txd_lower = le32_to_cpu(dp->lower.data);
645 uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D);
093454e2 646 unsigned int split_size = txd_lower & 0xffff, bytes, sz;
a0ae17a6 647 unsigned int msh = 0xfffff;
7c23b892
AZ
648 uint64_t addr;
649 struct e1000_context_desc *xp = (struct e1000_context_desc *)dp;
650 struct e1000_tx *tp = &s->tx;
651
e9845f09 652 s->mit_ide |= (txd_lower & E1000_TXD_CMD_IDE);
20f3e863 653 if (dtype == E1000_TXD_CMD_DEXT) { /* context descriptor */
d62644b4
ES
654 if (le32_to_cpu(xp->cmd_and_length) & E1000_TXD_CMD_TSE) {
655 e1000x_read_tx_ctx_descr(xp, &tp->tso_props);
ff214d42 656 s->use_tso_for_migration = 1;
d62644b4
ES
657 tp->tso_frames = 0;
658 } else {
659 e1000x_read_tx_ctx_descr(xp, &tp->props);
ff214d42 660 s->use_tso_for_migration = 0;
7c23b892
AZ
661 }
662 return;
1b0009db
AZ
663 } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) {
664 // data descriptor
735e77ec 665 if (tp->size == 0) {
7d08c73e 666 tp->sum_needed = le32_to_cpu(dp->upper.data) >> 8;
735e77ec 667 }
7d08c73e 668 tp->cptse = (txd_lower & E1000_TXD_CMD_TSE) ? 1 : 0;
43ad7e3e 669 } else {
1b0009db 670 // legacy descriptor
7d08c73e 671 tp->cptse = 0;
43ad7e3e 672 }
7c23b892 673
093454e2
DF
674 if (e1000x_vlan_enabled(s->mac_reg) &&
675 e1000x_is_vlan_txd(txd_lower) &&
7d08c73e 676 (tp->cptse || txd_lower & E1000_TXD_CMD_EOP)) {
8f2e8d1f 677 tp->vlan_needed = 1;
d8ee2591 678 stw_be_p(tp->vlan_header,
4e60a250 679 le16_to_cpu(s->mac_reg[VET]));
d8ee2591 680 stw_be_p(tp->vlan_header + 2,
8f2e8d1f
AL
681 le16_to_cpu(dp->upper.fields.special));
682 }
20f3e863 683
7c23b892 684 addr = le64_to_cpu(dp->buffer_addr);
d62644b4
ES
685 if (tp->cptse) {
686 msh = tp->tso_props.hdr_len + tp->tso_props.mss;
1b0009db
AZ
687 do {
688 bytes = split_size;
3de46e6f
JW
689 if (tp->size >= msh) {
690 goto eop;
691 }
1b0009db
AZ
692 if (tp->size + bytes > msh)
693 bytes = msh - tp->size;
65f82df0
AL
694
695 bytes = MIN(sizeof(tp->data) - tp->size, bytes);
b08340d5 696 pci_dma_read(d, addr, tp->data + tp->size, bytes);
a0ae17a6 697 sz = tp->size + bytes;
d62644b4
ES
698 if (sz >= tp->tso_props.hdr_len
699 && tp->size < tp->tso_props.hdr_len) {
700 memmove(tp->header, tp->data, tp->tso_props.hdr_len);
a0ae17a6 701 }
1b0009db
AZ
702 tp->size = sz;
703 addr += bytes;
704 if (sz == msh) {
705 xmit_seg(s);
d62644b4
ES
706 memmove(tp->data, tp->header, tp->tso_props.hdr_len);
707 tp->size = tp->tso_props.hdr_len;
1b0009db 708 }
b947ac2b
PP
709 split_size -= bytes;
710 } while (bytes && split_size);
1b0009db 711 } else {
65f82df0 712 split_size = MIN(sizeof(tp->data) - tp->size, split_size);
b08340d5 713 pci_dma_read(d, addr, tp->data + tp->size, split_size);
1b0009db 714 tp->size += split_size;
7c23b892 715 }
7c23b892 716
3de46e6f 717eop:
7c23b892
AZ
718 if (!(txd_lower & E1000_TXD_CMD_EOP))
719 return;
d62644b4 720 if (!(tp->cptse && tp->size < tp->tso_props.hdr_len)) {
7c23b892 721 xmit_seg(s);
a0ae17a6 722 }
7c23b892 723 tp->tso_frames = 0;
7d08c73e 724 tp->sum_needed = 0;
8f2e8d1f 725 tp->vlan_needed = 0;
7c23b892 726 tp->size = 0;
7d08c73e 727 tp->cptse = 0;
7c23b892
AZ
728}
729
730static uint32_t
62ecbd35 731txdesc_writeback(E1000State *s, dma_addr_t base, struct e1000_tx_desc *dp)
7c23b892 732{
b08340d5 733 PCIDevice *d = PCI_DEVICE(s);
7c23b892
AZ
734 uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data);
735
736 if (!(txd_lower & (E1000_TXD_CMD_RS|E1000_TXD_CMD_RPS)))
737 return 0;
738 txd_upper = (le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD) &
739 ~(E1000_TXD_STAT_EC | E1000_TXD_STAT_LC | E1000_TXD_STAT_TU);
740 dp->upper.data = cpu_to_le32(txd_upper);
b08340d5 741 pci_dma_write(d, base + ((char *)&dp->upper - (char *)dp),
00c3a05b 742 &dp->upper, sizeof(dp->upper));
7c23b892
AZ
743 return E1000_ICR_TXDW;
744}
745
d17161f6
KW
746static uint64_t tx_desc_base(E1000State *s)
747{
748 uint64_t bah = s->mac_reg[TDBAH];
749 uint64_t bal = s->mac_reg[TDBAL] & ~0xf;
750
751 return (bah << 32) + bal;
752}
753
7c23b892
AZ
754static void
755start_xmit(E1000State *s)
756{
b08340d5 757 PCIDevice *d = PCI_DEVICE(s);
62ecbd35 758 dma_addr_t base;
7c23b892
AZ
759 struct e1000_tx_desc desc;
760 uint32_t tdh_start = s->mac_reg[TDH], cause = E1000_ICS_TXQE;
761
762 if (!(s->mac_reg[TCTL] & E1000_TCTL_EN)) {
763 DBGOUT(TX, "tx disabled\n");
764 return;
765 }
766
25ddb946
JM
767 if (s->tx.busy) {
768 return;
769 }
770 s->tx.busy = true;
771
7c23b892 772 while (s->mac_reg[TDH] != s->mac_reg[TDT]) {
d17161f6 773 base = tx_desc_base(s) +
7c23b892 774 sizeof(struct e1000_tx_desc) * s->mac_reg[TDH];
b08340d5 775 pci_dma_read(d, base, &desc, sizeof(desc));
7c23b892
AZ
776
777 DBGOUT(TX, "index %d: %p : %x %x\n", s->mac_reg[TDH],
6106075b 778 (void *)(intptr_t)desc.buffer_addr, desc.lower.data,
7c23b892
AZ
779 desc.upper.data);
780
781 process_tx_desc(s, &desc);
62ecbd35 782 cause |= txdesc_writeback(s, base, &desc);
7c23b892
AZ
783
784 if (++s->mac_reg[TDH] * sizeof(desc) >= s->mac_reg[TDLEN])
785 s->mac_reg[TDH] = 0;
786 /*
787 * the following could happen only if guest sw assigns
788 * bogus values to TDT/TDLEN.
789 * there's nothing too intelligent we could do about this.
790 */
dd793a74
LE
791 if (s->mac_reg[TDH] == tdh_start ||
792 tdh_start >= s->mac_reg[TDLEN] / sizeof(desc)) {
7c23b892
AZ
793 DBGOUT(TXERR, "TDH wraparound @%x, TDT %x, TDLEN %x\n",
794 tdh_start, s->mac_reg[TDT], s->mac_reg[TDLEN]);
795 break;
796 }
797 }
25ddb946 798 s->tx.busy = false;
7c23b892
AZ
799 set_ics(s, 0, cause);
800}
801
802static int
803receive_filter(E1000State *s, const uint8_t *buf, int size)
804{
093454e2 805 uint32_t rctl = s->mac_reg[RCTL];
4aeea330 806 int isbcast = !memcmp(buf, bcast, sizeof bcast), ismcast = (buf[0] & 1);
7c23b892 807
093454e2
DF
808 if (e1000x_is_vlan_packet(buf, le16_to_cpu(s->mac_reg[VET])) &&
809 e1000x_vlan_rx_filter_enabled(s->mac_reg)) {
14e60aae
PM
810 uint16_t vid = lduw_be_p(buf + 14);
811 uint32_t vfta = ldl_le_p((uint32_t*)(s->mac_reg + VFTA) +
812 ((vid >> 5) & 0x7f));
8f2e8d1f
AL
813 if ((vfta & (1 << (vid & 0x1f))) == 0)
814 return 0;
815 }
816
4aeea330 817 if (!isbcast && !ismcast && (rctl & E1000_RCTL_UPE)) { /* promiscuous ucast */
7c23b892 818 return 1;
4aeea330 819 }
7c23b892 820
4aeea330 821 if (ismcast && (rctl & E1000_RCTL_MPE)) { /* promiscuous mcast */
093454e2 822 e1000x_inc_reg_if_not_full(s->mac_reg, MPRC);
7c23b892 823 return 1;
4aeea330 824 }
7c23b892 825
4aeea330 826 if (isbcast && (rctl & E1000_RCTL_BAM)) { /* broadcast enabled */
093454e2 827 e1000x_inc_reg_if_not_full(s->mac_reg, BPRC);
7c23b892 828 return 1;
3b274301 829 }
7c23b892 830
093454e2 831 return e1000x_rx_group_filter(s->mac_reg, buf);
7c23b892
AZ
832}
833
99ed7e30 834static void
4e68f7a0 835e1000_set_link_status(NetClientState *nc)
99ed7e30 836{
cc1f0f45 837 E1000State *s = qemu_get_nic_opaque(nc);
99ed7e30
AL
838 uint32_t old_status = s->mac_reg[STATUS];
839
d4044c2a 840 if (nc->link_down) {
093454e2 841 e1000x_update_regs_on_link_down(s->mac_reg, s->phy_reg);
d4044c2a 842 } else {
d7a41552 843 if (have_autoneg(s) &&
6a2acedb 844 !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
093454e2 845 e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer);
6a2acedb
GS
846 } else {
847 e1000_link_up(s);
848 }
d4044c2a 849 }
99ed7e30
AL
850
851 if (s->mac_reg[STATUS] != old_status)
852 set_ics(s, 0, E1000_ICR_LSC);
853}
854
322fd48a
MT
855static bool e1000_has_rxbufs(E1000State *s, size_t total_size)
856{
857 int bufs;
858 /* Fast-path short packets */
859 if (total_size <= s->rxbuf_size) {
e5b8b0d4 860 return s->mac_reg[RDH] != s->mac_reg[RDT];
322fd48a
MT
861 }
862 if (s->mac_reg[RDH] < s->mac_reg[RDT]) {
863 bufs = s->mac_reg[RDT] - s->mac_reg[RDH];
e5b8b0d4 864 } else if (s->mac_reg[RDH] > s->mac_reg[RDT]) {
322fd48a
MT
865 bufs = s->mac_reg[RDLEN] / sizeof(struct e1000_rx_desc) +
866 s->mac_reg[RDT] - s->mac_reg[RDH];
867 } else {
868 return false;
869 }
870 return total_size <= bufs * s->rxbuf_size;
871}
872
b8c4b67e 873static bool
4e68f7a0 874e1000_can_receive(NetClientState *nc)
6cdfab28 875{
cc1f0f45 876 E1000State *s = qemu_get_nic_opaque(nc);
6cdfab28 877
093454e2 878 return e1000x_rx_ready(&s->parent_obj, s->mac_reg) &&
157628d0 879 e1000_has_rxbufs(s, 1) && !timer_pending(s->flush_queue_timer);
6cdfab28
MT
880}
881
d17161f6
KW
882static uint64_t rx_desc_base(E1000State *s)
883{
884 uint64_t bah = s->mac_reg[RDBAH];
885 uint64_t bal = s->mac_reg[RDBAL] & ~0xf;
886
887 return (bah << 32) + bal;
888}
889
1001cf45
JW
890static void
891e1000_receiver_overrun(E1000State *s, size_t size)
892{
893 trace_e1000_receiver_overrun(size, s->mac_reg[RDH], s->mac_reg[RDT]);
894 e1000x_inc_reg_if_not_full(s->mac_reg, RNBC);
895 e1000x_inc_reg_if_not_full(s->mac_reg, MPC);
896 set_ics(s, 0, E1000_ICS_RXO);
897}
898
4f1c942b 899static ssize_t
97410dde 900e1000_receive_iov(NetClientState *nc, const struct iovec *iov, int iovcnt)
7c23b892 901{
cc1f0f45 902 E1000State *s = qemu_get_nic_opaque(nc);
b08340d5 903 PCIDevice *d = PCI_DEVICE(s);
7c23b892 904 struct e1000_rx_desc desc;
62ecbd35 905 dma_addr_t base;
7c23b892
AZ
906 unsigned int n, rdt;
907 uint32_t rdh_start;
8f2e8d1f 908 uint16_t vlan_special = 0;
97410dde 909 uint8_t vlan_status = 0;
78aeb23e 910 uint8_t min_buf[MIN_BUF_SIZE];
97410dde
VM
911 struct iovec min_iov;
912 uint8_t *filter_buf = iov->iov_base;
913 size_t size = iov_size(iov, iovcnt);
914 size_t iov_ofs = 0;
b19487e2
MT
915 size_t desc_offset;
916 size_t desc_size;
917 size_t total_size;
ddcb73b7 918
093454e2 919 if (!e1000x_hw_rx_enabled(s->mac_reg)) {
4f1c942b 920 return -1;
ddcb73b7 921 }
7c23b892 922
157628d0
YCL
923 if (timer_pending(s->flush_queue_timer)) {
924 return 0;
925 }
926
78aeb23e
SH
927 /* Pad to minimum Ethernet frame length */
928 if (size < sizeof(min_buf)) {
97410dde 929 iov_to_buf(iov, iovcnt, 0, min_buf, size);
78aeb23e 930 memset(&min_buf[size], 0, sizeof(min_buf) - size);
97410dde
VM
931 min_iov.iov_base = filter_buf = min_buf;
932 min_iov.iov_len = size = sizeof(min_buf);
933 iovcnt = 1;
934 iov = &min_iov;
935 } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) {
936 /* This is very unlikely, but may happen. */
937 iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN);
938 filter_buf = min_buf;
78aeb23e
SH
939 }
940
b0d9ffcd 941 /* Discard oversized packets if !LPE and !SBP. */
093454e2 942 if (e1000x_is_oversized(s->mac_reg, size)) {
b0d9ffcd
MC
943 return size;
944 }
945
97410dde 946 if (!receive_filter(s, filter_buf, size)) {
4f1c942b 947 return size;
97410dde 948 }
7c23b892 949
093454e2
DF
950 if (e1000x_vlan_enabled(s->mac_reg) &&
951 e1000x_is_vlan_packet(filter_buf, le16_to_cpu(s->mac_reg[VET]))) {
14e60aae 952 vlan_special = cpu_to_le16(lduw_be_p(filter_buf + 14));
97410dde
VM
953 iov_ofs = 4;
954 if (filter_buf == iov->iov_base) {
955 memmove(filter_buf + 4, filter_buf, 12);
956 } else {
957 iov_from_buf(iov, iovcnt, 4, filter_buf, 12);
958 while (iov->iov_len <= iov_ofs) {
959 iov_ofs -= iov->iov_len;
960 iov++;
961 }
962 }
8f2e8d1f 963 vlan_status = E1000_RXD_STAT_VP;
8f2e8d1f
AL
964 size -= 4;
965 }
966
7c23b892 967 rdh_start = s->mac_reg[RDH];
b19487e2 968 desc_offset = 0;
093454e2 969 total_size = size + e1000x_fcs_len(s->mac_reg);
322fd48a 970 if (!e1000_has_rxbufs(s, total_size)) {
1001cf45
JW
971 e1000_receiver_overrun(s, total_size);
972 return -1;
322fd48a 973 }
7c23b892 974 do {
b19487e2
MT
975 desc_size = total_size - desc_offset;
976 if (desc_size > s->rxbuf_size) {
977 desc_size = s->rxbuf_size;
978 }
d17161f6 979 base = rx_desc_base(s) + sizeof(desc) * s->mac_reg[RDH];
b08340d5 980 pci_dma_read(d, base, &desc, sizeof(desc));
8f2e8d1f 981 desc.special = vlan_special;
034d00d4 982 desc.status &= ~E1000_RXD_STAT_DD;
7c23b892 983 if (desc.buffer_addr) {
b19487e2 984 if (desc_offset < size) {
97410dde
VM
985 size_t iov_copy;
986 hwaddr ba = le64_to_cpu(desc.buffer_addr);
b19487e2
MT
987 size_t copy_size = size - desc_offset;
988 if (copy_size > s->rxbuf_size) {
989 copy_size = s->rxbuf_size;
990 }
97410dde
VM
991 do {
992 iov_copy = MIN(copy_size, iov->iov_len - iov_ofs);
993 pci_dma_write(d, ba, iov->iov_base + iov_ofs, iov_copy);
994 copy_size -= iov_copy;
995 ba += iov_copy;
996 iov_ofs += iov_copy;
997 if (iov_ofs == iov->iov_len) {
998 iov++;
999 iov_ofs = 0;
1000 }
1001 } while (copy_size);
b19487e2
MT
1002 }
1003 desc_offset += desc_size;
ee912ccf 1004 desc.length = cpu_to_le16(desc_size);
b19487e2 1005 if (desc_offset >= total_size) {
b19487e2
MT
1006 desc.status |= E1000_RXD_STAT_EOP | E1000_RXD_STAT_IXSM;
1007 } else {
ee912ccf
MT
1008 /* Guest zeroing out status is not a hardware requirement.
1009 Clear EOP in case guest didn't do it. */
1010 desc.status &= ~E1000_RXD_STAT_EOP;
b19487e2 1011 }
43ad7e3e 1012 } else { // as per intel docs; skip descriptors with null buf addr
7c23b892 1013 DBGOUT(RX, "Null RX descriptor!!\n");
43ad7e3e 1014 }
b08340d5 1015 pci_dma_write(d, base, &desc, sizeof(desc));
034d00d4
DH
1016 desc.status |= (vlan_status | E1000_RXD_STAT_DD);
1017 pci_dma_write(d, base + offsetof(struct e1000_rx_desc, status),
1018 &desc.status, sizeof(desc.status));
7c23b892
AZ
1019
1020 if (++s->mac_reg[RDH] * sizeof(desc) >= s->mac_reg[RDLEN])
1021 s->mac_reg[RDH] = 0;
7c23b892 1022 /* see comment in start_xmit; same here */
dd793a74
LE
1023 if (s->mac_reg[RDH] == rdh_start ||
1024 rdh_start >= s->mac_reg[RDLEN] / sizeof(desc)) {
7c23b892
AZ
1025 DBGOUT(RXERR, "RDH wraparound @%x, RDT %x, RDLEN %x\n",
1026 rdh_start, s->mac_reg[RDT], s->mac_reg[RDLEN]);
1001cf45 1027 e1000_receiver_overrun(s, total_size);
4f1c942b 1028 return -1;
7c23b892 1029 }
b19487e2 1030 } while (desc_offset < total_size);
7c23b892 1031
093454e2 1032 e1000x_update_rx_total_stats(s->mac_reg, size, total_size);
7c23b892
AZ
1033
1034 n = E1000_ICS_RXT0;
1035 if ((rdt = s->mac_reg[RDT]) < s->mac_reg[RDH])
1036 rdt += s->mac_reg[RDLEN] / sizeof(desc);
bf16cc8f
AL
1037 if (((rdt - s->mac_reg[RDH]) * sizeof(desc)) <= s->mac_reg[RDLEN] >>
1038 s->rxbuf_min_shift)
7c23b892
AZ
1039 n |= E1000_ICS_RXDMT0;
1040
1041 set_ics(s, 0, n);
4f1c942b
MM
1042
1043 return size;
7c23b892
AZ
1044}
1045
97410dde
VM
1046static ssize_t
1047e1000_receive(NetClientState *nc, const uint8_t *buf, size_t size)
1048{
1049 const struct iovec iov = {
1050 .iov_base = (uint8_t *)buf,
1051 .iov_len = size
1052 };
1053
1054 return e1000_receive_iov(nc, &iov, 1);
1055}
1056
7c23b892
AZ
1057static uint32_t
1058mac_readreg(E1000State *s, int index)
1059{
1060 return s->mac_reg[index];
1061}
1062
72ea771c
LB
1063static uint32_t
1064mac_low4_read(E1000State *s, int index)
1065{
1066 return s->mac_reg[index] & 0xf;
1067}
1068
1069static uint32_t
1070mac_low11_read(E1000State *s, int index)
1071{
1072 return s->mac_reg[index] & 0x7ff;
1073}
1074
1075static uint32_t
1076mac_low13_read(E1000State *s, int index)
1077{
1078 return s->mac_reg[index] & 0x1fff;
1079}
1080
1081static uint32_t
1082mac_low16_read(E1000State *s, int index)
1083{
1084 return s->mac_reg[index] & 0xffff;
1085}
1086
7c23b892
AZ
1087static uint32_t
1088mac_icr_read(E1000State *s, int index)
1089{
1090 uint32_t ret = s->mac_reg[ICR];
1091
1092 DBGOUT(INTERRUPT, "ICR read: %x\n", ret);
1093 set_interrupt_cause(s, 0, 0);
1094 return ret;
1095}
1096
1097static uint32_t
1098mac_read_clr4(E1000State *s, int index)
1099{
1100 uint32_t ret = s->mac_reg[index];
1101
1102 s->mac_reg[index] = 0;
1103 return ret;
1104}
1105
1106static uint32_t
1107mac_read_clr8(E1000State *s, int index)
1108{
1109 uint32_t ret = s->mac_reg[index];
1110
1111 s->mac_reg[index] = 0;
1112 s->mac_reg[index-1] = 0;
1113 return ret;
1114}
1115
1116static void
1117mac_writereg(E1000State *s, int index, uint32_t val)
1118{
7c36507c
AK
1119 uint32_t macaddr[2];
1120
7c23b892 1121 s->mac_reg[index] = val;
7c36507c 1122
90d131fb 1123 if (index == RA + 1) {
7c36507c
AK
1124 macaddr[0] = cpu_to_le32(s->mac_reg[RA]);
1125 macaddr[1] = cpu_to_le32(s->mac_reg[RA + 1]);
1126 qemu_format_nic_info_str(qemu_get_queue(s->nic), (uint8_t *)macaddr);
1127 }
7c23b892
AZ
1128}
1129
1130static void
1131set_rdt(E1000State *s, int index, uint32_t val)
1132{
7c23b892 1133 s->mac_reg[index] = val & 0xffff;
e8b4c680 1134 if (e1000_has_rxbufs(s, 1)) {
b356f76d 1135 qemu_flush_queued_packets(qemu_get_queue(s->nic));
e8b4c680 1136 }
7c23b892
AZ
1137}
1138
1139static void
1140set_16bit(E1000State *s, int index, uint32_t val)
1141{
1142 s->mac_reg[index] = val & 0xffff;
1143}
1144
1145static void
1146set_dlen(E1000State *s, int index, uint32_t val)
1147{
1148 s->mac_reg[index] = val & 0xfff80;
1149}
1150
1151static void
1152set_tctl(E1000State *s, int index, uint32_t val)
1153{
1154 s->mac_reg[index] = val;
1155 s->mac_reg[TDT] &= 0xffff;
1156 start_xmit(s);
1157}
1158
1159static void
1160set_icr(E1000State *s, int index, uint32_t val)
1161{
1162 DBGOUT(INTERRUPT, "set_icr %x\n", val);
1163 set_interrupt_cause(s, 0, s->mac_reg[ICR] & ~val);
1164}
1165
1166static void
1167set_imc(E1000State *s, int index, uint32_t val)
1168{
1169 s->mac_reg[IMS] &= ~val;
1170 set_ics(s, 0, 0);
1171}
1172
1173static void
1174set_ims(E1000State *s, int index, uint32_t val)
1175{
1176 s->mac_reg[IMS] |= val;
1177 set_ics(s, 0, 0);
1178}
1179
20f3e863 1180#define getreg(x) [x] = mac_readreg
3b6b3a27 1181typedef uint32_t (*readops)(E1000State *, int);
da5cf9a4 1182static const readops macreg_readops[] = {
20f3e863
LB
1183 getreg(PBA), getreg(RCTL), getreg(TDH), getreg(TXDCTL),
1184 getreg(WUFC), getreg(TDT), getreg(CTRL), getreg(LEDCTL),
1185 getreg(MANC), getreg(MDIC), getreg(SWSM), getreg(STATUS),
1186 getreg(TORL), getreg(TOTL), getreg(IMS), getreg(TCTL),
1187 getreg(RDH), getreg(RDT), getreg(VET), getreg(ICS),
1188 getreg(TDBAL), getreg(TDBAH), getreg(RDBAH), getreg(RDBAL),
1189 getreg(TDLEN), getreg(RDLEN), getreg(RDTR), getreg(RADV),
72ea771c
LB
1190 getreg(TADV), getreg(ITR), getreg(FCRUC), getreg(IPAV),
1191 getreg(WUC), getreg(WUS), getreg(SCC), getreg(ECOL),
1192 getreg(MCC), getreg(LATECOL), getreg(COLC), getreg(DC),
757704f1 1193 getreg(TNCRS), getreg(SEQEC), getreg(CEXTERR), getreg(RLEC),
72ea771c
LB
1194 getreg(XONRXC), getreg(XONTXC), getreg(XOFFRXC), getreg(XOFFTXC),
1195 getreg(RFC), getreg(RJC), getreg(RNBC), getreg(TSCTFC),
3b274301
LB
1196 getreg(MGTPRC), getreg(MGTPDC), getreg(MGTPTC), getreg(GORCL),
1197 getreg(GOTCL),
20f3e863
LB
1198
1199 [TOTH] = mac_read_clr8, [TORH] = mac_read_clr8,
3b274301
LB
1200 [GOTCH] = mac_read_clr8, [GORCH] = mac_read_clr8,
1201 [PRC64] = mac_read_clr4, [PRC127] = mac_read_clr4,
1202 [PRC255] = mac_read_clr4, [PRC511] = mac_read_clr4,
1203 [PRC1023] = mac_read_clr4, [PRC1522] = mac_read_clr4,
1204 [PTC64] = mac_read_clr4, [PTC127] = mac_read_clr4,
1205 [PTC255] = mac_read_clr4, [PTC511] = mac_read_clr4,
1206 [PTC1023] = mac_read_clr4, [PTC1522] = mac_read_clr4,
20f3e863
LB
1207 [GPRC] = mac_read_clr4, [GPTC] = mac_read_clr4,
1208 [TPT] = mac_read_clr4, [TPR] = mac_read_clr4,
3b274301
LB
1209 [RUC] = mac_read_clr4, [ROC] = mac_read_clr4,
1210 [BPRC] = mac_read_clr4, [MPRC] = mac_read_clr4,
1211 [TSCTC] = mac_read_clr4, [BPTC] = mac_read_clr4,
1212 [MPTC] = mac_read_clr4,
20f3e863
LB
1213 [ICR] = mac_icr_read, [EECD] = get_eecd,
1214 [EERD] = flash_eerd_read,
72ea771c
LB
1215 [RDFH] = mac_low13_read, [RDFT] = mac_low13_read,
1216 [RDFHS] = mac_low13_read, [RDFTS] = mac_low13_read,
1217 [RDFPC] = mac_low13_read,
1218 [TDFH] = mac_low11_read, [TDFT] = mac_low11_read,
1219 [TDFHS] = mac_low13_read, [TDFTS] = mac_low13_read,
1220 [TDFPC] = mac_low13_read,
1221 [AIT] = mac_low16_read,
20f3e863
LB
1222
1223 [CRCERRS ... MPC] = &mac_readreg,
72ea771c
LB
1224 [IP6AT ... IP6AT+3] = &mac_readreg, [IP4AT ... IP4AT+6] = &mac_readreg,
1225 [FFLT ... FFLT+6] = &mac_low11_read,
20f3e863 1226 [RA ... RA+31] = &mac_readreg,
72ea771c 1227 [WUPM ... WUPM+31] = &mac_readreg,
20f3e863 1228 [MTA ... MTA+127] = &mac_readreg,
8f2e8d1f 1229 [VFTA ... VFTA+127] = &mac_readreg,
72ea771c
LB
1230 [FFMT ... FFMT+254] = &mac_low4_read,
1231 [FFVT ... FFVT+254] = &mac_readreg,
1232 [PBM ... PBM+16383] = &mac_readreg,
7c23b892 1233};
b1503cda 1234enum { NREADOPS = ARRAY_SIZE(macreg_readops) };
7c23b892 1235
20f3e863 1236#define putreg(x) [x] = mac_writereg
3b6b3a27 1237typedef void (*writeops)(E1000State *, int, uint32_t);
da5cf9a4 1238static const writeops macreg_writeops[] = {
20f3e863
LB
1239 putreg(PBA), putreg(EERD), putreg(SWSM), putreg(WUFC),
1240 putreg(TDBAL), putreg(TDBAH), putreg(TXDCTL), putreg(RDBAH),
72ea771c
LB
1241 putreg(RDBAL), putreg(LEDCTL), putreg(VET), putreg(FCRUC),
1242 putreg(TDFH), putreg(TDFT), putreg(TDFHS), putreg(TDFTS),
1243 putreg(TDFPC), putreg(RDFH), putreg(RDFT), putreg(RDFHS),
1244 putreg(RDFTS), putreg(RDFPC), putreg(IPAV), putreg(WUC),
1245 putreg(WUS), putreg(AIT),
20f3e863
LB
1246
1247 [TDLEN] = set_dlen, [RDLEN] = set_dlen, [TCTL] = set_tctl,
1248 [TDT] = set_tctl, [MDIC] = set_mdic, [ICS] = set_ics,
1249 [TDH] = set_16bit, [RDH] = set_16bit, [RDT] = set_rdt,
1250 [IMC] = set_imc, [IMS] = set_ims, [ICR] = set_icr,
1251 [EECD] = set_eecd, [RCTL] = set_rx_control, [CTRL] = set_ctrl,
1252 [RDTR] = set_16bit, [RADV] = set_16bit, [TADV] = set_16bit,
1253 [ITR] = set_16bit,
1254
72ea771c
LB
1255 [IP6AT ... IP6AT+3] = &mac_writereg, [IP4AT ... IP4AT+6] = &mac_writereg,
1256 [FFLT ... FFLT+6] = &mac_writereg,
20f3e863 1257 [RA ... RA+31] = &mac_writereg,
72ea771c 1258 [WUPM ... WUPM+31] = &mac_writereg,
20f3e863 1259 [MTA ... MTA+127] = &mac_writereg,
8f2e8d1f 1260 [VFTA ... VFTA+127] = &mac_writereg,
72ea771c
LB
1261 [FFMT ... FFMT+254] = &mac_writereg, [FFVT ... FFVT+254] = &mac_writereg,
1262 [PBM ... PBM+16383] = &mac_writereg,
7c23b892 1263};
b9d03e35 1264
b1503cda 1265enum { NWRITEOPS = ARRAY_SIZE(macreg_writeops) };
7c23b892 1266
bc0f0674
LB
1267enum { MAC_ACCESS_PARTIAL = 1, MAC_ACCESS_FLAG_NEEDED = 2 };
1268
1269#define markflag(x) ((E1000_FLAG_##x << 2) | MAC_ACCESS_FLAG_NEEDED)
1270/* In the array below the meaning of the bits is: [f|f|f|f|f|f|n|p]
1271 * f - flag bits (up to 6 possible flags)
1272 * n - flag needed
1273 * p - partially implenented */
1274static const uint8_t mac_reg_access[0x8000] = {
1275 [RDTR] = markflag(MIT), [TADV] = markflag(MIT),
1276 [RADV] = markflag(MIT), [ITR] = markflag(MIT),
72ea771c
LB
1277
1278 [IPAV] = markflag(MAC), [WUC] = markflag(MAC),
1279 [IP6AT] = markflag(MAC), [IP4AT] = markflag(MAC),
1280 [FFVT] = markflag(MAC), [WUPM] = markflag(MAC),
1281 [ECOL] = markflag(MAC), [MCC] = markflag(MAC),
1282 [DC] = markflag(MAC), [TNCRS] = markflag(MAC),
1283 [RLEC] = markflag(MAC), [XONRXC] = markflag(MAC),
1284 [XOFFTXC] = markflag(MAC), [RFC] = markflag(MAC),
1285 [TSCTFC] = markflag(MAC), [MGTPRC] = markflag(MAC),
1286 [WUS] = markflag(MAC), [AIT] = markflag(MAC),
1287 [FFLT] = markflag(MAC), [FFMT] = markflag(MAC),
1288 [SCC] = markflag(MAC), [FCRUC] = markflag(MAC),
1289 [LATECOL] = markflag(MAC), [COLC] = markflag(MAC),
757704f1 1290 [SEQEC] = markflag(MAC), [CEXTERR] = markflag(MAC),
72ea771c
LB
1291 [XONTXC] = markflag(MAC), [XOFFRXC] = markflag(MAC),
1292 [RJC] = markflag(MAC), [RNBC] = markflag(MAC),
1293 [MGTPDC] = markflag(MAC), [MGTPTC] = markflag(MAC),
3b274301
LB
1294 [RUC] = markflag(MAC), [ROC] = markflag(MAC),
1295 [GORCL] = markflag(MAC), [GORCH] = markflag(MAC),
1296 [GOTCL] = markflag(MAC), [GOTCH] = markflag(MAC),
1297 [BPRC] = markflag(MAC), [MPRC] = markflag(MAC),
1298 [TSCTC] = markflag(MAC), [PRC64] = markflag(MAC),
1299 [PRC127] = markflag(MAC), [PRC255] = markflag(MAC),
1300 [PRC511] = markflag(MAC), [PRC1023] = markflag(MAC),
1301 [PRC1522] = markflag(MAC), [PTC64] = markflag(MAC),
1302 [PTC127] = markflag(MAC), [PTC255] = markflag(MAC),
1303 [PTC511] = markflag(MAC), [PTC1023] = markflag(MAC),
1304 [PTC1522] = markflag(MAC), [MPTC] = markflag(MAC),
1305 [BPTC] = markflag(MAC),
72ea771c
LB
1306
1307 [TDFH] = markflag(MAC) | MAC_ACCESS_PARTIAL,
1308 [TDFT] = markflag(MAC) | MAC_ACCESS_PARTIAL,
1309 [TDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
1310 [TDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
1311 [TDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL,
1312 [RDFH] = markflag(MAC) | MAC_ACCESS_PARTIAL,
1313 [RDFT] = markflag(MAC) | MAC_ACCESS_PARTIAL,
1314 [RDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
1315 [RDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
1316 [RDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL,
1317 [PBM] = markflag(MAC) | MAC_ACCESS_PARTIAL,
bc0f0674
LB
1318};
1319
7c23b892 1320static void
a8170e5e 1321e1000_mmio_write(void *opaque, hwaddr addr, uint64_t val,
ad00a9b9 1322 unsigned size)
7c23b892
AZ
1323{
1324 E1000State *s = opaque;
8da3ff18 1325 unsigned int index = (addr & 0x1ffff) >> 2;
7c23b892 1326
43ad7e3e 1327 if (index < NWRITEOPS && macreg_writeops[index]) {
bc0f0674
LB
1328 if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED)
1329 || (s->compat_flags & (mac_reg_access[index] >> 2))) {
1330 if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
1331 DBGOUT(GENERAL, "Writing to register at offset: 0x%08x. "
1332 "It is not fully implemented.\n", index<<2);
1333 }
1334 macreg_writeops[index](s, index, val);
1335 } else { /* "flag needed" bit is set, but the flag is not active */
1336 DBGOUT(MMIO, "MMIO write attempt to disabled reg. addr=0x%08x\n",
1337 index<<2);
1338 }
43ad7e3e 1339 } else if (index < NREADOPS && macreg_readops[index]) {
bc0f0674
LB
1340 DBGOUT(MMIO, "e1000_mmio_writel RO %x: 0x%04"PRIx64"\n",
1341 index<<2, val);
43ad7e3e 1342 } else {
ad00a9b9 1343 DBGOUT(UNKNOWN, "MMIO unknown write addr=0x%08x,val=0x%08"PRIx64"\n",
7c23b892 1344 index<<2, val);
43ad7e3e 1345 }
7c23b892
AZ
1346}
1347
ad00a9b9 1348static uint64_t
a8170e5e 1349e1000_mmio_read(void *opaque, hwaddr addr, unsigned size)
7c23b892
AZ
1350{
1351 E1000State *s = opaque;
8da3ff18 1352 unsigned int index = (addr & 0x1ffff) >> 2;
7c23b892 1353
bc0f0674
LB
1354 if (index < NREADOPS && macreg_readops[index]) {
1355 if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED)
1356 || (s->compat_flags & (mac_reg_access[index] >> 2))) {
1357 if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
1358 DBGOUT(GENERAL, "Reading register at offset: 0x%08x. "
1359 "It is not fully implemented.\n", index<<2);
1360 }
1361 return macreg_readops[index](s, index);
1362 } else { /* "flag needed" bit is set, but the flag is not active */
1363 DBGOUT(MMIO, "MMIO read attempt of disabled reg. addr=0x%08x\n",
1364 index<<2);
1365 }
1366 } else {
1367 DBGOUT(UNKNOWN, "MMIO unknown read addr=0x%08x\n", index<<2);
6b59fc74 1368 }
7c23b892
AZ
1369 return 0;
1370}
1371
ad00a9b9
AK
1372static const MemoryRegionOps e1000_mmio_ops = {
1373 .read = e1000_mmio_read,
1374 .write = e1000_mmio_write,
1375 .endianness = DEVICE_LITTLE_ENDIAN,
1376 .impl = {
1377 .min_access_size = 4,
1378 .max_access_size = 4,
1379 },
1380};
1381
a8170e5e 1382static uint64_t e1000_io_read(void *opaque, hwaddr addr,
ad00a9b9 1383 unsigned size)
7c23b892 1384{
ad00a9b9
AK
1385 E1000State *s = opaque;
1386
1387 (void)s;
1388 return 0;
7c23b892
AZ
1389}
1390
a8170e5e 1391static void e1000_io_write(void *opaque, hwaddr addr,
ad00a9b9 1392 uint64_t val, unsigned size)
7c23b892 1393{
ad00a9b9
AK
1394 E1000State *s = opaque;
1395
1396 (void)s;
7c23b892
AZ
1397}
1398
ad00a9b9
AK
1399static const MemoryRegionOps e1000_io_ops = {
1400 .read = e1000_io_read,
1401 .write = e1000_io_write,
1402 .endianness = DEVICE_LITTLE_ENDIAN,
1403};
1404
e482dc3e 1405static bool is_version_1(void *opaque, int version_id)
7c23b892 1406{
e482dc3e 1407 return version_id == 1;
7c23b892
AZ
1408}
1409
44b1ff31 1410static int e1000_pre_save(void *opaque)
ddcb73b7
MT
1411{
1412 E1000State *s = opaque;
1413 NetClientState *nc = qemu_get_queue(s->nic);
2af234e6 1414
ddcb73b7 1415 /*
6a2acedb
GS
1416 * If link is down and auto-negotiation is supported and ongoing,
1417 * complete auto-negotiation immediately. This allows us to look
1418 * at MII_SR_AUTONEG_COMPLETE to infer link status on load.
ddcb73b7 1419 */
d7a41552
GS
1420 if (nc->link_down && have_autoneg(s)) {
1421 s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
ddcb73b7 1422 }
44b1ff31 1423
ff214d42
DDAG
1424 /* Decide which set of props to migrate in the main structure */
1425 if (chkflag(TSO) || !s->use_tso_for_migration) {
1426 /* Either we're migrating with the extra subsection, in which
1427 * case the mig_props is always 'props' OR
1428 * we've not got the subsection, but 'props' was the last
1429 * updated.
1430 */
1431 s->mig_props = s->tx.props;
1432 } else {
1433 /* We're not using the subsection, and 'tso_props' was
1434 * the last updated.
1435 */
1436 s->mig_props = s->tx.tso_props;
1437 }
44b1ff31 1438 return 0;
ddcb73b7
MT
1439}
1440
e4b82364
AK
1441static int e1000_post_load(void *opaque, int version_id)
1442{
1443 E1000State *s = opaque;
b356f76d 1444 NetClientState *nc = qemu_get_queue(s->nic);
e4b82364 1445
bc0f0674 1446 if (!chkflag(MIT)) {
e9845f09
VM
1447 s->mac_reg[ITR] = s->mac_reg[RDTR] = s->mac_reg[RADV] =
1448 s->mac_reg[TADV] = 0;
1449 s->mit_irq_level = false;
1450 }
1451 s->mit_ide = 0;
f46efa9b
JW
1452 s->mit_timer_on = true;
1453 timer_mod(s->mit_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 1);
e9845f09 1454
e4b82364 1455 /* nc.link_down can't be migrated, so infer link_down according
ddcb73b7
MT
1456 * to link status bit in mac_reg[STATUS].
1457 * Alternatively, restart link negotiation if it was in progress. */
b356f76d 1458 nc->link_down = (s->mac_reg[STATUS] & E1000_STATUS_LU) == 0;
2af234e6 1459
d7a41552 1460 if (have_autoneg(s) &&
ddcb73b7
MT
1461 !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
1462 nc->link_down = false;
d7a41552
GS
1463 timer_mod(s->autoneg_timer,
1464 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
ddcb73b7 1465 }
e4b82364 1466
59354484 1467 s->tx.props = s->mig_props;
3c4053c5
DDAG
1468 if (!s->received_tx_tso) {
1469 /* We received only one set of offload data (tx.props)
1470 * and haven't got tx.tso_props. The best we can do
1471 * is dupe the data.
1472 */
59354484 1473 s->tx.tso_props = s->mig_props;
3c4053c5
DDAG
1474 }
1475 return 0;
1476}
1477
1478static int e1000_tx_tso_post_load(void *opaque, int version_id)
1479{
1480 E1000State *s = opaque;
1481 s->received_tx_tso = true;
e4b82364
AK
1482 return 0;
1483}
1484
e9845f09
VM
1485static bool e1000_mit_state_needed(void *opaque)
1486{
1487 E1000State *s = opaque;
1488
bc0f0674 1489 return chkflag(MIT);
e9845f09
VM
1490}
1491
9e117734
LB
1492static bool e1000_full_mac_needed(void *opaque)
1493{
1494 E1000State *s = opaque;
1495
bc0f0674 1496 return chkflag(MAC);
9e117734
LB
1497}
1498
46f2a9ec
DDAG
1499static bool e1000_tso_state_needed(void *opaque)
1500{
1501 E1000State *s = opaque;
1502
1503 return chkflag(TSO);
1504}
1505
e9845f09
VM
1506static const VMStateDescription vmstate_e1000_mit_state = {
1507 .name = "e1000/mit_state",
1508 .version_id = 1,
1509 .minimum_version_id = 1,
5cd8cada 1510 .needed = e1000_mit_state_needed,
d49805ae 1511 .fields = (VMStateField[]) {
e9845f09
VM
1512 VMSTATE_UINT32(mac_reg[RDTR], E1000State),
1513 VMSTATE_UINT32(mac_reg[RADV], E1000State),
1514 VMSTATE_UINT32(mac_reg[TADV], E1000State),
1515 VMSTATE_UINT32(mac_reg[ITR], E1000State),
1516 VMSTATE_BOOL(mit_irq_level, E1000State),
1517 VMSTATE_END_OF_LIST()
1518 }
1519};
1520
9e117734
LB
1521static const VMStateDescription vmstate_e1000_full_mac_state = {
1522 .name = "e1000/full_mac_state",
1523 .version_id = 1,
1524 .minimum_version_id = 1,
1525 .needed = e1000_full_mac_needed,
1526 .fields = (VMStateField[]) {
1527 VMSTATE_UINT32_ARRAY(mac_reg, E1000State, 0x8000),
1528 VMSTATE_END_OF_LIST()
1529 }
1530};
1531
4ae4bf5b
DDAG
1532static const VMStateDescription vmstate_e1000_tx_tso_state = {
1533 .name = "e1000/tx_tso_state",
1534 .version_id = 1,
1535 .minimum_version_id = 1,
46f2a9ec 1536 .needed = e1000_tso_state_needed,
3c4053c5 1537 .post_load = e1000_tx_tso_post_load,
4ae4bf5b
DDAG
1538 .fields = (VMStateField[]) {
1539 VMSTATE_UINT8(tx.tso_props.ipcss, E1000State),
1540 VMSTATE_UINT8(tx.tso_props.ipcso, E1000State),
1541 VMSTATE_UINT16(tx.tso_props.ipcse, E1000State),
1542 VMSTATE_UINT8(tx.tso_props.tucss, E1000State),
1543 VMSTATE_UINT8(tx.tso_props.tucso, E1000State),
1544 VMSTATE_UINT16(tx.tso_props.tucse, E1000State),
1545 VMSTATE_UINT32(tx.tso_props.paylen, E1000State),
1546 VMSTATE_UINT8(tx.tso_props.hdr_len, E1000State),
1547 VMSTATE_UINT16(tx.tso_props.mss, E1000State),
1548 VMSTATE_INT8(tx.tso_props.ip, E1000State),
1549 VMSTATE_INT8(tx.tso_props.tcp, E1000State),
1550 VMSTATE_END_OF_LIST()
1551 }
1552};
1553
e482dc3e
JQ
1554static const VMStateDescription vmstate_e1000 = {
1555 .name = "e1000",
4ae4bf5b 1556 .version_id = 2,
e482dc3e 1557 .minimum_version_id = 1,
ddcb73b7 1558 .pre_save = e1000_pre_save,
e4b82364 1559 .post_load = e1000_post_load,
d49805ae 1560 .fields = (VMStateField[]) {
b08340d5 1561 VMSTATE_PCI_DEVICE(parent_obj, E1000State),
e482dc3e
JQ
1562 VMSTATE_UNUSED_TEST(is_version_1, 4), /* was instance id */
1563 VMSTATE_UNUSED(4), /* Was mmio_base. */
1564 VMSTATE_UINT32(rxbuf_size, E1000State),
1565 VMSTATE_UINT32(rxbuf_min_shift, E1000State),
1566 VMSTATE_UINT32(eecd_state.val_in, E1000State),
1567 VMSTATE_UINT16(eecd_state.bitnum_in, E1000State),
1568 VMSTATE_UINT16(eecd_state.bitnum_out, E1000State),
1569 VMSTATE_UINT16(eecd_state.reading, E1000State),
1570 VMSTATE_UINT32(eecd_state.old_eecd, E1000State),
59354484
DDAG
1571 VMSTATE_UINT8(mig_props.ipcss, E1000State),
1572 VMSTATE_UINT8(mig_props.ipcso, E1000State),
1573 VMSTATE_UINT16(mig_props.ipcse, E1000State),
1574 VMSTATE_UINT8(mig_props.tucss, E1000State),
1575 VMSTATE_UINT8(mig_props.tucso, E1000State),
1576 VMSTATE_UINT16(mig_props.tucse, E1000State),
1577 VMSTATE_UINT32(mig_props.paylen, E1000State),
1578 VMSTATE_UINT8(mig_props.hdr_len, E1000State),
1579 VMSTATE_UINT16(mig_props.mss, E1000State),
e482dc3e
JQ
1580 VMSTATE_UINT16(tx.size, E1000State),
1581 VMSTATE_UINT16(tx.tso_frames, E1000State),
7d08c73e 1582 VMSTATE_UINT8(tx.sum_needed, E1000State),
59354484
DDAG
1583 VMSTATE_INT8(mig_props.ip, E1000State),
1584 VMSTATE_INT8(mig_props.tcp, E1000State),
e482dc3e
JQ
1585 VMSTATE_BUFFER(tx.header, E1000State),
1586 VMSTATE_BUFFER(tx.data, E1000State),
1587 VMSTATE_UINT16_ARRAY(eeprom_data, E1000State, 64),
1588 VMSTATE_UINT16_ARRAY(phy_reg, E1000State, 0x20),
1589 VMSTATE_UINT32(mac_reg[CTRL], E1000State),
1590 VMSTATE_UINT32(mac_reg[EECD], E1000State),
1591 VMSTATE_UINT32(mac_reg[EERD], E1000State),
1592 VMSTATE_UINT32(mac_reg[GPRC], E1000State),
1593 VMSTATE_UINT32(mac_reg[GPTC], E1000State),
1594 VMSTATE_UINT32(mac_reg[ICR], E1000State),
1595 VMSTATE_UINT32(mac_reg[ICS], E1000State),
1596 VMSTATE_UINT32(mac_reg[IMC], E1000State),
1597 VMSTATE_UINT32(mac_reg[IMS], E1000State),
1598 VMSTATE_UINT32(mac_reg[LEDCTL], E1000State),
1599 VMSTATE_UINT32(mac_reg[MANC], E1000State),
1600 VMSTATE_UINT32(mac_reg[MDIC], E1000State),
1601 VMSTATE_UINT32(mac_reg[MPC], E1000State),
1602 VMSTATE_UINT32(mac_reg[PBA], E1000State),
1603 VMSTATE_UINT32(mac_reg[RCTL], E1000State),
1604 VMSTATE_UINT32(mac_reg[RDBAH], E1000State),
1605 VMSTATE_UINT32(mac_reg[RDBAL], E1000State),
1606 VMSTATE_UINT32(mac_reg[RDH], E1000State),
1607 VMSTATE_UINT32(mac_reg[RDLEN], E1000State),
1608 VMSTATE_UINT32(mac_reg[RDT], E1000State),
1609 VMSTATE_UINT32(mac_reg[STATUS], E1000State),
1610 VMSTATE_UINT32(mac_reg[SWSM], E1000State),
1611 VMSTATE_UINT32(mac_reg[TCTL], E1000State),
1612 VMSTATE_UINT32(mac_reg[TDBAH], E1000State),
1613 VMSTATE_UINT32(mac_reg[TDBAL], E1000State),
1614 VMSTATE_UINT32(mac_reg[TDH], E1000State),
1615 VMSTATE_UINT32(mac_reg[TDLEN], E1000State),
1616 VMSTATE_UINT32(mac_reg[TDT], E1000State),
1617 VMSTATE_UINT32(mac_reg[TORH], E1000State),
1618 VMSTATE_UINT32(mac_reg[TORL], E1000State),
1619 VMSTATE_UINT32(mac_reg[TOTH], E1000State),
1620 VMSTATE_UINT32(mac_reg[TOTL], E1000State),
1621 VMSTATE_UINT32(mac_reg[TPR], E1000State),
1622 VMSTATE_UINT32(mac_reg[TPT], E1000State),
1623 VMSTATE_UINT32(mac_reg[TXDCTL], E1000State),
1624 VMSTATE_UINT32(mac_reg[WUFC], E1000State),
1625 VMSTATE_UINT32(mac_reg[VET], E1000State),
1626 VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, RA, 32),
1627 VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, MTA, 128),
1628 VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, VFTA, 128),
1629 VMSTATE_END_OF_LIST()
e9845f09 1630 },
5cd8cada
JQ
1631 .subsections = (const VMStateDescription*[]) {
1632 &vmstate_e1000_mit_state,
9e117734 1633 &vmstate_e1000_full_mac_state,
4ae4bf5b 1634 &vmstate_e1000_tx_tso_state,
5cd8cada 1635 NULL
e482dc3e
JQ
1636 }
1637};
7c23b892 1638
8597f2e1
GS
1639/*
1640 * EEPROM contents documented in Tables 5-2 and 5-3, pp. 98-102.
80867bdb 1641 * Note: A valid DevId will be inserted during pci_e1000_realize().
8597f2e1 1642 */
88b4e9db 1643static const uint16_t e1000_eeprom_template[64] = {
7c23b892 1644 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0x0000,
8597f2e1 1645 0x3000, 0x1000, 0x6403, 0 /*DevId*/, 0x8086, 0 /*DevId*/, 0x8086, 0x3040,
7c23b892
AZ
1646 0x0008, 0x2000, 0x7e14, 0x0048, 0x1000, 0x00d8, 0x0000, 0x2700,
1647 0x6cc9, 0x3150, 0x0722, 0x040b, 0x0984, 0x0000, 0xc000, 0x0706,
1648 0x1008, 0x0000, 0x0f04, 0x7fff, 0x4d01, 0xffff, 0xffff, 0xffff,
1649 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
1650 0x0100, 0x4000, 0x121c, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
1651 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x0000,
1652};
1653
7c23b892
AZ
1654/* PCI interface */
1655
7c23b892 1656static void
ad00a9b9 1657e1000_mmio_setup(E1000State *d)
7c23b892 1658{
f65ed4c1
AL
1659 int i;
1660 const uint32_t excluded_regs[] = {
1661 E1000_MDIC, E1000_ICR, E1000_ICS, E1000_IMS,
1662 E1000_IMC, E1000_TCTL, E1000_TDT, PNPMMIO_SIZE
1663 };
1664
eedfac6f
PB
1665 memory_region_init_io(&d->mmio, OBJECT(d), &e1000_mmio_ops, d,
1666 "e1000-mmio", PNPMMIO_SIZE);
ad00a9b9 1667 memory_region_add_coalescing(&d->mmio, 0, excluded_regs[0]);
f65ed4c1 1668 for (i = 0; excluded_regs[i] != PNPMMIO_SIZE; i++)
ad00a9b9
AK
1669 memory_region_add_coalescing(&d->mmio, excluded_regs[i] + 4,
1670 excluded_regs[i+1] - excluded_regs[i] - 4);
eedfac6f 1671 memory_region_init_io(&d->io, OBJECT(d), &e1000_io_ops, d, "e1000-io", IOPORT_SIZE);
7c23b892
AZ
1672}
1673
f90c2bcd 1674static void
4b09be85
AL
1675pci_e1000_uninit(PCIDevice *dev)
1676{
567a3c9e 1677 E1000State *d = E1000(dev);
4b09be85 1678
bc72ad67 1679 timer_free(d->autoneg_timer);
e9845f09 1680 timer_free(d->mit_timer);
157628d0 1681 timer_free(d->flush_queue_timer);
948ecf21 1682 qemu_del_nic(d->nic);
4b09be85
AL
1683}
1684
a03e2aec 1685static NetClientInfo net_e1000_info = {
f394b2e2 1686 .type = NET_CLIENT_DRIVER_NIC,
a03e2aec
MM
1687 .size = sizeof(NICState),
1688 .can_receive = e1000_can_receive,
1689 .receive = e1000_receive,
97410dde 1690 .receive_iov = e1000_receive_iov,
a03e2aec
MM
1691 .link_status_changed = e1000_set_link_status,
1692};
1693
20302e71
MT
1694static void e1000_write_config(PCIDevice *pci_dev, uint32_t address,
1695 uint32_t val, int len)
1696{
1697 E1000State *s = E1000(pci_dev);
1698
1699 pci_default_write_config(pci_dev, address, val, len);
1700
1701 if (range_covers_byte(address, len, PCI_COMMAND) &&
1702 (pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
1703 qemu_flush_queued_packets(qemu_get_queue(s->nic));
1704 }
1705}
1706
9af21dbe 1707static void pci_e1000_realize(PCIDevice *pci_dev, Error **errp)
7c23b892 1708{
567a3c9e
PC
1709 DeviceState *dev = DEVICE(pci_dev);
1710 E1000State *d = E1000(pci_dev);
7c23b892 1711 uint8_t *pci_conf;
fbdaa002 1712 uint8_t *macaddr;
aff427a1 1713
20302e71
MT
1714 pci_dev->config_write = e1000_write_config;
1715
b08340d5 1716 pci_conf = pci_dev->config;
7c23b892 1717
a9cbacb0
MT
1718 /* TODO: RST# value should be 0, PCI spec 6.2.4 */
1719 pci_conf[PCI_CACHE_LINE_SIZE] = 0x10;
7c23b892 1720
817e0b6f 1721 pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
7c23b892 1722
ad00a9b9 1723 e1000_mmio_setup(d);
7c23b892 1724
b08340d5 1725 pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
7c23b892 1726
b08340d5 1727 pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->io);
7c23b892 1728
fbdaa002
GH
1729 qemu_macaddr_default_if_unset(&d->conf.macaddr);
1730 macaddr = d->conf.macaddr.a;
093454e2
DF
1731
1732 e1000x_core_prepare_eeprom(d->eeprom_data,
1733 e1000_eeprom_template,
1734 sizeof(e1000_eeprom_template),
1735 PCI_DEVICE_GET_CLASS(pci_dev)->device_id,
1736 macaddr);
7c23b892 1737
a03e2aec 1738 d->nic = qemu_new_nic(&net_e1000_info, &d->conf,
567a3c9e 1739 object_get_typename(OBJECT(d)), dev->id, d);
7c23b892 1740
b356f76d 1741 qemu_format_nic_info_str(qemu_get_queue(d->nic), macaddr);
1ca4d09a 1742
bc72ad67 1743 d->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, e1000_autoneg_timer, d);
e9845f09 1744 d->mit_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000_mit_timer, d);
157628d0
YCL
1745 d->flush_queue_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
1746 e1000_flush_queue_timer, d);
9d07d757 1747}
72da4208 1748
fbdaa002
GH
1749static void qdev_e1000_reset(DeviceState *dev)
1750{
567a3c9e 1751 E1000State *d = E1000(dev);
fbdaa002
GH
1752 e1000_reset(d);
1753}
1754
40021f08
AL
1755static Property e1000_properties[] = {
1756 DEFINE_NIC_PROPERTIES(E1000State, conf),
2af234e6
MT
1757 DEFINE_PROP_BIT("autonegotiation", E1000State,
1758 compat_flags, E1000_FLAG_AUTONEG_BIT, true),
e9845f09
VM
1759 DEFINE_PROP_BIT("mitigation", E1000State,
1760 compat_flags, E1000_FLAG_MIT_BIT, true),
ba63ec85
LB
1761 DEFINE_PROP_BIT("extra_mac_registers", E1000State,
1762 compat_flags, E1000_FLAG_MAC_BIT, true),
46f2a9ec
DDAG
1763 DEFINE_PROP_BIT("migrate_tso_props", E1000State,
1764 compat_flags, E1000_FLAG_TSO_BIT, true),
a1d7e475
CW
1765 DEFINE_PROP_BIT("init-vet", E1000State,
1766 compat_flags, E1000_FLAG_VET_BIT, true),
40021f08
AL
1767 DEFINE_PROP_END_OF_LIST(),
1768};
1769
8597f2e1
GS
1770typedef struct E1000Info {
1771 const char *name;
1772 uint16_t device_id;
1773 uint8_t revision;
1774 uint16_t phy_id2;
8597f2e1
GS
1775} E1000Info;
1776
40021f08
AL
1777static void e1000_class_init(ObjectClass *klass, void *data)
1778{
39bffca2 1779 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08 1780 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
c51325d8 1781 E1000BaseClass *e = E1000_CLASS(klass);
8597f2e1 1782 const E1000Info *info = data;
40021f08 1783
9af21dbe 1784 k->realize = pci_e1000_realize;
40021f08 1785 k->exit = pci_e1000_uninit;
c45e5b5b 1786 k->romfile = "efi-e1000.rom";
40021f08 1787 k->vendor_id = PCI_VENDOR_ID_INTEL;
8597f2e1
GS
1788 k->device_id = info->device_id;
1789 k->revision = info->revision;
1790 e->phy_id2 = info->phy_id2;
40021f08 1791 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
125ee0ed 1792 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
39bffca2
AL
1793 dc->desc = "Intel Gigabit Ethernet";
1794 dc->reset = qdev_e1000_reset;
1795 dc->vmsd = &vmstate_e1000;
4f67d30b 1796 device_class_set_props(dc, e1000_properties);
40021f08
AL
1797}
1798
5df3bf62
GA
1799static void e1000_instance_init(Object *obj)
1800{
1801 E1000State *n = E1000(obj);
1802 device_add_bootindex_property(obj, &n->conf.bootindex,
1803 "bootindex", "/ethernet-phy@0",
40c2281c 1804 DEVICE(n));
5df3bf62
GA
1805}
1806
8597f2e1
GS
1807static const TypeInfo e1000_base_info = {
1808 .name = TYPE_E1000_BASE,
39bffca2
AL
1809 .parent = TYPE_PCI_DEVICE,
1810 .instance_size = sizeof(E1000State),
5df3bf62 1811 .instance_init = e1000_instance_init,
8597f2e1
GS
1812 .class_size = sizeof(E1000BaseClass),
1813 .abstract = true,
fd3b02c8
EH
1814 .interfaces = (InterfaceInfo[]) {
1815 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1816 { },
1817 },
8597f2e1
GS
1818};
1819
1820static const E1000Info e1000_devices[] = {
1821 {
83044020 1822 .name = "e1000",
8597f2e1
GS
1823 .device_id = E1000_DEV_ID_82540EM,
1824 .revision = 0x03,
1825 .phy_id2 = E1000_PHY_ID2_8254xx_DEFAULT,
1826 },
1827 {
1828 .name = "e1000-82544gc",
1829 .device_id = E1000_DEV_ID_82544GC_COPPER,
1830 .revision = 0x03,
1831 .phy_id2 = E1000_PHY_ID2_82544x,
1832 },
1833 {
1834 .name = "e1000-82545em",
1835 .device_id = E1000_DEV_ID_82545EM_COPPER,
1836 .revision = 0x03,
1837 .phy_id2 = E1000_PHY_ID2_8254xx_DEFAULT,
1838 },
8597f2e1
GS
1839};
1840
83f7d43a 1841static void e1000_register_types(void)
9d07d757 1842{
8597f2e1
GS
1843 int i;
1844
1845 type_register_static(&e1000_base_info);
1846 for (i = 0; i < ARRAY_SIZE(e1000_devices); i++) {
1847 const E1000Info *info = &e1000_devices[i];
1848 TypeInfo type_info = {};
1849
1850 type_info.name = info->name;
1851 type_info.parent = TYPE_E1000_BASE;
1852 type_info.class_data = (void *)info;
1853 type_info.class_init = e1000_class_init;
1854
1855 type_register(&type_info);
1856 }
7c23b892 1857}
9d07d757 1858
83f7d43a 1859type_init(e1000_register_types)