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