4 * Software developer's manual:
5 * http://download.intel.com/design/network/manuals/8254x_GBe_SDM.pdf
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
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.
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.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include "qemu/osdep.h"
29 #include "hw/pci/pci.h"
30 #include "hw/qdev-properties.h"
31 #include "migration/vmstate.h"
33 #include "net/checksum.h"
34 #include "sysemu/sysemu.h"
35 #include "sysemu/dma.h"
37 #include "qemu/module.h"
38 #include "qemu/range.h"
40 #include "e1000x_common.h"
43 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
45 /* #define E1000_DEBUG */
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
,
52 DEBUG_RXFILTER
, DEBUG_PHY
, DEBUG_NOTYET
,
54 #define DBGBIT(x) (1<<DEBUG_##x)
55 static int debugflags
= DBGBIT(TXERR
) | DBGBIT(GENERAL
);
57 #define DBGOUT(what, fmt, ...) do { \
58 if (debugflags & DBGBIT(what)) \
59 fprintf(stderr, "e1000: " fmt, ## __VA_ARGS__); \
62 #define DBGOUT(what, fmt, ...) do {} while (0)
65 #define IOPORT_SIZE 0x40
66 #define PNPMMIO_SIZE 0x20000
67 #define MIN_BUF_SIZE 60 /* Min. octets in an ethernet frame sans FCS */
69 #define MAXIMUM_ETHERNET_HDR_LEN (14+4)
73 * E1000_DEV_ID_82540EM works with Windows, Linux, and OS X <= 10.8
74 * E1000_DEV_ID_82544GC_COPPER appears to work; not well tested
75 * E1000_DEV_ID_82545EM_COPPER works with Linux and OS X >= 10.6
79 typedef struct E1000State_st
{
89 uint32_t mac_reg
[0x8000];
90 uint16_t phy_reg
[0x20];
91 uint16_t eeprom_data
[64];
94 uint32_t rxbuf_min_shift
;
96 unsigned char header
[256];
97 unsigned char vlan_header
[4];
98 /* Fields vlan and data must not be reordered or separated. */
99 unsigned char vlan
[4];
100 unsigned char data
[0x10000];
102 unsigned char vlan_needed
;
103 unsigned char sum_needed
;
105 e1000x_txd_props props
;
106 e1000x_txd_props tso_props
;
111 uint32_t val_in
; /* shifted in from guest driver */
118 QEMUTimer
*autoneg_timer
;
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. */
125 QEMUTimer
*flush_queue_timer
;
127 /* Compatibility flags for migration to/from qemu 1.3.0 and older */
128 #define E1000_FLAG_AUTONEG_BIT 0
129 #define E1000_FLAG_MIT_BIT 1
130 #define E1000_FLAG_MAC_BIT 2
131 #define E1000_FLAG_TSO_BIT 3
132 #define E1000_FLAG_AUTONEG (1 << E1000_FLAG_AUTONEG_BIT)
133 #define E1000_FLAG_MIT (1 << E1000_FLAG_MIT_BIT)
134 #define E1000_FLAG_MAC (1 << E1000_FLAG_MAC_BIT)
135 #define E1000_FLAG_TSO (1 << E1000_FLAG_TSO_BIT)
136 uint32_t compat_flags
;
137 bool received_tx_tso
;
138 bool use_tso_for_migration
;
139 e1000x_txd_props mig_props
;
142 #define chkflag(x) (s->compat_flags & E1000_FLAG_##x)
144 typedef struct E1000BaseClass
{
145 PCIDeviceClass parent_class
;
149 #define TYPE_E1000_BASE "e1000-base"
152 OBJECT_CHECK(E1000State, (obj), TYPE_E1000_BASE)
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)
160 e1000_link_up(E1000State
*s
)
162 e1000x_update_regs_on_link_up(s
->mac_reg
, s
->phy_reg
);
164 /* E1000_STATUS_LU is tested by e1000_can_receive() */
165 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
169 e1000_autoneg_done(E1000State
*s
)
171 e1000x_update_regs_on_autoneg_done(s
->mac_reg
, s
->phy_reg
);
173 /* E1000_STATUS_LU is tested by e1000_can_receive() */
174 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
178 have_autoneg(E1000State
*s
)
180 return chkflag(AUTONEG
) && (s
->phy_reg
[PHY_CTRL
] & MII_CR_AUTO_NEG_EN
);
184 set_phy_ctrl(E1000State
*s
, int index
, uint16_t val
)
186 /* bits 0-5 reserved; MII_CR_[RESTART_AUTO_NEG,RESET] are self clearing */
187 s
->phy_reg
[PHY_CTRL
] = val
& ~(0x3f |
189 MII_CR_RESTART_AUTO_NEG
);
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
196 if (have_autoneg(s
) && (val
& MII_CR_RESTART_AUTO_NEG
)) {
197 e1000x_restart_autoneg(s
->mac_reg
, s
->phy_reg
, s
->autoneg_timer
);
201 static void (*phyreg_writeops
[])(E1000State
*, int, uint16_t) = {
202 [PHY_CTRL
] = set_phy_ctrl
,
205 enum { NPHYWRITEOPS
= ARRAY_SIZE(phyreg_writeops
) };
207 enum { PHY_R
= 1, PHY_W
= 2, PHY_RW
= PHY_R
| PHY_W
};
208 static const char phy_regcap
[0x20] = {
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
,
215 [PHY_AUTONEG_EXP
] = PHY_R
,
218 /* PHY_ID2 documented in 8254x_GBe_SDM.pdf, pp. 250 */
219 static const uint16_t phy_reg_init
[] = {
220 [PHY_CTRL
] = MII_CR_SPEED_SELECT_MSB
|
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
|
232 MII_SR_100X_HD_CAPS
|
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,
242 [M88E1000_PHY_SPEC_STATUS
] = 0xac00,
243 [M88E1000_EXT_PHY_SPEC_CTRL
] = 0x0d60,
246 static const uint32_t mac_reg_init
[] = {
249 [CTRL
] = E1000_CTRL_SWDPIN2
| E1000_CTRL_SWDPIN0
|
250 E1000_CTRL_SPD_1000
| E1000_CTRL_SLU
,
251 [STATUS
] = 0x80000000 | E1000_STATUS_GIO_MASTER_ENABLE
|
252 E1000_STATUS_ASDV
| E1000_STATUS_MTXCKOK
|
253 E1000_STATUS_SPEED_1000
| E1000_STATUS_FD
|
255 [MANC
] = E1000_MANC_EN_MNG2HOST
| E1000_MANC_RCV_TCO_EN
|
256 E1000_MANC_ARP_EN
| E1000_MANC_0298_EN
|
260 /* Helper function, *curr == 0 means the value is not set */
262 mit_update_delay(uint32_t *curr
, uint32_t value
)
264 if (value
&& (*curr
== 0 || value
< *curr
)) {
270 set_interrupt_cause(E1000State
*s
, int index
, uint32_t val
)
272 PCIDevice
*d
= PCI_DEVICE(s
);
273 uint32_t pending_ints
;
276 s
->mac_reg
[ICR
] = val
;
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).
284 * The VxWorks PRO/1000 driver uses this behaviour.
286 s
->mac_reg
[ICS
] = val
;
288 pending_ints
= (s
->mac_reg
[IMS
] & s
->mac_reg
[ICR
]);
289 if (!s
->mit_irq_level
&& pending_ints
) {
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.
299 if (s
->mit_timer_on
) {
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.
310 (pending_ints
& (E1000_ICR_TXQE
| E1000_ICR_TXDW
))) {
311 mit_update_delay(&mit_delay
, s
->mac_reg
[TADV
] * 4);
313 if (s
->mac_reg
[RDTR
] && (pending_ints
& E1000_ICS_RXT0
)) {
314 mit_update_delay(&mit_delay
, s
->mac_reg
[RADV
] * 4);
316 mit_update_delay(&mit_delay
, s
->mac_reg
[ITR
]);
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.
324 mit_delay
= (mit_delay
< 500) ? 500 : mit_delay
;
327 timer_mod(s
->mit_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
333 s
->mit_irq_level
= (pending_ints
!= 0);
334 pci_set_irq(d
, s
->mit_irq_level
);
338 e1000_mit_timer(void *opaque
)
340 E1000State
*s
= opaque
;
343 /* Call set_interrupt_cause to update the irq level (if necessary). */
344 set_interrupt_cause(s
, 0, s
->mac_reg
[ICR
]);
348 set_ics(E1000State
*s
, int index
, uint32_t val
)
350 DBGOUT(INTERRUPT
, "set_ics %x, ICR %x, IMR %x\n", val
, s
->mac_reg
[ICR
],
352 set_interrupt_cause(s
, 0, val
| s
->mac_reg
[ICR
]);
356 e1000_autoneg_timer(void *opaque
)
358 E1000State
*s
= opaque
;
359 if (!qemu_get_queue(s
->nic
)->link_down
) {
360 e1000_autoneg_done(s
);
361 set_ics(s
, 0, E1000_ICS_LSC
); /* signal link status change to guest */
365 static void e1000_reset(void *opaque
)
367 E1000State
*d
= opaque
;
368 E1000BaseClass
*edc
= E1000_DEVICE_GET_CLASS(d
);
369 uint8_t *macaddr
= d
->conf
.macaddr
.a
;
371 timer_del(d
->autoneg_timer
);
372 timer_del(d
->mit_timer
);
373 timer_del(d
->flush_queue_timer
);
375 d
->mit_irq_level
= 0;
377 memset(d
->phy_reg
, 0, sizeof d
->phy_reg
);
378 memmove(d
->phy_reg
, phy_reg_init
, sizeof phy_reg_init
);
379 d
->phy_reg
[PHY_ID2
] = edc
->phy_id2
;
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
);
385 if (qemu_get_queue(d
->nic
)->link_down
) {
386 e1000x_update_regs_on_link_down(d
->mac_reg
, d
->phy_reg
);
389 e1000x_reset_mac_addr(d
->nic
, d
->mac_reg
, macaddr
);
393 set_ctrl(E1000State
*s
, int index
, uint32_t val
)
395 /* RST is self clearing */
396 s
->mac_reg
[CTRL
] = val
& ~E1000_CTRL_RST
;
400 e1000_flush_queue_timer(void *opaque
)
402 E1000State
*s
= opaque
;
404 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
408 set_rx_control(E1000State
*s
, int index
, uint32_t val
)
410 s
->mac_reg
[RCTL
] = val
;
411 s
->rxbuf_size
= e1000x_rxbufsize(val
);
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
],
415 timer_mod(s
->flush_queue_timer
,
416 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + 1000);
420 set_mdic(E1000State
*s
, int index
, uint32_t val
)
422 uint32_t data
= val
& E1000_MDIC_DATA_MASK
;
423 uint32_t addr
= ((val
& E1000_MDIC_REG_MASK
) >> E1000_MDIC_REG_SHIFT
);
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
;
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
;
440 if (addr
< NPHYWRITEOPS
&& phyreg_writeops
[addr
]) {
441 phyreg_writeops
[addr
](s
, index
, data
);
443 s
->phy_reg
[addr
] = data
;
447 s
->mac_reg
[MDIC
] = val
| E1000_MDIC_READY
;
449 if (val
& E1000_MDIC_INT_EN
) {
450 set_ics(s
, 0, E1000_ICR_MDAC
);
455 get_eecd(E1000State
*s
, int index
)
457 uint32_t ret
= E1000_EECD_PRES
|E1000_EECD_GNT
| s
->eecd_state
.old_eecd
;
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
;
469 set_eecd(E1000State
*s
, int index
, uint32_t val
)
471 uint32_t oldval
= s
->eecd_state
.old_eecd
;
473 s
->eecd_state
.old_eecd
= val
& (E1000_EECD_SK
| E1000_EECD_CS
|
474 E1000_EECD_DI
|E1000_EECD_FWE_MASK
|E1000_EECD_REQ
);
475 if (!(E1000_EECD_CS
& val
)) { /* CS inactive; nothing to do */
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;
484 if (!(E1000_EECD_SK
& (val
^ oldval
))) { /* no clock edge */
487 if (!(E1000_EECD_SK
& val
)) { /* falling edge */
488 s
->eecd_state
.bitnum_out
++;
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
);
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
);
505 flash_eerd_read(E1000State
*s
, int x
)
507 unsigned int index
, r
= s
->mac_reg
[EERD
] & ~E1000_EEPROM_RW_REG_START
;
509 if ((s
->mac_reg
[EERD
] & E1000_EEPROM_RW_REG_START
) == 0)
510 return (s
->mac_reg
[EERD
]);
512 if ((index
= r
>> E1000_EEPROM_RW_ADDR_SHIFT
) > EEPROM_CHECKSUM_REG
)
513 return (E1000_EEPROM_RW_REG_DONE
| r
);
515 return ((s
->eeprom_data
[index
] << E1000_EEPROM_RW_REG_DATA
) |
516 E1000_EEPROM_RW_REG_DONE
| r
);
520 putsum(uint8_t *data
, uint32_t n
, uint32_t sloc
, uint32_t css
, uint32_t cse
)
527 sum
= net_checksum_add(n
-css
, data
+css
);
528 stw_be_p(data
+ sloc
, net_checksum_finish_nozero(sum
));
533 inc_tx_bcast_or_mcast_count(E1000State
*s
, const unsigned char *arr
)
535 if (!memcmp(arr
, bcast
, sizeof bcast
)) {
536 e1000x_inc_reg_if_not_full(s
->mac_reg
, BPTC
);
537 } else if (arr
[0] & 1) {
538 e1000x_inc_reg_if_not_full(s
->mac_reg
, MPTC
);
543 e1000_send_packet(E1000State
*s
, const uint8_t *buf
, int size
)
545 static const int PTCregs
[6] = { PTC64
, PTC127
, PTC255
, PTC511
,
548 NetClientState
*nc
= qemu_get_queue(s
->nic
);
549 if (s
->phy_reg
[PHY_CTRL
] & MII_CR_LOOPBACK
) {
550 nc
->info
->receive(nc
, buf
, size
);
552 qemu_send_packet(nc
, buf
, size
);
554 inc_tx_bcast_or_mcast_count(s
, buf
);
555 e1000x_increase_size_stats(s
->mac_reg
, PTCregs
, size
);
559 xmit_seg(E1000State
*s
)
562 unsigned int frames
= s
->tx
.tso_frames
, css
, sofar
;
563 struct e1000_tx
*tp
= &s
->tx
;
564 struct e1000x_txd_props
*props
= tp
->cptse
? &tp
->tso_props
: &tp
->props
;
568 DBGOUT(TXSUM
, "frames %d size %d ipcss %d\n",
569 frames
, tp
->size
, css
);
570 if (props
->ip
) { /* IPv4 */
571 stw_be_p(tp
->data
+css
+2, tp
->size
- css
);
572 stw_be_p(tp
->data
+css
+4,
573 lduw_be_p(tp
->data
+ css
+ 4) + frames
);
575 stw_be_p(tp
->data
+css
+4, tp
->size
- css
);
578 len
= tp
->size
- css
;
579 DBGOUT(TXSUM
, "tcp %d tucss %d len %d\n", props
->tcp
, css
, len
);
581 sofar
= frames
* props
->mss
;
582 stl_be_p(tp
->data
+css
+4, ldl_be_p(tp
->data
+css
+4)+sofar
); /* seq */
583 if (props
->paylen
- sofar
> props
->mss
) {
584 tp
->data
[css
+ 13] &= ~9; /* PSH, FIN */
586 e1000x_inc_reg_if_not_full(s
->mac_reg
, TSCTC
);
589 stw_be_p(tp
->data
+css
+4, len
);
591 if (tp
->sum_needed
& E1000_TXD_POPTS_TXSM
) {
593 // add pseudo-header length before checksum calculation
594 void *sp
= tp
->data
+ props
->tucso
;
596 phsum
= lduw_be_p(sp
) + len
;
597 phsum
= (phsum
>> 16) + (phsum
& 0xffff);
603 if (tp
->sum_needed
& E1000_TXD_POPTS_TXSM
) {
604 putsum(tp
->data
, tp
->size
, props
->tucso
, props
->tucss
, props
->tucse
);
606 if (tp
->sum_needed
& E1000_TXD_POPTS_IXSM
) {
607 putsum(tp
->data
, tp
->size
, props
->ipcso
, props
->ipcss
, props
->ipcse
);
609 if (tp
->vlan_needed
) {
610 memmove(tp
->vlan
, tp
->data
, 4);
611 memmove(tp
->data
, tp
->data
+ 4, 8);
612 memcpy(tp
->data
+ 8, tp
->vlan_header
, 4);
613 e1000_send_packet(s
, tp
->vlan
, tp
->size
+ 4);
615 e1000_send_packet(s
, tp
->data
, tp
->size
);
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
);
620 s
->mac_reg
[GPTC
] = s
->mac_reg
[TPT
];
621 s
->mac_reg
[GOTCL
] = s
->mac_reg
[TOTL
];
622 s
->mac_reg
[GOTCH
] = s
->mac_reg
[TOTH
];
626 process_tx_desc(E1000State
*s
, struct e1000_tx_desc
*dp
)
628 PCIDevice
*d
= PCI_DEVICE(s
);
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
);
631 unsigned int split_size
= txd_lower
& 0xffff, bytes
, sz
;
632 unsigned int msh
= 0xfffff;
634 struct e1000_context_desc
*xp
= (struct e1000_context_desc
*)dp
;
635 struct e1000_tx
*tp
= &s
->tx
;
637 s
->mit_ide
|= (txd_lower
& E1000_TXD_CMD_IDE
);
638 if (dtype
== E1000_TXD_CMD_DEXT
) { /* context descriptor */
639 if (le32_to_cpu(xp
->cmd_and_length
) & E1000_TXD_CMD_TSE
) {
640 e1000x_read_tx_ctx_descr(xp
, &tp
->tso_props
);
641 s
->use_tso_for_migration
= 1;
644 e1000x_read_tx_ctx_descr(xp
, &tp
->props
);
645 s
->use_tso_for_migration
= 0;
648 } else if (dtype
== (E1000_TXD_CMD_DEXT
| E1000_TXD_DTYP_D
)) {
651 tp
->sum_needed
= le32_to_cpu(dp
->upper
.data
) >> 8;
653 tp
->cptse
= (txd_lower
& E1000_TXD_CMD_TSE
) ? 1 : 0;
659 if (e1000x_vlan_enabled(s
->mac_reg
) &&
660 e1000x_is_vlan_txd(txd_lower
) &&
661 (tp
->cptse
|| txd_lower
& E1000_TXD_CMD_EOP
)) {
663 stw_be_p(tp
->vlan_header
,
664 le16_to_cpu(s
->mac_reg
[VET
]));
665 stw_be_p(tp
->vlan_header
+ 2,
666 le16_to_cpu(dp
->upper
.fields
.special
));
669 addr
= le64_to_cpu(dp
->buffer_addr
);
671 msh
= tp
->tso_props
.hdr_len
+ tp
->tso_props
.mss
;
674 if (tp
->size
+ bytes
> msh
)
675 bytes
= msh
- tp
->size
;
677 bytes
= MIN(sizeof(tp
->data
) - tp
->size
, bytes
);
678 pci_dma_read(d
, addr
, tp
->data
+ tp
->size
, bytes
);
679 sz
= tp
->size
+ bytes
;
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
);
688 memmove(tp
->data
, tp
->header
, tp
->tso_props
.hdr_len
);
689 tp
->size
= tp
->tso_props
.hdr_len
;
692 } while (bytes
&& split_size
);
694 split_size
= MIN(sizeof(tp
->data
) - tp
->size
, split_size
);
695 pci_dma_read(d
, addr
, tp
->data
+ tp
->size
, split_size
);
696 tp
->size
+= split_size
;
699 if (!(txd_lower
& E1000_TXD_CMD_EOP
))
701 if (!(tp
->cptse
&& tp
->size
< tp
->tso_props
.hdr_len
)) {
712 txdesc_writeback(E1000State
*s
, dma_addr_t base
, struct e1000_tx_desc
*dp
)
714 PCIDevice
*d
= PCI_DEVICE(s
);
715 uint32_t txd_upper
, txd_lower
= le32_to_cpu(dp
->lower
.data
);
717 if (!(txd_lower
& (E1000_TXD_CMD_RS
|E1000_TXD_CMD_RPS
)))
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
);
722 pci_dma_write(d
, base
+ ((char *)&dp
->upper
- (char *)dp
),
723 &dp
->upper
, sizeof(dp
->upper
));
724 return E1000_ICR_TXDW
;
727 static uint64_t tx_desc_base(E1000State
*s
)
729 uint64_t bah
= s
->mac_reg
[TDBAH
];
730 uint64_t bal
= s
->mac_reg
[TDBAL
] & ~0xf;
732 return (bah
<< 32) + bal
;
736 start_xmit(E1000State
*s
)
738 PCIDevice
*d
= PCI_DEVICE(s
);
740 struct e1000_tx_desc desc
;
741 uint32_t tdh_start
= s
->mac_reg
[TDH
], cause
= E1000_ICS_TXQE
;
743 if (!(s
->mac_reg
[TCTL
] & E1000_TCTL_EN
)) {
744 DBGOUT(TX
, "tx disabled\n");
748 while (s
->mac_reg
[TDH
] != s
->mac_reg
[TDT
]) {
749 base
= tx_desc_base(s
) +
750 sizeof(struct e1000_tx_desc
) * s
->mac_reg
[TDH
];
751 pci_dma_read(d
, base
, &desc
, sizeof(desc
));
753 DBGOUT(TX
, "index %d: %p : %x %x\n", s
->mac_reg
[TDH
],
754 (void *)(intptr_t)desc
.buffer_addr
, desc
.lower
.data
,
757 process_tx_desc(s
, &desc
);
758 cause
|= txdesc_writeback(s
, base
, &desc
);
760 if (++s
->mac_reg
[TDH
] * sizeof(desc
) >= s
->mac_reg
[TDLEN
])
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.
767 if (s
->mac_reg
[TDH
] == tdh_start
||
768 tdh_start
>= s
->mac_reg
[TDLEN
] / sizeof(desc
)) {
769 DBGOUT(TXERR
, "TDH wraparound @%x, TDT %x, TDLEN %x\n",
770 tdh_start
, s
->mac_reg
[TDT
], s
->mac_reg
[TDLEN
]);
774 set_ics(s
, 0, cause
);
778 receive_filter(E1000State
*s
, const uint8_t *buf
, int size
)
780 uint32_t rctl
= s
->mac_reg
[RCTL
];
781 int isbcast
= !memcmp(buf
, bcast
, sizeof bcast
), ismcast
= (buf
[0] & 1);
783 if (e1000x_is_vlan_packet(buf
, le16_to_cpu(s
->mac_reg
[VET
])) &&
784 e1000x_vlan_rx_filter_enabled(s
->mac_reg
)) {
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));
788 if ((vfta
& (1 << (vid
& 0x1f))) == 0)
792 if (!isbcast
&& !ismcast
&& (rctl
& E1000_RCTL_UPE
)) { /* promiscuous ucast */
796 if (ismcast
&& (rctl
& E1000_RCTL_MPE
)) { /* promiscuous mcast */
797 e1000x_inc_reg_if_not_full(s
->mac_reg
, MPRC
);
801 if (isbcast
&& (rctl
& E1000_RCTL_BAM
)) { /* broadcast enabled */
802 e1000x_inc_reg_if_not_full(s
->mac_reg
, BPRC
);
806 return e1000x_rx_group_filter(s
->mac_reg
, buf
);
810 e1000_set_link_status(NetClientState
*nc
)
812 E1000State
*s
= qemu_get_nic_opaque(nc
);
813 uint32_t old_status
= s
->mac_reg
[STATUS
];
816 e1000x_update_regs_on_link_down(s
->mac_reg
, s
->phy_reg
);
818 if (have_autoneg(s
) &&
819 !(s
->phy_reg
[PHY_STATUS
] & MII_SR_AUTONEG_COMPLETE
)) {
820 e1000x_restart_autoneg(s
->mac_reg
, s
->phy_reg
, s
->autoneg_timer
);
826 if (s
->mac_reg
[STATUS
] != old_status
)
827 set_ics(s
, 0, E1000_ICR_LSC
);
830 static bool e1000_has_rxbufs(E1000State
*s
, size_t total_size
)
833 /* Fast-path short packets */
834 if (total_size
<= s
->rxbuf_size
) {
835 return s
->mac_reg
[RDH
] != s
->mac_reg
[RDT
];
837 if (s
->mac_reg
[RDH
] < s
->mac_reg
[RDT
]) {
838 bufs
= s
->mac_reg
[RDT
] - s
->mac_reg
[RDH
];
839 } else if (s
->mac_reg
[RDH
] > s
->mac_reg
[RDT
]) {
840 bufs
= s
->mac_reg
[RDLEN
] / sizeof(struct e1000_rx_desc
) +
841 s
->mac_reg
[RDT
] - s
->mac_reg
[RDH
];
845 return total_size
<= bufs
* s
->rxbuf_size
;
849 e1000_can_receive(NetClientState
*nc
)
851 E1000State
*s
= qemu_get_nic_opaque(nc
);
853 return e1000x_rx_ready(&s
->parent_obj
, s
->mac_reg
) &&
854 e1000_has_rxbufs(s
, 1) && !timer_pending(s
->flush_queue_timer
);
857 static uint64_t rx_desc_base(E1000State
*s
)
859 uint64_t bah
= s
->mac_reg
[RDBAH
];
860 uint64_t bal
= s
->mac_reg
[RDBAL
] & ~0xf;
862 return (bah
<< 32) + bal
;
866 e1000_receiver_overrun(E1000State
*s
, size_t size
)
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
);
875 e1000_receive_iov(NetClientState
*nc
, const struct iovec
*iov
, int iovcnt
)
877 E1000State
*s
= qemu_get_nic_opaque(nc
);
878 PCIDevice
*d
= PCI_DEVICE(s
);
879 struct e1000_rx_desc desc
;
883 uint16_t vlan_special
= 0;
884 uint8_t vlan_status
= 0;
885 uint8_t min_buf
[MIN_BUF_SIZE
];
886 struct iovec min_iov
;
887 uint8_t *filter_buf
= iov
->iov_base
;
888 size_t size
= iov_size(iov
, iovcnt
);
894 if (!e1000x_hw_rx_enabled(s
->mac_reg
)) {
898 if (timer_pending(s
->flush_queue_timer
)) {
902 /* Pad to minimum Ethernet frame length */
903 if (size
< sizeof(min_buf
)) {
904 iov_to_buf(iov
, iovcnt
, 0, min_buf
, size
);
905 memset(&min_buf
[size
], 0, sizeof(min_buf
) - size
);
906 min_iov
.iov_base
= filter_buf
= min_buf
;
907 min_iov
.iov_len
= size
= sizeof(min_buf
);
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
;
916 /* Discard oversized packets if !LPE and !SBP. */
917 if (e1000x_is_oversized(s
->mac_reg
, size
)) {
921 if (!receive_filter(s
, filter_buf
, size
)) {
925 if (e1000x_vlan_enabled(s
->mac_reg
) &&
926 e1000x_is_vlan_packet(filter_buf
, le16_to_cpu(s
->mac_reg
[VET
]))) {
927 vlan_special
= cpu_to_le16(lduw_be_p(filter_buf
+ 14));
929 if (filter_buf
== iov
->iov_base
) {
930 memmove(filter_buf
+ 4, filter_buf
, 12);
932 iov_from_buf(iov
, iovcnt
, 4, filter_buf
, 12);
933 while (iov
->iov_len
<= iov_ofs
) {
934 iov_ofs
-= iov
->iov_len
;
938 vlan_status
= E1000_RXD_STAT_VP
;
942 rdh_start
= s
->mac_reg
[RDH
];
944 total_size
= size
+ e1000x_fcs_len(s
->mac_reg
);
945 if (!e1000_has_rxbufs(s
, total_size
)) {
946 e1000_receiver_overrun(s
, total_size
);
950 desc_size
= total_size
- desc_offset
;
951 if (desc_size
> s
->rxbuf_size
) {
952 desc_size
= s
->rxbuf_size
;
954 base
= rx_desc_base(s
) + sizeof(desc
) * s
->mac_reg
[RDH
];
955 pci_dma_read(d
, base
, &desc
, sizeof(desc
));
956 desc
.special
= vlan_special
;
957 desc
.status
|= (vlan_status
| E1000_RXD_STAT_DD
);
958 if (desc
.buffer_addr
) {
959 if (desc_offset
< size
) {
961 hwaddr ba
= le64_to_cpu(desc
.buffer_addr
);
962 size_t copy_size
= size
- desc_offset
;
963 if (copy_size
> s
->rxbuf_size
) {
964 copy_size
= s
->rxbuf_size
;
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
;
972 if (iov_ofs
== iov
->iov_len
) {
978 desc_offset
+= desc_size
;
979 desc
.length
= cpu_to_le16(desc_size
);
980 if (desc_offset
>= total_size
) {
981 desc
.status
|= E1000_RXD_STAT_EOP
| E1000_RXD_STAT_IXSM
;
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
;
987 } else { // as per intel docs; skip descriptors with null buf addr
988 DBGOUT(RX
, "Null RX descriptor!!\n");
990 pci_dma_write(d
, base
, &desc
, sizeof(desc
));
992 if (++s
->mac_reg
[RDH
] * sizeof(desc
) >= s
->mac_reg
[RDLEN
])
994 /* see comment in start_xmit; same here */
995 if (s
->mac_reg
[RDH
] == rdh_start
||
996 rdh_start
>= s
->mac_reg
[RDLEN
] / sizeof(desc
)) {
997 DBGOUT(RXERR
, "RDH wraparound @%x, RDT %x, RDLEN %x\n",
998 rdh_start
, s
->mac_reg
[RDT
], s
->mac_reg
[RDLEN
]);
999 e1000_receiver_overrun(s
, total_size
);
1002 } while (desc_offset
< total_size
);
1004 e1000x_update_rx_total_stats(s
->mac_reg
, size
, total_size
);
1007 if ((rdt
= s
->mac_reg
[RDT
]) < s
->mac_reg
[RDH
])
1008 rdt
+= s
->mac_reg
[RDLEN
] / sizeof(desc
);
1009 if (((rdt
- s
->mac_reg
[RDH
]) * sizeof(desc
)) <= s
->mac_reg
[RDLEN
] >>
1011 n
|= E1000_ICS_RXDMT0
;
1019 e1000_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
1021 const struct iovec iov
= {
1022 .iov_base
= (uint8_t *)buf
,
1026 return e1000_receive_iov(nc
, &iov
, 1);
1030 mac_readreg(E1000State
*s
, int index
)
1032 return s
->mac_reg
[index
];
1036 mac_low4_read(E1000State
*s
, int index
)
1038 return s
->mac_reg
[index
] & 0xf;
1042 mac_low11_read(E1000State
*s
, int index
)
1044 return s
->mac_reg
[index
] & 0x7ff;
1048 mac_low13_read(E1000State
*s
, int index
)
1050 return s
->mac_reg
[index
] & 0x1fff;
1054 mac_low16_read(E1000State
*s
, int index
)
1056 return s
->mac_reg
[index
] & 0xffff;
1060 mac_icr_read(E1000State
*s
, int index
)
1062 uint32_t ret
= s
->mac_reg
[ICR
];
1064 DBGOUT(INTERRUPT
, "ICR read: %x\n", ret
);
1065 set_interrupt_cause(s
, 0, 0);
1070 mac_read_clr4(E1000State
*s
, int index
)
1072 uint32_t ret
= s
->mac_reg
[index
];
1074 s
->mac_reg
[index
] = 0;
1079 mac_read_clr8(E1000State
*s
, int index
)
1081 uint32_t ret
= s
->mac_reg
[index
];
1083 s
->mac_reg
[index
] = 0;
1084 s
->mac_reg
[index
-1] = 0;
1089 mac_writereg(E1000State
*s
, int index
, uint32_t val
)
1091 uint32_t macaddr
[2];
1093 s
->mac_reg
[index
] = val
;
1095 if (index
== RA
+ 1) {
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
);
1103 set_rdt(E1000State
*s
, int index
, uint32_t val
)
1105 s
->mac_reg
[index
] = val
& 0xffff;
1106 if (e1000_has_rxbufs(s
, 1)) {
1107 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
1112 set_16bit(E1000State
*s
, int index
, uint32_t val
)
1114 s
->mac_reg
[index
] = val
& 0xffff;
1118 set_dlen(E1000State
*s
, int index
, uint32_t val
)
1120 s
->mac_reg
[index
] = val
& 0xfff80;
1124 set_tctl(E1000State
*s
, int index
, uint32_t val
)
1126 s
->mac_reg
[index
] = val
;
1127 s
->mac_reg
[TDT
] &= 0xffff;
1132 set_icr(E1000State
*s
, int index
, uint32_t val
)
1134 DBGOUT(INTERRUPT
, "set_icr %x\n", val
);
1135 set_interrupt_cause(s
, 0, s
->mac_reg
[ICR
] & ~val
);
1139 set_imc(E1000State
*s
, int index
, uint32_t val
)
1141 s
->mac_reg
[IMS
] &= ~val
;
1146 set_ims(E1000State
*s
, int index
, uint32_t val
)
1148 s
->mac_reg
[IMS
] |= val
;
1152 #define getreg(x) [x] = mac_readreg
1153 static uint32_t (*macreg_readops
[])(E1000State
*, int) = {
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
),
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
),
1164 getreg(TNCRS
), getreg(SEQEC
), getreg(CEXTERR
), getreg(RLEC
),
1165 getreg(XONRXC
), getreg(XONTXC
), getreg(XOFFRXC
), getreg(XOFFTXC
),
1166 getreg(RFC
), getreg(RJC
), getreg(RNBC
), getreg(TSCTFC
),
1167 getreg(MGTPRC
), getreg(MGTPDC
), getreg(MGTPTC
), getreg(GORCL
),
1170 [TOTH
] = mac_read_clr8
, [TORH
] = mac_read_clr8
,
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
,
1178 [GPRC
] = mac_read_clr4
, [GPTC
] = mac_read_clr4
,
1179 [TPT
] = mac_read_clr4
, [TPR
] = mac_read_clr4
,
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
,
1184 [ICR
] = mac_icr_read
, [EECD
] = get_eecd
,
1185 [EERD
] = flash_eerd_read
,
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
,
1194 [CRCERRS
... MPC
] = &mac_readreg
,
1195 [IP6AT
... IP6AT
+3] = &mac_readreg
, [IP4AT
... IP4AT
+6] = &mac_readreg
,
1196 [FFLT
... FFLT
+6] = &mac_low11_read
,
1197 [RA
... RA
+31] = &mac_readreg
,
1198 [WUPM
... WUPM
+31] = &mac_readreg
,
1199 [MTA
... MTA
+127] = &mac_readreg
,
1200 [VFTA
... VFTA
+127] = &mac_readreg
,
1201 [FFMT
... FFMT
+254] = &mac_low4_read
,
1202 [FFVT
... FFVT
+254] = &mac_readreg
,
1203 [PBM
... PBM
+16383] = &mac_readreg
,
1205 enum { NREADOPS
= ARRAY_SIZE(macreg_readops
) };
1207 #define putreg(x) [x] = mac_writereg
1208 static void (*macreg_writeops
[])(E1000State
*, int, uint32_t) = {
1209 putreg(PBA
), putreg(EERD
), putreg(SWSM
), putreg(WUFC
),
1210 putreg(TDBAL
), putreg(TDBAH
), putreg(TXDCTL
), putreg(RDBAH
),
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
),
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
,
1225 [IP6AT
... IP6AT
+3] = &mac_writereg
, [IP4AT
... IP4AT
+6] = &mac_writereg
,
1226 [FFLT
... FFLT
+6] = &mac_writereg
,
1227 [RA
... RA
+31] = &mac_writereg
,
1228 [WUPM
... WUPM
+31] = &mac_writereg
,
1229 [MTA
... MTA
+127] = &mac_writereg
,
1230 [VFTA
... VFTA
+127] = &mac_writereg
,
1231 [FFMT
... FFMT
+254] = &mac_writereg
, [FFVT
... FFVT
+254] = &mac_writereg
,
1232 [PBM
... PBM
+16383] = &mac_writereg
,
1235 enum { NWRITEOPS
= ARRAY_SIZE(macreg_writeops
) };
1237 enum { MAC_ACCESS_PARTIAL
= 1, MAC_ACCESS_FLAG_NEEDED
= 2 };
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)
1243 * p - partially implenented */
1244 static const uint8_t mac_reg_access
[0x8000] = {
1245 [RDTR
] = markflag(MIT
), [TADV
] = markflag(MIT
),
1246 [RADV
] = markflag(MIT
), [ITR
] = markflag(MIT
),
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
),
1260 [SEQEC
] = markflag(MAC
), [CEXTERR
] = markflag(MAC
),
1261 [XONTXC
] = markflag(MAC
), [XOFFRXC
] = markflag(MAC
),
1262 [RJC
] = markflag(MAC
), [RNBC
] = markflag(MAC
),
1263 [MGTPDC
] = markflag(MAC
), [MGTPTC
] = markflag(MAC
),
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
),
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
,
1291 e1000_mmio_write(void *opaque
, hwaddr addr
, uint64_t val
,
1294 E1000State
*s
= opaque
;
1295 unsigned int index
= (addr
& 0x1ffff) >> 2;
1297 if (index
< NWRITEOPS
&& macreg_writeops
[index
]) {
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);
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",
1309 } else if (index
< NREADOPS
&& macreg_readops
[index
]) {
1310 DBGOUT(MMIO
, "e1000_mmio_writel RO %x: 0x%04"PRIx64
"\n",
1313 DBGOUT(UNKNOWN
, "MMIO unknown write addr=0x%08x,val=0x%08"PRIx64
"\n",
1319 e1000_mmio_read(void *opaque
, hwaddr addr
, unsigned size
)
1321 E1000State
*s
= opaque
;
1322 unsigned int index
= (addr
& 0x1ffff) >> 2;
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);
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",
1337 DBGOUT(UNKNOWN
, "MMIO unknown read addr=0x%08x\n", index
<<2);
1342 static const MemoryRegionOps e1000_mmio_ops
= {
1343 .read
= e1000_mmio_read
,
1344 .write
= e1000_mmio_write
,
1345 .endianness
= DEVICE_LITTLE_ENDIAN
,
1347 .min_access_size
= 4,
1348 .max_access_size
= 4,
1352 static uint64_t e1000_io_read(void *opaque
, hwaddr addr
,
1355 E1000State
*s
= opaque
;
1361 static void e1000_io_write(void *opaque
, hwaddr addr
,
1362 uint64_t val
, unsigned size
)
1364 E1000State
*s
= opaque
;
1369 static const MemoryRegionOps e1000_io_ops
= {
1370 .read
= e1000_io_read
,
1371 .write
= e1000_io_write
,
1372 .endianness
= DEVICE_LITTLE_ENDIAN
,
1375 static bool is_version_1(void *opaque
, int version_id
)
1377 return version_id
== 1;
1380 static int e1000_pre_save(void *opaque
)
1382 E1000State
*s
= opaque
;
1383 NetClientState
*nc
= qemu_get_queue(s
->nic
);
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.
1390 if (nc
->link_down
&& have_autoneg(s
)) {
1391 s
->phy_reg
[PHY_STATUS
] |= MII_SR_AUTONEG_COMPLETE
;
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
1401 s
->mig_props
= s
->tx
.props
;
1403 /* We're not using the subsection, and 'tso_props' was
1406 s
->mig_props
= s
->tx
.tso_props
;
1411 static int e1000_post_load(void *opaque
, int version_id
)
1413 E1000State
*s
= opaque
;
1414 NetClientState
*nc
= qemu_get_queue(s
->nic
);
1416 if (!chkflag(MIT
)) {
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;
1422 s
->mit_timer_on
= true;
1423 timer_mod(s
->mit_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 1);
1425 /* nc.link_down can't be migrated, so infer link_down according
1426 * to link status bit in mac_reg[STATUS].
1427 * Alternatively, restart link negotiation if it was in progress. */
1428 nc
->link_down
= (s
->mac_reg
[STATUS
] & E1000_STATUS_LU
) == 0;
1430 if (have_autoneg(s
) &&
1431 !(s
->phy_reg
[PHY_STATUS
] & MII_SR_AUTONEG_COMPLETE
)) {
1432 nc
->link_down
= false;
1433 timer_mod(s
->autoneg_timer
,
1434 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + 500);
1437 s
->tx
.props
= s
->mig_props
;
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
1443 s
->tx
.tso_props
= s
->mig_props
;
1448 static int e1000_tx_tso_post_load(void *opaque
, int version_id
)
1450 E1000State
*s
= opaque
;
1451 s
->received_tx_tso
= true;
1455 static bool e1000_mit_state_needed(void *opaque
)
1457 E1000State
*s
= opaque
;
1459 return chkflag(MIT
);
1462 static bool e1000_full_mac_needed(void *opaque
)
1464 E1000State
*s
= opaque
;
1466 return chkflag(MAC
);
1469 static bool e1000_tso_state_needed(void *opaque
)
1471 E1000State
*s
= opaque
;
1473 return chkflag(TSO
);
1476 static const VMStateDescription vmstate_e1000_mit_state
= {
1477 .name
= "e1000/mit_state",
1479 .minimum_version_id
= 1,
1480 .needed
= e1000_mit_state_needed
,
1481 .fields
= (VMStateField
[]) {
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()
1491 static const VMStateDescription vmstate_e1000_full_mac_state
= {
1492 .name
= "e1000/full_mac_state",
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()
1502 static const VMStateDescription vmstate_e1000_tx_tso_state
= {
1503 .name
= "e1000/tx_tso_state",
1505 .minimum_version_id
= 1,
1506 .needed
= e1000_tso_state_needed
,
1507 .post_load
= e1000_tx_tso_post_load
,
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()
1524 static const VMStateDescription vmstate_e1000
= {
1527 .minimum_version_id
= 1,
1528 .pre_save
= e1000_pre_save
,
1529 .post_load
= e1000_post_load
,
1530 .fields
= (VMStateField
[]) {
1531 VMSTATE_PCI_DEVICE(parent_obj
, E1000State
),
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
),
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
),
1550 VMSTATE_UINT16(tx
.size
, E1000State
),
1551 VMSTATE_UINT16(tx
.tso_frames
, E1000State
),
1552 VMSTATE_UINT8(tx
.sum_needed
, E1000State
),
1553 VMSTATE_INT8(mig_props
.ip
, E1000State
),
1554 VMSTATE_INT8(mig_props
.tcp
, E1000State
),
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()
1601 .subsections
= (const VMStateDescription
*[]) {
1602 &vmstate_e1000_mit_state
,
1603 &vmstate_e1000_full_mac_state
,
1604 &vmstate_e1000_tx_tso_state
,
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().
1613 static const uint16_t e1000_eeprom_template
[64] = {
1614 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0x0000,
1615 0x3000, 0x1000, 0x6403, 0 /*DevId*/, 0x8086, 0 /*DevId*/, 0x8086, 0x3040,
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,
1627 e1000_mmio_setup(E1000State
*d
)
1630 const uint32_t excluded_regs
[] = {
1631 E1000_MDIC
, E1000_ICR
, E1000_ICS
, E1000_IMS
,
1632 E1000_IMC
, E1000_TCTL
, E1000_TDT
, PNPMMIO_SIZE
1635 memory_region_init_io(&d
->mmio
, OBJECT(d
), &e1000_mmio_ops
, d
,
1636 "e1000-mmio", PNPMMIO_SIZE
);
1637 memory_region_add_coalescing(&d
->mmio
, 0, excluded_regs
[0]);
1638 for (i
= 0; excluded_regs
[i
] != PNPMMIO_SIZE
; i
++)
1639 memory_region_add_coalescing(&d
->mmio
, excluded_regs
[i
] + 4,
1640 excluded_regs
[i
+1] - excluded_regs
[i
] - 4);
1641 memory_region_init_io(&d
->io
, OBJECT(d
), &e1000_io_ops
, d
, "e1000-io", IOPORT_SIZE
);
1645 pci_e1000_uninit(PCIDevice
*dev
)
1647 E1000State
*d
= E1000(dev
);
1649 timer_del(d
->autoneg_timer
);
1650 timer_free(d
->autoneg_timer
);
1651 timer_del(d
->mit_timer
);
1652 timer_free(d
->mit_timer
);
1653 timer_del(d
->flush_queue_timer
);
1654 timer_free(d
->flush_queue_timer
);
1655 qemu_del_nic(d
->nic
);
1658 static NetClientInfo net_e1000_info
= {
1659 .type
= NET_CLIENT_DRIVER_NIC
,
1660 .size
= sizeof(NICState
),
1661 .can_receive
= e1000_can_receive
,
1662 .receive
= e1000_receive
,
1663 .receive_iov
= e1000_receive_iov
,
1664 .link_status_changed
= e1000_set_link_status
,
1667 static void e1000_write_config(PCIDevice
*pci_dev
, uint32_t address
,
1668 uint32_t val
, int len
)
1670 E1000State
*s
= E1000(pci_dev
);
1672 pci_default_write_config(pci_dev
, address
, val
, len
);
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
));
1680 static void pci_e1000_realize(PCIDevice
*pci_dev
, Error
**errp
)
1682 DeviceState
*dev
= DEVICE(pci_dev
);
1683 E1000State
*d
= E1000(pci_dev
);
1687 pci_dev
->config_write
= e1000_write_config
;
1689 pci_conf
= pci_dev
->config
;
1691 /* TODO: RST# value should be 0, PCI spec 6.2.4 */
1692 pci_conf
[PCI_CACHE_LINE_SIZE
] = 0x10;
1694 pci_conf
[PCI_INTERRUPT_PIN
] = 1; /* interrupt pin A */
1696 e1000_mmio_setup(d
);
1698 pci_register_bar(pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
, &d
->mmio
);
1700 pci_register_bar(pci_dev
, 1, PCI_BASE_ADDRESS_SPACE_IO
, &d
->io
);
1702 qemu_macaddr_default_if_unset(&d
->conf
.macaddr
);
1703 macaddr
= d
->conf
.macaddr
.a
;
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
,
1711 d
->nic
= qemu_new_nic(&net_e1000_info
, &d
->conf
,
1712 object_get_typename(OBJECT(d
)), dev
->id
, d
);
1714 qemu_format_nic_info_str(qemu_get_queue(d
->nic
), macaddr
);
1716 d
->autoneg_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, e1000_autoneg_timer
, d
);
1717 d
->mit_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, e1000_mit_timer
, d
);
1718 d
->flush_queue_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
,
1719 e1000_flush_queue_timer
, d
);
1722 static void qdev_e1000_reset(DeviceState
*dev
)
1724 E1000State
*d
= E1000(dev
);
1728 static Property e1000_properties
[] = {
1729 DEFINE_NIC_PROPERTIES(E1000State
, conf
),
1730 DEFINE_PROP_BIT("autonegotiation", E1000State
,
1731 compat_flags
, E1000_FLAG_AUTONEG_BIT
, true),
1732 DEFINE_PROP_BIT("mitigation", E1000State
,
1733 compat_flags
, E1000_FLAG_MIT_BIT
, true),
1734 DEFINE_PROP_BIT("extra_mac_registers", E1000State
,
1735 compat_flags
, E1000_FLAG_MAC_BIT
, true),
1736 DEFINE_PROP_BIT("migrate_tso_props", E1000State
,
1737 compat_flags
, E1000_FLAG_TSO_BIT
, true),
1738 DEFINE_PROP_END_OF_LIST(),
1741 typedef struct E1000Info
{
1748 static void e1000_class_init(ObjectClass
*klass
, void *data
)
1750 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1751 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1752 E1000BaseClass
*e
= E1000_DEVICE_CLASS(klass
);
1753 const E1000Info
*info
= data
;
1755 k
->realize
= pci_e1000_realize
;
1756 k
->exit
= pci_e1000_uninit
;
1757 k
->romfile
= "efi-e1000.rom";
1758 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
1759 k
->device_id
= info
->device_id
;
1760 k
->revision
= info
->revision
;
1761 e
->phy_id2
= info
->phy_id2
;
1762 k
->class_id
= PCI_CLASS_NETWORK_ETHERNET
;
1763 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
1764 dc
->desc
= "Intel Gigabit Ethernet";
1765 dc
->reset
= qdev_e1000_reset
;
1766 dc
->vmsd
= &vmstate_e1000
;
1767 dc
->props
= e1000_properties
;
1770 static void e1000_instance_init(Object
*obj
)
1772 E1000State
*n
= E1000(obj
);
1773 device_add_bootindex_property(obj
, &n
->conf
.bootindex
,
1774 "bootindex", "/ethernet-phy@0",
1778 static const TypeInfo e1000_base_info
= {
1779 .name
= TYPE_E1000_BASE
,
1780 .parent
= TYPE_PCI_DEVICE
,
1781 .instance_size
= sizeof(E1000State
),
1782 .instance_init
= e1000_instance_init
,
1783 .class_size
= sizeof(E1000BaseClass
),
1785 .interfaces
= (InterfaceInfo
[]) {
1786 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
1791 static const E1000Info e1000_devices
[] = {
1794 .device_id
= E1000_DEV_ID_82540EM
,
1796 .phy_id2
= E1000_PHY_ID2_8254xx_DEFAULT
,
1799 .name
= "e1000-82544gc",
1800 .device_id
= E1000_DEV_ID_82544GC_COPPER
,
1802 .phy_id2
= E1000_PHY_ID2_82544x
,
1805 .name
= "e1000-82545em",
1806 .device_id
= E1000_DEV_ID_82545EM_COPPER
,
1808 .phy_id2
= E1000_PHY_ID2_8254xx_DEFAULT
,
1812 static void e1000_register_types(void)
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
= {};
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
;
1825 type_info
.instance_init
= e1000_instance_init
;
1827 type_register(&type_info
);
1831 type_init(e1000_register_types
)