17c23b892Sbalrog /* 27c23b892Sbalrog * QEMU e1000 emulation 37c23b892Sbalrog * 42758aa52SMichael S. Tsirkin * Software developer's manual: 52758aa52SMichael S. Tsirkin * http://download.intel.com/design/network/manuals/8254x_GBe_SDM.pdf 62758aa52SMichael S. Tsirkin * 77c23b892Sbalrog * Nir Peleg, Tutis Systems Ltd. for Qumranet Inc. 87c23b892Sbalrog * Copyright (c) 2008 Qumranet 97c23b892Sbalrog * Based on work done by: 107c23b892Sbalrog * Copyright (c) 2007 Dan Aloni 117c23b892Sbalrog * Copyright (c) 2004 Antony T Curtis 127c23b892Sbalrog * 137c23b892Sbalrog * This library is free software; you can redistribute it and/or 147c23b892Sbalrog * modify it under the terms of the GNU Lesser General Public 157c23b892Sbalrog * License as published by the Free Software Foundation; either 167c23b892Sbalrog * version 2 of the License, or (at your option) any later version. 177c23b892Sbalrog * 187c23b892Sbalrog * This library is distributed in the hope that it will be useful, 197c23b892Sbalrog * but WITHOUT ANY WARRANTY; without even the implied warranty of 207c23b892Sbalrog * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 217c23b892Sbalrog * Lesser General Public License for more details. 227c23b892Sbalrog * 237c23b892Sbalrog * You should have received a copy of the GNU Lesser General Public 248167ee88SBlue Swirl * License along with this library; if not, see <http://www.gnu.org/licenses/>. 257c23b892Sbalrog */ 267c23b892Sbalrog 277c23b892Sbalrog 28e8d40465SPeter Maydell #include "qemu/osdep.h" 2983c9f4caSPaolo Bonzini #include "hw/hw.h" 3083c9f4caSPaolo Bonzini #include "hw/pci/pci.h" 311422e32dSPaolo Bonzini #include "net/net.h" 327200ac3cSMark McLoughlin #include "net/checksum.h" 339c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 349c17d615SPaolo Bonzini #include "sysemu/dma.h" 3597410ddeSVincenzo Maffione #include "qemu/iov.h" 3620302e71SMichael S. Tsirkin #include "qemu/range.h" 377c23b892Sbalrog 38093454e2SDmitry Fleytman #include "e1000x_common.h" 391001cf45SJason Wang #include "trace.h" 407c23b892Sbalrog 413b274301SLeonid Bloch static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 423b274301SLeonid Bloch 43b4053c64SJason Wang /* #define E1000_DEBUG */ 447c23b892Sbalrog 4527124888SJes Sorensen #ifdef E1000_DEBUG 467c23b892Sbalrog enum { 477c23b892Sbalrog DEBUG_GENERAL, DEBUG_IO, DEBUG_MMIO, DEBUG_INTERRUPT, 487c23b892Sbalrog DEBUG_RX, DEBUG_TX, DEBUG_MDIC, DEBUG_EEPROM, 497c23b892Sbalrog DEBUG_UNKNOWN, DEBUG_TXSUM, DEBUG_TXERR, DEBUG_RXERR, 50f9c1cdf4SJason Wang DEBUG_RXFILTER, DEBUG_PHY, DEBUG_NOTYET, 517c23b892Sbalrog }; 527c23b892Sbalrog #define DBGBIT(x) (1<<DEBUG_##x) 537c23b892Sbalrog static int debugflags = DBGBIT(TXERR) | DBGBIT(GENERAL); 547c23b892Sbalrog 556c7f4b47SBlue Swirl #define DBGOUT(what, fmt, ...) do { \ 567c23b892Sbalrog if (debugflags & DBGBIT(what)) \ 576c7f4b47SBlue Swirl fprintf(stderr, "e1000: " fmt, ## __VA_ARGS__); \ 587c23b892Sbalrog } while (0) 597c23b892Sbalrog #else 606c7f4b47SBlue Swirl #define DBGOUT(what, fmt, ...) do {} while (0) 617c23b892Sbalrog #endif 627c23b892Sbalrog 637c23b892Sbalrog #define IOPORT_SIZE 0x40 64e94bbefeSaurel32 #define PNPMMIO_SIZE 0x20000 6578aeb23eSStefan Hajnoczi #define MIN_BUF_SIZE 60 /* Min. octets in an ethernet frame sans FCS */ 667c23b892Sbalrog 6797410ddeSVincenzo Maffione #define MAXIMUM_ETHERNET_HDR_LEN (14+4) 6897410ddeSVincenzo Maffione 697c23b892Sbalrog /* 707c23b892Sbalrog * HW models: 718597f2e1SGabriel L. Somlo * E1000_DEV_ID_82540EM works with Windows, Linux, and OS X <= 10.8 727c23b892Sbalrog * E1000_DEV_ID_82544GC_COPPER appears to work; not well tested 738597f2e1SGabriel L. Somlo * E1000_DEV_ID_82545EM_COPPER works with Linux and OS X >= 10.6 747c23b892Sbalrog * Others never tested 757c23b892Sbalrog */ 767c23b892Sbalrog 777c23b892Sbalrog typedef struct E1000State_st { 78b08340d5SAndreas Färber /*< private >*/ 79b08340d5SAndreas Färber PCIDevice parent_obj; 80b08340d5SAndreas Färber /*< public >*/ 81b08340d5SAndreas Färber 82a03e2aecSMark McLoughlin NICState *nic; 83fbdaa002SGerd Hoffmann NICConf conf; 84ad00a9b9SAvi Kivity MemoryRegion mmio; 85ad00a9b9SAvi Kivity MemoryRegion io; 867c23b892Sbalrog 877c23b892Sbalrog uint32_t mac_reg[0x8000]; 887c23b892Sbalrog uint16_t phy_reg[0x20]; 897c23b892Sbalrog uint16_t eeprom_data[64]; 907c23b892Sbalrog 917c23b892Sbalrog uint32_t rxbuf_size; 927c23b892Sbalrog uint32_t rxbuf_min_shift; 937c23b892Sbalrog struct e1000_tx { 947c23b892Sbalrog unsigned char header[256]; 958f2e8d1fSaliguori unsigned char vlan_header[4]; 96b10fec9bSStefan Weil /* Fields vlan and data must not be reordered or separated. */ 978f2e8d1fSaliguori unsigned char vlan[4]; 987c23b892Sbalrog unsigned char data[0x10000]; 997c23b892Sbalrog uint16_t size; 1008f2e8d1fSaliguori unsigned char vlan_needed; 1017d08c73eSEd Swierk via Qemu-devel unsigned char sum_needed; 1027d08c73eSEd Swierk via Qemu-devel bool cptse; 103093454e2SDmitry Fleytman e1000x_txd_props props; 104d62644b4SEd Swierk via Qemu-devel e1000x_txd_props tso_props; 1057c23b892Sbalrog uint16_t tso_frames; 1067c23b892Sbalrog } tx; 1077c23b892Sbalrog 1087c23b892Sbalrog struct { 10920f3e863SLeonid Bloch uint32_t val_in; /* shifted in from guest driver */ 1107c23b892Sbalrog uint16_t bitnum_in; 1117c23b892Sbalrog uint16_t bitnum_out; 1127c23b892Sbalrog uint16_t reading; 1137c23b892Sbalrog uint32_t old_eecd; 1147c23b892Sbalrog } eecd_state; 115b9d03e35SJason Wang 116b9d03e35SJason Wang QEMUTimer *autoneg_timer; 1172af234e6SMichael S. Tsirkin 118e9845f09SVincenzo Maffione QEMUTimer *mit_timer; /* Mitigation timer. */ 119e9845f09SVincenzo Maffione bool mit_timer_on; /* Mitigation timer is running. */ 120e9845f09SVincenzo Maffione bool mit_irq_level; /* Tracks interrupt pin level. */ 121e9845f09SVincenzo Maffione uint32_t mit_ide; /* Tracks E1000_TXD_CMD_IDE bit. */ 122e9845f09SVincenzo Maffione 123*157628d0Syuchenlin QEMUTimer *flush_queue_timer; 124*157628d0Syuchenlin 1252af234e6SMichael S. Tsirkin /* Compatibility flags for migration to/from qemu 1.3.0 and older */ 1262af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG_BIT 0 127e9845f09SVincenzo Maffione #define E1000_FLAG_MIT_BIT 1 1289e117734SLeonid Bloch #define E1000_FLAG_MAC_BIT 2 12946f2a9ecSDr. David Alan Gilbert #define E1000_FLAG_TSO_BIT 3 1302af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG (1 << E1000_FLAG_AUTONEG_BIT) 131e9845f09SVincenzo Maffione #define E1000_FLAG_MIT (1 << E1000_FLAG_MIT_BIT) 1329e117734SLeonid Bloch #define E1000_FLAG_MAC (1 << E1000_FLAG_MAC_BIT) 13346f2a9ecSDr. David Alan Gilbert #define E1000_FLAG_TSO (1 << E1000_FLAG_TSO_BIT) 1342af234e6SMichael S. Tsirkin uint32_t compat_flags; 1353c4053c5SDr. David Alan Gilbert bool received_tx_tso; 136ff214d42SDr. David Alan Gilbert bool use_tso_for_migration; 13759354484SDr. David Alan Gilbert e1000x_txd_props mig_props; 1387c23b892Sbalrog } E1000State; 1397c23b892Sbalrog 140bc0f0674SLeonid Bloch #define chkflag(x) (s->compat_flags & E1000_FLAG_##x) 141bc0f0674SLeonid Bloch 1428597f2e1SGabriel L. Somlo typedef struct E1000BaseClass { 1438597f2e1SGabriel L. Somlo PCIDeviceClass parent_class; 1448597f2e1SGabriel L. Somlo uint16_t phy_id2; 1458597f2e1SGabriel L. Somlo } E1000BaseClass; 1468597f2e1SGabriel L. Somlo 1478597f2e1SGabriel L. Somlo #define TYPE_E1000_BASE "e1000-base" 148567a3c9eSPeter Crosthwaite 149567a3c9eSPeter Crosthwaite #define E1000(obj) \ 1508597f2e1SGabriel L. Somlo OBJECT_CHECK(E1000State, (obj), TYPE_E1000_BASE) 1518597f2e1SGabriel L. Somlo 1528597f2e1SGabriel L. Somlo #define E1000_DEVICE_CLASS(klass) \ 1538597f2e1SGabriel L. Somlo OBJECT_CLASS_CHECK(E1000BaseClass, (klass), TYPE_E1000_BASE) 1548597f2e1SGabriel L. Somlo #define E1000_DEVICE_GET_CLASS(obj) \ 1558597f2e1SGabriel L. Somlo OBJECT_GET_CLASS(E1000BaseClass, (obj), TYPE_E1000_BASE) 156567a3c9eSPeter Crosthwaite 15771aadd3cSJason Wang static void 15871aadd3cSJason Wang e1000_link_up(E1000State *s) 15971aadd3cSJason Wang { 160093454e2SDmitry Fleytman e1000x_update_regs_on_link_up(s->mac_reg, s->phy_reg); 161093454e2SDmitry Fleytman 162093454e2SDmitry Fleytman /* E1000_STATUS_LU is tested by e1000_can_receive() */ 163093454e2SDmitry Fleytman qemu_flush_queued_packets(qemu_get_queue(s->nic)); 164093454e2SDmitry Fleytman } 165093454e2SDmitry Fleytman 166093454e2SDmitry Fleytman static void 167093454e2SDmitry Fleytman e1000_autoneg_done(E1000State *s) 168093454e2SDmitry Fleytman { 169093454e2SDmitry Fleytman e1000x_update_regs_on_autoneg_done(s->mac_reg, s->phy_reg); 1705df6a185SStefan Hajnoczi 1715df6a185SStefan Hajnoczi /* E1000_STATUS_LU is tested by e1000_can_receive() */ 1725df6a185SStefan Hajnoczi qemu_flush_queued_packets(qemu_get_queue(s->nic)); 17371aadd3cSJason Wang } 17471aadd3cSJason Wang 1751195fed9SGabriel L. Somlo static bool 1761195fed9SGabriel L. Somlo have_autoneg(E1000State *s) 1771195fed9SGabriel L. Somlo { 178bc0f0674SLeonid Bloch return chkflag(AUTONEG) && (s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN); 1791195fed9SGabriel L. Somlo } 1801195fed9SGabriel L. Somlo 181b9d03e35SJason Wang static void 182b9d03e35SJason Wang set_phy_ctrl(E1000State *s, int index, uint16_t val) 183b9d03e35SJason Wang { 1841195fed9SGabriel L. Somlo /* bits 0-5 reserved; MII_CR_[RESTART_AUTO_NEG,RESET] are self clearing */ 1851195fed9SGabriel L. Somlo s->phy_reg[PHY_CTRL] = val & ~(0x3f | 1861195fed9SGabriel L. Somlo MII_CR_RESET | 1871195fed9SGabriel L. Somlo MII_CR_RESTART_AUTO_NEG); 1881195fed9SGabriel L. Somlo 1892af234e6SMichael S. Tsirkin /* 1902af234e6SMichael S. Tsirkin * QEMU 1.3 does not support link auto-negotiation emulation, so if we 1912af234e6SMichael S. Tsirkin * migrate during auto negotiation, after migration the link will be 1922af234e6SMichael S. Tsirkin * down. 1932af234e6SMichael S. Tsirkin */ 1941195fed9SGabriel L. Somlo if (have_autoneg(s) && (val & MII_CR_RESTART_AUTO_NEG)) { 195093454e2SDmitry Fleytman e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer); 196b9d03e35SJason Wang } 197b9d03e35SJason Wang } 198b9d03e35SJason Wang 199b9d03e35SJason Wang static void (*phyreg_writeops[])(E1000State *, int, uint16_t) = { 200b9d03e35SJason Wang [PHY_CTRL] = set_phy_ctrl, 201b9d03e35SJason Wang }; 202b9d03e35SJason Wang 203b9d03e35SJason Wang enum { NPHYWRITEOPS = ARRAY_SIZE(phyreg_writeops) }; 204b9d03e35SJason Wang 2057c23b892Sbalrog enum { PHY_R = 1, PHY_W = 2, PHY_RW = PHY_R | PHY_W }; 20688b4e9dbSblueswir1 static const char phy_regcap[0x20] = { 2077c23b892Sbalrog [PHY_STATUS] = PHY_R, [M88E1000_EXT_PHY_SPEC_CTRL] = PHY_RW, 2087c23b892Sbalrog [PHY_ID1] = PHY_R, [M88E1000_PHY_SPEC_CTRL] = PHY_RW, 2097c23b892Sbalrog [PHY_CTRL] = PHY_RW, [PHY_1000T_CTRL] = PHY_RW, 2107c23b892Sbalrog [PHY_LP_ABILITY] = PHY_R, [PHY_1000T_STATUS] = PHY_R, 2117c23b892Sbalrog [PHY_AUTONEG_ADV] = PHY_RW, [M88E1000_RX_ERR_CNTR] = PHY_R, 2126883b591SGabriel L. Somlo [PHY_ID2] = PHY_R, [M88E1000_PHY_SPEC_STATUS] = PHY_R, 2136883b591SGabriel L. Somlo [PHY_AUTONEG_EXP] = PHY_R, 2147c23b892Sbalrog }; 2157c23b892Sbalrog 2168597f2e1SGabriel L. Somlo /* PHY_ID2 documented in 8254x_GBe_SDM.pdf, pp. 250 */ 217814cd3acSMichael S. Tsirkin static const uint16_t phy_reg_init[] = { 2189616c290SGabriel L. Somlo [PHY_CTRL] = MII_CR_SPEED_SELECT_MSB | 2199616c290SGabriel L. Somlo MII_CR_FULL_DUPLEX | 2209616c290SGabriel L. Somlo MII_CR_AUTO_NEG_EN, 2219616c290SGabriel L. Somlo 2229616c290SGabriel L. Somlo [PHY_STATUS] = MII_SR_EXTENDED_CAPS | 2239616c290SGabriel L. Somlo MII_SR_LINK_STATUS | /* link initially up */ 2249616c290SGabriel L. Somlo MII_SR_AUTONEG_CAPS | 2259616c290SGabriel L. Somlo /* MII_SR_AUTONEG_COMPLETE: initially NOT completed */ 2269616c290SGabriel L. Somlo MII_SR_PREAMBLE_SUPPRESS | 2279616c290SGabriel L. Somlo MII_SR_EXTENDED_STATUS | 2289616c290SGabriel L. Somlo MII_SR_10T_HD_CAPS | 2299616c290SGabriel L. Somlo MII_SR_10T_FD_CAPS | 2309616c290SGabriel L. Somlo MII_SR_100X_HD_CAPS | 2319616c290SGabriel L. Somlo MII_SR_100X_FD_CAPS, 2329616c290SGabriel L. Somlo 2339616c290SGabriel L. Somlo [PHY_ID1] = 0x141, 2349616c290SGabriel L. Somlo /* [PHY_ID2] configured per DevId, from e1000_reset() */ 2359616c290SGabriel L. Somlo [PHY_AUTONEG_ADV] = 0xde1, 2369616c290SGabriel L. Somlo [PHY_LP_ABILITY] = 0x1e0, 2379616c290SGabriel L. Somlo [PHY_1000T_CTRL] = 0x0e00, 2389616c290SGabriel L. Somlo [PHY_1000T_STATUS] = 0x3c00, 2399616c290SGabriel L. Somlo [M88E1000_PHY_SPEC_CTRL] = 0x360, 240814cd3acSMichael S. Tsirkin [M88E1000_PHY_SPEC_STATUS] = 0xac00, 2419616c290SGabriel L. Somlo [M88E1000_EXT_PHY_SPEC_CTRL] = 0x0d60, 242814cd3acSMichael S. Tsirkin }; 243814cd3acSMichael S. Tsirkin 244814cd3acSMichael S. Tsirkin static const uint32_t mac_reg_init[] = { 245814cd3acSMichael S. Tsirkin [PBA] = 0x00100030, 246814cd3acSMichael S. Tsirkin [LEDCTL] = 0x602, 247814cd3acSMichael S. Tsirkin [CTRL] = E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN0 | 248814cd3acSMichael S. Tsirkin E1000_CTRL_SPD_1000 | E1000_CTRL_SLU, 249814cd3acSMichael S. Tsirkin [STATUS] = 0x80000000 | E1000_STATUS_GIO_MASTER_ENABLE | 250814cd3acSMichael S. Tsirkin E1000_STATUS_ASDV | E1000_STATUS_MTXCKOK | 251814cd3acSMichael S. Tsirkin E1000_STATUS_SPEED_1000 | E1000_STATUS_FD | 252814cd3acSMichael S. Tsirkin E1000_STATUS_LU, 253814cd3acSMichael S. Tsirkin [MANC] = E1000_MANC_EN_MNG2HOST | E1000_MANC_RCV_TCO_EN | 254814cd3acSMichael S. Tsirkin E1000_MANC_ARP_EN | E1000_MANC_0298_EN | 255814cd3acSMichael S. Tsirkin E1000_MANC_RMCP_EN, 256814cd3acSMichael S. Tsirkin }; 257814cd3acSMichael S. Tsirkin 258e9845f09SVincenzo Maffione /* Helper function, *curr == 0 means the value is not set */ 259e9845f09SVincenzo Maffione static inline void 260e9845f09SVincenzo Maffione mit_update_delay(uint32_t *curr, uint32_t value) 261e9845f09SVincenzo Maffione { 262e9845f09SVincenzo Maffione if (value && (*curr == 0 || value < *curr)) { 263e9845f09SVincenzo Maffione *curr = value; 264e9845f09SVincenzo Maffione } 265e9845f09SVincenzo Maffione } 266e9845f09SVincenzo Maffione 2677c23b892Sbalrog static void 2687c23b892Sbalrog set_interrupt_cause(E1000State *s, int index, uint32_t val) 2697c23b892Sbalrog { 270b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 271e9845f09SVincenzo Maffione uint32_t pending_ints; 272e9845f09SVincenzo Maffione uint32_t mit_delay; 273b08340d5SAndreas Färber 2747c23b892Sbalrog s->mac_reg[ICR] = val; 275a52a8841SMichael S. Tsirkin 276a52a8841SMichael S. Tsirkin /* 277a52a8841SMichael S. Tsirkin * Make sure ICR and ICS registers have the same value. 278a52a8841SMichael S. Tsirkin * The spec says that the ICS register is write-only. However in practice, 279a52a8841SMichael S. Tsirkin * on real hardware ICS is readable, and for reads it has the same value as 280a52a8841SMichael S. Tsirkin * ICR (except that ICS does not have the clear on read behaviour of ICR). 281a52a8841SMichael S. Tsirkin * 282a52a8841SMichael S. Tsirkin * The VxWorks PRO/1000 driver uses this behaviour. 283a52a8841SMichael S. Tsirkin */ 284b1332393SBill Paul s->mac_reg[ICS] = val; 285a52a8841SMichael S. Tsirkin 286e9845f09SVincenzo Maffione pending_ints = (s->mac_reg[IMS] & s->mac_reg[ICR]); 287e9845f09SVincenzo Maffione if (!s->mit_irq_level && pending_ints) { 288e9845f09SVincenzo Maffione /* 289e9845f09SVincenzo Maffione * Here we detect a potential raising edge. We postpone raising the 290e9845f09SVincenzo Maffione * interrupt line if we are inside the mitigation delay window 291e9845f09SVincenzo Maffione * (s->mit_timer_on == 1). 292e9845f09SVincenzo Maffione * We provide a partial implementation of interrupt mitigation, 293e9845f09SVincenzo Maffione * emulating only RADV, TADV and ITR (lower 16 bits, 1024ns units for 294e9845f09SVincenzo Maffione * RADV and TADV, 256ns units for ITR). RDTR is only used to enable 295e9845f09SVincenzo Maffione * RADV; relative timers based on TIDV and RDTR are not implemented. 296e9845f09SVincenzo Maffione */ 297e9845f09SVincenzo Maffione if (s->mit_timer_on) { 298e9845f09SVincenzo Maffione return; 299e9845f09SVincenzo Maffione } 300bc0f0674SLeonid Bloch if (chkflag(MIT)) { 301e9845f09SVincenzo Maffione /* Compute the next mitigation delay according to pending 302e9845f09SVincenzo Maffione * interrupts and the current values of RADV (provided 303e9845f09SVincenzo Maffione * RDTR!=0), TADV and ITR. 304e9845f09SVincenzo Maffione * Then rearm the timer. 305e9845f09SVincenzo Maffione */ 306e9845f09SVincenzo Maffione mit_delay = 0; 307e9845f09SVincenzo Maffione if (s->mit_ide && 308e9845f09SVincenzo Maffione (pending_ints & (E1000_ICR_TXQE | E1000_ICR_TXDW))) { 309e9845f09SVincenzo Maffione mit_update_delay(&mit_delay, s->mac_reg[TADV] * 4); 310e9845f09SVincenzo Maffione } 311e9845f09SVincenzo Maffione if (s->mac_reg[RDTR] && (pending_ints & E1000_ICS_RXT0)) { 312e9845f09SVincenzo Maffione mit_update_delay(&mit_delay, s->mac_reg[RADV] * 4); 313e9845f09SVincenzo Maffione } 314e9845f09SVincenzo Maffione mit_update_delay(&mit_delay, s->mac_reg[ITR]); 315e9845f09SVincenzo Maffione 31674004e8cSSameeh Jubran /* 31774004e8cSSameeh Jubran * According to e1000 SPEC, the Ethernet controller guarantees 31874004e8cSSameeh Jubran * a maximum observable interrupt rate of 7813 interrupts/sec. 31974004e8cSSameeh Jubran * Thus if mit_delay < 500 then the delay should be set to the 32074004e8cSSameeh Jubran * minimum delay possible which is 500. 32174004e8cSSameeh Jubran */ 32274004e8cSSameeh Jubran mit_delay = (mit_delay < 500) ? 500 : mit_delay; 32374004e8cSSameeh Jubran 324e9845f09SVincenzo Maffione s->mit_timer_on = 1; 325e9845f09SVincenzo Maffione timer_mod(s->mit_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 326e9845f09SVincenzo Maffione mit_delay * 256); 327e9845f09SVincenzo Maffione s->mit_ide = 0; 328e9845f09SVincenzo Maffione } 329e9845f09SVincenzo Maffione } 330e9845f09SVincenzo Maffione 331e9845f09SVincenzo Maffione s->mit_irq_level = (pending_ints != 0); 3329e64f8a3SMarcel Apfelbaum pci_set_irq(d, s->mit_irq_level); 333e9845f09SVincenzo Maffione } 334e9845f09SVincenzo Maffione 335e9845f09SVincenzo Maffione static void 336e9845f09SVincenzo Maffione e1000_mit_timer(void *opaque) 337e9845f09SVincenzo Maffione { 338e9845f09SVincenzo Maffione E1000State *s = opaque; 339e9845f09SVincenzo Maffione 340e9845f09SVincenzo Maffione s->mit_timer_on = 0; 341e9845f09SVincenzo Maffione /* Call set_interrupt_cause to update the irq level (if necessary). */ 342e9845f09SVincenzo Maffione set_interrupt_cause(s, 0, s->mac_reg[ICR]); 3437c23b892Sbalrog } 3447c23b892Sbalrog 3457c23b892Sbalrog static void 3467c23b892Sbalrog set_ics(E1000State *s, int index, uint32_t val) 3477c23b892Sbalrog { 3487c23b892Sbalrog DBGOUT(INTERRUPT, "set_ics %x, ICR %x, IMR %x\n", val, s->mac_reg[ICR], 3497c23b892Sbalrog s->mac_reg[IMS]); 3507c23b892Sbalrog set_interrupt_cause(s, 0, val | s->mac_reg[ICR]); 3517c23b892Sbalrog } 3527c23b892Sbalrog 353d52aec95SGabriel L. Somlo static void 354d52aec95SGabriel L. Somlo e1000_autoneg_timer(void *opaque) 355d52aec95SGabriel L. Somlo { 356d52aec95SGabriel L. Somlo E1000State *s = opaque; 357d52aec95SGabriel L. Somlo if (!qemu_get_queue(s->nic)->link_down) { 358093454e2SDmitry Fleytman e1000_autoneg_done(s); 359d52aec95SGabriel L. Somlo set_ics(s, 0, E1000_ICS_LSC); /* signal link status change to guest */ 360d52aec95SGabriel L. Somlo } 361d52aec95SGabriel L. Somlo } 362d52aec95SGabriel L. Somlo 363814cd3acSMichael S. Tsirkin static void e1000_reset(void *opaque) 364814cd3acSMichael S. Tsirkin { 365814cd3acSMichael S. Tsirkin E1000State *d = opaque; 3668597f2e1SGabriel L. Somlo E1000BaseClass *edc = E1000_DEVICE_GET_CLASS(d); 367372254c6SGabriel L. Somlo uint8_t *macaddr = d->conf.macaddr.a; 368814cd3acSMichael S. Tsirkin 369bc72ad67SAlex Bligh timer_del(d->autoneg_timer); 370e9845f09SVincenzo Maffione timer_del(d->mit_timer); 371*157628d0Syuchenlin timer_del(d->flush_queue_timer); 372e9845f09SVincenzo Maffione d->mit_timer_on = 0; 373e9845f09SVincenzo Maffione d->mit_irq_level = 0; 374e9845f09SVincenzo Maffione d->mit_ide = 0; 375814cd3acSMichael S. Tsirkin memset(d->phy_reg, 0, sizeof d->phy_reg); 376814cd3acSMichael S. Tsirkin memmove(d->phy_reg, phy_reg_init, sizeof phy_reg_init); 3778597f2e1SGabriel L. Somlo d->phy_reg[PHY_ID2] = edc->phy_id2; 378814cd3acSMichael S. Tsirkin memset(d->mac_reg, 0, sizeof d->mac_reg); 379814cd3acSMichael S. Tsirkin memmove(d->mac_reg, mac_reg_init, sizeof mac_reg_init); 380814cd3acSMichael S. Tsirkin d->rxbuf_min_shift = 1; 381814cd3acSMichael S. Tsirkin memset(&d->tx, 0, sizeof d->tx); 382814cd3acSMichael S. Tsirkin 383b356f76dSJason Wang if (qemu_get_queue(d->nic)->link_down) { 384093454e2SDmitry Fleytman e1000x_update_regs_on_link_down(d->mac_reg, d->phy_reg); 385814cd3acSMichael S. Tsirkin } 386372254c6SGabriel L. Somlo 387093454e2SDmitry Fleytman e1000x_reset_mac_addr(d->nic, d->mac_reg, macaddr); 388814cd3acSMichael S. Tsirkin } 389814cd3acSMichael S. Tsirkin 3907c23b892Sbalrog static void 391cab3c825SKevin Wolf set_ctrl(E1000State *s, int index, uint32_t val) 392cab3c825SKevin Wolf { 393cab3c825SKevin Wolf /* RST is self clearing */ 394cab3c825SKevin Wolf s->mac_reg[CTRL] = val & ~E1000_CTRL_RST; 395cab3c825SKevin Wolf } 396cab3c825SKevin Wolf 397cab3c825SKevin Wolf static void 398*157628d0Syuchenlin e1000_flush_queue_timer(void *opaque) 399*157628d0Syuchenlin { 400*157628d0Syuchenlin E1000State *s = opaque; 401*157628d0Syuchenlin 402*157628d0Syuchenlin qemu_flush_queued_packets(qemu_get_queue(s->nic)); 403*157628d0Syuchenlin } 404*157628d0Syuchenlin 405*157628d0Syuchenlin static void 4067c23b892Sbalrog set_rx_control(E1000State *s, int index, uint32_t val) 4077c23b892Sbalrog { 4087c23b892Sbalrog s->mac_reg[RCTL] = val; 409093454e2SDmitry Fleytman s->rxbuf_size = e1000x_rxbufsize(val); 4107c23b892Sbalrog s->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1; 4117c23b892Sbalrog DBGOUT(RX, "RCTL: %d, mac_reg[RCTL] = 0x%x\n", s->mac_reg[RDT], 4127c23b892Sbalrog s->mac_reg[RCTL]); 413*157628d0Syuchenlin timer_mod(s->flush_queue_timer, 414*157628d0Syuchenlin qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1000); 4157c23b892Sbalrog } 4167c23b892Sbalrog 4177c23b892Sbalrog static void 4187c23b892Sbalrog set_mdic(E1000State *s, int index, uint32_t val) 4197c23b892Sbalrog { 4207c23b892Sbalrog uint32_t data = val & E1000_MDIC_DATA_MASK; 4217c23b892Sbalrog uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT); 4227c23b892Sbalrog 4237c23b892Sbalrog if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) // phy # 4247c23b892Sbalrog val = s->mac_reg[MDIC] | E1000_MDIC_ERROR; 4257c23b892Sbalrog else if (val & E1000_MDIC_OP_READ) { 4267c23b892Sbalrog DBGOUT(MDIC, "MDIC read reg 0x%x\n", addr); 4277c23b892Sbalrog if (!(phy_regcap[addr] & PHY_R)) { 4287c23b892Sbalrog DBGOUT(MDIC, "MDIC read reg %x unhandled\n", addr); 4297c23b892Sbalrog val |= E1000_MDIC_ERROR; 4307c23b892Sbalrog } else 4317c23b892Sbalrog val = (val ^ data) | s->phy_reg[addr]; 4327c23b892Sbalrog } else if (val & E1000_MDIC_OP_WRITE) { 4337c23b892Sbalrog DBGOUT(MDIC, "MDIC write reg 0x%x, value 0x%x\n", addr, data); 4347c23b892Sbalrog if (!(phy_regcap[addr] & PHY_W)) { 4357c23b892Sbalrog DBGOUT(MDIC, "MDIC write reg %x unhandled\n", addr); 4367c23b892Sbalrog val |= E1000_MDIC_ERROR; 437b9d03e35SJason Wang } else { 438b9d03e35SJason Wang if (addr < NPHYWRITEOPS && phyreg_writeops[addr]) { 439b9d03e35SJason Wang phyreg_writeops[addr](s, index, data); 4401195fed9SGabriel L. Somlo } else { 4417c23b892Sbalrog s->phy_reg[addr] = data; 4427c23b892Sbalrog } 443b9d03e35SJason Wang } 4441195fed9SGabriel L. Somlo } 4457c23b892Sbalrog s->mac_reg[MDIC] = val | E1000_MDIC_READY; 44617fbbb0bSJason Wang 44717fbbb0bSJason Wang if (val & E1000_MDIC_INT_EN) { 4487c23b892Sbalrog set_ics(s, 0, E1000_ICR_MDAC); 4497c23b892Sbalrog } 45017fbbb0bSJason Wang } 4517c23b892Sbalrog 4527c23b892Sbalrog static uint32_t 4537c23b892Sbalrog get_eecd(E1000State *s, int index) 4547c23b892Sbalrog { 4557c23b892Sbalrog uint32_t ret = E1000_EECD_PRES|E1000_EECD_GNT | s->eecd_state.old_eecd; 4567c23b892Sbalrog 4577c23b892Sbalrog DBGOUT(EEPROM, "reading eeprom bit %d (reading %d)\n", 4587c23b892Sbalrog s->eecd_state.bitnum_out, s->eecd_state.reading); 4597c23b892Sbalrog if (!s->eecd_state.reading || 4607c23b892Sbalrog ((s->eeprom_data[(s->eecd_state.bitnum_out >> 4) & 0x3f] >> 4617c23b892Sbalrog ((s->eecd_state.bitnum_out & 0xf) ^ 0xf))) & 1) 4627c23b892Sbalrog ret |= E1000_EECD_DO; 4637c23b892Sbalrog return ret; 4647c23b892Sbalrog } 4657c23b892Sbalrog 4667c23b892Sbalrog static void 4677c23b892Sbalrog set_eecd(E1000State *s, int index, uint32_t val) 4687c23b892Sbalrog { 4697c23b892Sbalrog uint32_t oldval = s->eecd_state.old_eecd; 4707c23b892Sbalrog 4717c23b892Sbalrog s->eecd_state.old_eecd = val & (E1000_EECD_SK | E1000_EECD_CS | 4727c23b892Sbalrog E1000_EECD_DI|E1000_EECD_FWE_MASK|E1000_EECD_REQ); 47320f3e863SLeonid Bloch if (!(E1000_EECD_CS & val)) { /* CS inactive; nothing to do */ 4749651ac55SIzumi Tsutsui return; 47520f3e863SLeonid Bloch } 47620f3e863SLeonid Bloch if (E1000_EECD_CS & (val ^ oldval)) { /* CS rise edge; reset state */ 4779651ac55SIzumi Tsutsui s->eecd_state.val_in = 0; 4789651ac55SIzumi Tsutsui s->eecd_state.bitnum_in = 0; 4799651ac55SIzumi Tsutsui s->eecd_state.bitnum_out = 0; 4809651ac55SIzumi Tsutsui s->eecd_state.reading = 0; 4819651ac55SIzumi Tsutsui } 48220f3e863SLeonid Bloch if (!(E1000_EECD_SK & (val ^ oldval))) { /* no clock edge */ 4837c23b892Sbalrog return; 48420f3e863SLeonid Bloch } 48520f3e863SLeonid Bloch if (!(E1000_EECD_SK & val)) { /* falling edge */ 4867c23b892Sbalrog s->eecd_state.bitnum_out++; 4877c23b892Sbalrog return; 4887c23b892Sbalrog } 4897c23b892Sbalrog s->eecd_state.val_in <<= 1; 4907c23b892Sbalrog if (val & E1000_EECD_DI) 4917c23b892Sbalrog s->eecd_state.val_in |= 1; 4927c23b892Sbalrog if (++s->eecd_state.bitnum_in == 9 && !s->eecd_state.reading) { 4937c23b892Sbalrog s->eecd_state.bitnum_out = ((s->eecd_state.val_in & 0x3f)<<4)-1; 4947c23b892Sbalrog s->eecd_state.reading = (((s->eecd_state.val_in >> 6) & 7) == 4957c23b892Sbalrog EEPROM_READ_OPCODE_MICROWIRE); 4967c23b892Sbalrog } 4977c23b892Sbalrog DBGOUT(EEPROM, "eeprom bitnum in %d out %d, reading %d\n", 4987c23b892Sbalrog s->eecd_state.bitnum_in, s->eecd_state.bitnum_out, 4997c23b892Sbalrog s->eecd_state.reading); 5007c23b892Sbalrog } 5017c23b892Sbalrog 5027c23b892Sbalrog static uint32_t 5037c23b892Sbalrog flash_eerd_read(E1000State *s, int x) 5047c23b892Sbalrog { 5057c23b892Sbalrog unsigned int index, r = s->mac_reg[EERD] & ~E1000_EEPROM_RW_REG_START; 5067c23b892Sbalrog 507b1332393SBill Paul if ((s->mac_reg[EERD] & E1000_EEPROM_RW_REG_START) == 0) 508b1332393SBill Paul return (s->mac_reg[EERD]); 509b1332393SBill Paul 5107c23b892Sbalrog if ((index = r >> E1000_EEPROM_RW_ADDR_SHIFT) > EEPROM_CHECKSUM_REG) 511b1332393SBill Paul return (E1000_EEPROM_RW_REG_DONE | r); 512b1332393SBill Paul 513b1332393SBill Paul return ((s->eeprom_data[index] << E1000_EEPROM_RW_REG_DATA) | 514b1332393SBill Paul E1000_EEPROM_RW_REG_DONE | r); 5157c23b892Sbalrog } 5167c23b892Sbalrog 5177c23b892Sbalrog static void 5187c23b892Sbalrog putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t css, uint32_t cse) 5197c23b892Sbalrog { 520c6a6a5e3Saliguori uint32_t sum; 521c6a6a5e3Saliguori 5227c23b892Sbalrog if (cse && cse < n) 5237c23b892Sbalrog n = cse + 1; 524c6a6a5e3Saliguori if (sloc < n-1) { 525c6a6a5e3Saliguori sum = net_checksum_add(n-css, data+css); 5260dacea92SEd Swierk stw_be_p(data + sloc, net_checksum_finish_nozero(sum)); 527c6a6a5e3Saliguori } 5287c23b892Sbalrog } 5297c23b892Sbalrog 5301f67f92cSLeonid Bloch static inline void 5313b274301SLeonid Bloch inc_tx_bcast_or_mcast_count(E1000State *s, const unsigned char *arr) 5323b274301SLeonid Bloch { 5333b274301SLeonid Bloch if (!memcmp(arr, bcast, sizeof bcast)) { 534093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, BPTC); 5353b274301SLeonid Bloch } else if (arr[0] & 1) { 536093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, MPTC); 5373b274301SLeonid Bloch } 5383b274301SLeonid Bloch } 5393b274301SLeonid Bloch 54045e93764SLeonid Bloch static void 54193e37d76SJason Wang e1000_send_packet(E1000State *s, const uint8_t *buf, int size) 54293e37d76SJason Wang { 5433b274301SLeonid Bloch static const int PTCregs[6] = { PTC64, PTC127, PTC255, PTC511, 5443b274301SLeonid Bloch PTC1023, PTC1522 }; 5453b274301SLeonid Bloch 546b356f76dSJason Wang NetClientState *nc = qemu_get_queue(s->nic); 54793e37d76SJason Wang if (s->phy_reg[PHY_CTRL] & MII_CR_LOOPBACK) { 548b356f76dSJason Wang nc->info->receive(nc, buf, size); 54993e37d76SJason Wang } else { 550b356f76dSJason Wang qemu_send_packet(nc, buf, size); 55193e37d76SJason Wang } 5523b274301SLeonid Bloch inc_tx_bcast_or_mcast_count(s, buf); 553093454e2SDmitry Fleytman e1000x_increase_size_stats(s->mac_reg, PTCregs, size); 55493e37d76SJason Wang } 55593e37d76SJason Wang 55693e37d76SJason Wang static void 5577c23b892Sbalrog xmit_seg(E1000State *s) 5587c23b892Sbalrog { 55914e60aaeSPeter Maydell uint16_t len; 56045e93764SLeonid Bloch unsigned int frames = s->tx.tso_frames, css, sofar; 5617c23b892Sbalrog struct e1000_tx *tp = &s->tx; 562d62644b4SEd Swierk via Qemu-devel struct e1000x_txd_props *props = tp->cptse ? &tp->tso_props : &tp->props; 5637c23b892Sbalrog 564d62644b4SEd Swierk via Qemu-devel if (tp->cptse) { 565d62644b4SEd Swierk via Qemu-devel css = props->ipcss; 5667c23b892Sbalrog DBGOUT(TXSUM, "frames %d size %d ipcss %d\n", 5677c23b892Sbalrog frames, tp->size, css); 568d62644b4SEd Swierk via Qemu-devel if (props->ip) { /* IPv4 */ 569d8ee2591SPeter Maydell stw_be_p(tp->data+css+2, tp->size - css); 570d8ee2591SPeter Maydell stw_be_p(tp->data+css+4, 57114e60aaeSPeter Maydell lduw_be_p(tp->data + css + 4) + frames); 57220f3e863SLeonid Bloch } else { /* IPv6 */ 573d8ee2591SPeter Maydell stw_be_p(tp->data+css+4, tp->size - css); 57420f3e863SLeonid Bloch } 575d62644b4SEd Swierk via Qemu-devel css = props->tucss; 5767c23b892Sbalrog len = tp->size - css; 577d62644b4SEd Swierk via Qemu-devel DBGOUT(TXSUM, "tcp %d tucss %d len %d\n", props->tcp, css, len); 578d62644b4SEd Swierk via Qemu-devel if (props->tcp) { 579d62644b4SEd Swierk via Qemu-devel sofar = frames * props->mss; 5806bd194abSPeter Maydell stl_be_p(tp->data+css+4, ldl_be_p(tp->data+css+4)+sofar); /* seq */ 581d62644b4SEd Swierk via Qemu-devel if (props->paylen - sofar > props->mss) { 58220f3e863SLeonid Bloch tp->data[css + 13] &= ~9; /* PSH, FIN */ 5833b274301SLeonid Bloch } else if (frames) { 584093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, TSCTC); 5853b274301SLeonid Bloch } 586d62644b4SEd Swierk via Qemu-devel } else { /* UDP */ 587d8ee2591SPeter Maydell stw_be_p(tp->data+css+4, len); 588d62644b4SEd Swierk via Qemu-devel } 5897d08c73eSEd Swierk via Qemu-devel if (tp->sum_needed & E1000_TXD_POPTS_TXSM) { 590e685b4ebSAlex Williamson unsigned int phsum; 5917c23b892Sbalrog // add pseudo-header length before checksum calculation 592d62644b4SEd Swierk via Qemu-devel void *sp = tp->data + props->tucso; 59314e60aaeSPeter Maydell 59414e60aaeSPeter Maydell phsum = lduw_be_p(sp) + len; 595e685b4ebSAlex Williamson phsum = (phsum >> 16) + (phsum & 0xffff); 596d8ee2591SPeter Maydell stw_be_p(sp, phsum); 5977c23b892Sbalrog } 5987c23b892Sbalrog tp->tso_frames++; 5997c23b892Sbalrog } 6007c23b892Sbalrog 6017d08c73eSEd Swierk via Qemu-devel if (tp->sum_needed & E1000_TXD_POPTS_TXSM) { 602d62644b4SEd Swierk via Qemu-devel putsum(tp->data, tp->size, props->tucso, props->tucss, props->tucse); 603093454e2SDmitry Fleytman } 6047d08c73eSEd Swierk via Qemu-devel if (tp->sum_needed & E1000_TXD_POPTS_IXSM) { 605d62644b4SEd Swierk via Qemu-devel putsum(tp->data, tp->size, props->ipcso, props->ipcss, props->ipcse); 606093454e2SDmitry Fleytman } 6078f2e8d1fSaliguori if (tp->vlan_needed) { 608b10fec9bSStefan Weil memmove(tp->vlan, tp->data, 4); 609b10fec9bSStefan Weil memmove(tp->data, tp->data + 4, 8); 6108f2e8d1fSaliguori memcpy(tp->data + 8, tp->vlan_header, 4); 61193e37d76SJason Wang e1000_send_packet(s, tp->vlan, tp->size + 4); 61220f3e863SLeonid Bloch } else { 61393e37d76SJason Wang e1000_send_packet(s, tp->data, tp->size); 61420f3e863SLeonid Bloch } 61520f3e863SLeonid Bloch 616093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, TPT); 617093454e2SDmitry Fleytman e1000x_grow_8reg_if_not_full(s->mac_reg, TOTL, s->tx.size); 6181f67f92cSLeonid Bloch s->mac_reg[GPTC] = s->mac_reg[TPT]; 6193b274301SLeonid Bloch s->mac_reg[GOTCL] = s->mac_reg[TOTL]; 6203b274301SLeonid Bloch s->mac_reg[GOTCH] = s->mac_reg[TOTH]; 6217c23b892Sbalrog } 6227c23b892Sbalrog 6237c23b892Sbalrog static void 6247c23b892Sbalrog process_tx_desc(E1000State *s, struct e1000_tx_desc *dp) 6257c23b892Sbalrog { 626b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 6277c23b892Sbalrog uint32_t txd_lower = le32_to_cpu(dp->lower.data); 6287c23b892Sbalrog uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D); 629093454e2SDmitry Fleytman unsigned int split_size = txd_lower & 0xffff, bytes, sz; 630a0ae17a6SAndrew Jones unsigned int msh = 0xfffff; 6317c23b892Sbalrog uint64_t addr; 6327c23b892Sbalrog struct e1000_context_desc *xp = (struct e1000_context_desc *)dp; 6337c23b892Sbalrog struct e1000_tx *tp = &s->tx; 6347c23b892Sbalrog 635e9845f09SVincenzo Maffione s->mit_ide |= (txd_lower & E1000_TXD_CMD_IDE); 63620f3e863SLeonid Bloch if (dtype == E1000_TXD_CMD_DEXT) { /* context descriptor */ 637d62644b4SEd Swierk via Qemu-devel if (le32_to_cpu(xp->cmd_and_length) & E1000_TXD_CMD_TSE) { 638d62644b4SEd Swierk via Qemu-devel e1000x_read_tx_ctx_descr(xp, &tp->tso_props); 639ff214d42SDr. David Alan Gilbert s->use_tso_for_migration = 1; 6407c23b892Sbalrog tp->tso_frames = 0; 641d62644b4SEd Swierk via Qemu-devel } else { 642d62644b4SEd Swierk via Qemu-devel e1000x_read_tx_ctx_descr(xp, &tp->props); 643ff214d42SDr. David Alan Gilbert s->use_tso_for_migration = 0; 6447c23b892Sbalrog } 6457c23b892Sbalrog return; 6461b0009dbSbalrog } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) { 6471b0009dbSbalrog // data descriptor 648735e77ecSStefan Hajnoczi if (tp->size == 0) { 6497d08c73eSEd Swierk via Qemu-devel tp->sum_needed = le32_to_cpu(dp->upper.data) >> 8; 650735e77ecSStefan Hajnoczi } 6517d08c73eSEd Swierk via Qemu-devel tp->cptse = (txd_lower & E1000_TXD_CMD_TSE) ? 1 : 0; 65243ad7e3eSJes Sorensen } else { 6531b0009dbSbalrog // legacy descriptor 6547d08c73eSEd Swierk via Qemu-devel tp->cptse = 0; 65543ad7e3eSJes Sorensen } 6567c23b892Sbalrog 657093454e2SDmitry Fleytman if (e1000x_vlan_enabled(s->mac_reg) && 658093454e2SDmitry Fleytman e1000x_is_vlan_txd(txd_lower) && 6597d08c73eSEd Swierk via Qemu-devel (tp->cptse || txd_lower & E1000_TXD_CMD_EOP)) { 6608f2e8d1fSaliguori tp->vlan_needed = 1; 661d8ee2591SPeter Maydell stw_be_p(tp->vlan_header, 6624e60a250SShannon Zhao le16_to_cpu(s->mac_reg[VET])); 663d8ee2591SPeter Maydell stw_be_p(tp->vlan_header + 2, 6648f2e8d1fSaliguori le16_to_cpu(dp->upper.fields.special)); 6658f2e8d1fSaliguori } 6668f2e8d1fSaliguori 6677c23b892Sbalrog addr = le64_to_cpu(dp->buffer_addr); 668d62644b4SEd Swierk via Qemu-devel if (tp->cptse) { 669d62644b4SEd Swierk via Qemu-devel msh = tp->tso_props.hdr_len + tp->tso_props.mss; 6707c23b892Sbalrog do { 6717c23b892Sbalrog bytes = split_size; 6727c23b892Sbalrog if (tp->size + bytes > msh) 6737c23b892Sbalrog bytes = msh - tp->size; 67465f82df0SAnthony Liguori 67565f82df0SAnthony Liguori bytes = MIN(sizeof(tp->data) - tp->size, bytes); 676b08340d5SAndreas Färber pci_dma_read(d, addr, tp->data + tp->size, bytes); 677a0ae17a6SAndrew Jones sz = tp->size + bytes; 678d62644b4SEd Swierk via Qemu-devel if (sz >= tp->tso_props.hdr_len 679d62644b4SEd Swierk via Qemu-devel && tp->size < tp->tso_props.hdr_len) { 680d62644b4SEd Swierk via Qemu-devel memmove(tp->header, tp->data, tp->tso_props.hdr_len); 681a0ae17a6SAndrew Jones } 6827c23b892Sbalrog tp->size = sz; 6837c23b892Sbalrog addr += bytes; 6847c23b892Sbalrog if (sz == msh) { 6857c23b892Sbalrog xmit_seg(s); 686d62644b4SEd Swierk via Qemu-devel memmove(tp->data, tp->header, tp->tso_props.hdr_len); 687d62644b4SEd Swierk via Qemu-devel tp->size = tp->tso_props.hdr_len; 6887c23b892Sbalrog } 689b947ac2bSP J P split_size -= bytes; 690b947ac2bSP J P } while (bytes && split_size); 6911b0009dbSbalrog } else { 69265f82df0SAnthony Liguori split_size = MIN(sizeof(tp->data) - tp->size, split_size); 693b08340d5SAndreas Färber pci_dma_read(d, addr, tp->data + tp->size, split_size); 6941b0009dbSbalrog tp->size += split_size; 6951b0009dbSbalrog } 6967c23b892Sbalrog 6977c23b892Sbalrog if (!(txd_lower & E1000_TXD_CMD_EOP)) 6987c23b892Sbalrog return; 699d62644b4SEd Swierk via Qemu-devel if (!(tp->cptse && tp->size < tp->tso_props.hdr_len)) { 7007c23b892Sbalrog xmit_seg(s); 701a0ae17a6SAndrew Jones } 7027c23b892Sbalrog tp->tso_frames = 0; 7037d08c73eSEd Swierk via Qemu-devel tp->sum_needed = 0; 7048f2e8d1fSaliguori tp->vlan_needed = 0; 7057c23b892Sbalrog tp->size = 0; 7067d08c73eSEd Swierk via Qemu-devel tp->cptse = 0; 7077c23b892Sbalrog } 7087c23b892Sbalrog 7097c23b892Sbalrog static uint32_t 71062ecbd35SEduard - Gabriel Munteanu txdesc_writeback(E1000State *s, dma_addr_t base, struct e1000_tx_desc *dp) 7117c23b892Sbalrog { 712b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 7137c23b892Sbalrog uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data); 7147c23b892Sbalrog 7157c23b892Sbalrog if (!(txd_lower & (E1000_TXD_CMD_RS|E1000_TXD_CMD_RPS))) 7167c23b892Sbalrog return 0; 7177c23b892Sbalrog txd_upper = (le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD) & 7187c23b892Sbalrog ~(E1000_TXD_STAT_EC | E1000_TXD_STAT_LC | E1000_TXD_STAT_TU); 7197c23b892Sbalrog dp->upper.data = cpu_to_le32(txd_upper); 720b08340d5SAndreas Färber pci_dma_write(d, base + ((char *)&dp->upper - (char *)dp), 72100c3a05bSDavid Gibson &dp->upper, sizeof(dp->upper)); 7227c23b892Sbalrog return E1000_ICR_TXDW; 7237c23b892Sbalrog } 7247c23b892Sbalrog 725d17161f6SKevin Wolf static uint64_t tx_desc_base(E1000State *s) 726d17161f6SKevin Wolf { 727d17161f6SKevin Wolf uint64_t bah = s->mac_reg[TDBAH]; 728d17161f6SKevin Wolf uint64_t bal = s->mac_reg[TDBAL] & ~0xf; 729d17161f6SKevin Wolf 730d17161f6SKevin Wolf return (bah << 32) + bal; 731d17161f6SKevin Wolf } 732d17161f6SKevin Wolf 7337c23b892Sbalrog static void 7347c23b892Sbalrog start_xmit(E1000State *s) 7357c23b892Sbalrog { 736b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 73762ecbd35SEduard - Gabriel Munteanu dma_addr_t base; 7387c23b892Sbalrog struct e1000_tx_desc desc; 7397c23b892Sbalrog uint32_t tdh_start = s->mac_reg[TDH], cause = E1000_ICS_TXQE; 7407c23b892Sbalrog 7417c23b892Sbalrog if (!(s->mac_reg[TCTL] & E1000_TCTL_EN)) { 7427c23b892Sbalrog DBGOUT(TX, "tx disabled\n"); 7437c23b892Sbalrog return; 7447c23b892Sbalrog } 7457c23b892Sbalrog 7467c23b892Sbalrog while (s->mac_reg[TDH] != s->mac_reg[TDT]) { 747d17161f6SKevin Wolf base = tx_desc_base(s) + 7487c23b892Sbalrog sizeof(struct e1000_tx_desc) * s->mac_reg[TDH]; 749b08340d5SAndreas Färber pci_dma_read(d, base, &desc, sizeof(desc)); 7507c23b892Sbalrog 7517c23b892Sbalrog DBGOUT(TX, "index %d: %p : %x %x\n", s->mac_reg[TDH], 7526106075bSths (void *)(intptr_t)desc.buffer_addr, desc.lower.data, 7537c23b892Sbalrog desc.upper.data); 7547c23b892Sbalrog 7557c23b892Sbalrog process_tx_desc(s, &desc); 75662ecbd35SEduard - Gabriel Munteanu cause |= txdesc_writeback(s, base, &desc); 7577c23b892Sbalrog 7587c23b892Sbalrog if (++s->mac_reg[TDH] * sizeof(desc) >= s->mac_reg[TDLEN]) 7597c23b892Sbalrog s->mac_reg[TDH] = 0; 7607c23b892Sbalrog /* 7617c23b892Sbalrog * the following could happen only if guest sw assigns 7627c23b892Sbalrog * bogus values to TDT/TDLEN. 7637c23b892Sbalrog * there's nothing too intelligent we could do about this. 7647c23b892Sbalrog */ 765dd793a74SLaszlo Ersek if (s->mac_reg[TDH] == tdh_start || 766dd793a74SLaszlo Ersek tdh_start >= s->mac_reg[TDLEN] / sizeof(desc)) { 7677c23b892Sbalrog DBGOUT(TXERR, "TDH wraparound @%x, TDT %x, TDLEN %x\n", 7687c23b892Sbalrog tdh_start, s->mac_reg[TDT], s->mac_reg[TDLEN]); 7697c23b892Sbalrog break; 7707c23b892Sbalrog } 7717c23b892Sbalrog } 7727c23b892Sbalrog set_ics(s, 0, cause); 7737c23b892Sbalrog } 7747c23b892Sbalrog 7757c23b892Sbalrog static int 7767c23b892Sbalrog receive_filter(E1000State *s, const uint8_t *buf, int size) 7777c23b892Sbalrog { 778093454e2SDmitry Fleytman uint32_t rctl = s->mac_reg[RCTL]; 7794aeea330SLeonid Bloch int isbcast = !memcmp(buf, bcast, sizeof bcast), ismcast = (buf[0] & 1); 7807c23b892Sbalrog 781093454e2SDmitry Fleytman if (e1000x_is_vlan_packet(buf, le16_to_cpu(s->mac_reg[VET])) && 782093454e2SDmitry Fleytman e1000x_vlan_rx_filter_enabled(s->mac_reg)) { 78314e60aaeSPeter Maydell uint16_t vid = lduw_be_p(buf + 14); 78414e60aaeSPeter Maydell uint32_t vfta = ldl_le_p((uint32_t*)(s->mac_reg + VFTA) + 7858f2e8d1fSaliguori ((vid >> 5) & 0x7f)); 7868f2e8d1fSaliguori if ((vfta & (1 << (vid & 0x1f))) == 0) 7878f2e8d1fSaliguori return 0; 7888f2e8d1fSaliguori } 7898f2e8d1fSaliguori 7904aeea330SLeonid Bloch if (!isbcast && !ismcast && (rctl & E1000_RCTL_UPE)) { /* promiscuous ucast */ 7917c23b892Sbalrog return 1; 7924aeea330SLeonid Bloch } 7937c23b892Sbalrog 7944aeea330SLeonid Bloch if (ismcast && (rctl & E1000_RCTL_MPE)) { /* promiscuous mcast */ 795093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, MPRC); 7967c23b892Sbalrog return 1; 7974aeea330SLeonid Bloch } 7987c23b892Sbalrog 7994aeea330SLeonid Bloch if (isbcast && (rctl & E1000_RCTL_BAM)) { /* broadcast enabled */ 800093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, BPRC); 8017c23b892Sbalrog return 1; 8024aeea330SLeonid Bloch } 8037c23b892Sbalrog 804093454e2SDmitry Fleytman return e1000x_rx_group_filter(s->mac_reg, buf); 8057c23b892Sbalrog } 8067c23b892Sbalrog 80799ed7e30Saliguori static void 8084e68f7a0SStefan Hajnoczi e1000_set_link_status(NetClientState *nc) 80999ed7e30Saliguori { 810cc1f0f45SJason Wang E1000State *s = qemu_get_nic_opaque(nc); 81199ed7e30Saliguori uint32_t old_status = s->mac_reg[STATUS]; 81299ed7e30Saliguori 813d4044c2aSBjørn Mork if (nc->link_down) { 814093454e2SDmitry Fleytman e1000x_update_regs_on_link_down(s->mac_reg, s->phy_reg); 815d4044c2aSBjørn Mork } else { 816d7a41552SGabriel L. Somlo if (have_autoneg(s) && 8176a2acedbSGabriel L. Somlo !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) { 818093454e2SDmitry Fleytman e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer); 8196a2acedbSGabriel L. Somlo } else { 82071aadd3cSJason Wang e1000_link_up(s); 821d4044c2aSBjørn Mork } 8226a2acedbSGabriel L. Somlo } 82399ed7e30Saliguori 82499ed7e30Saliguori if (s->mac_reg[STATUS] != old_status) 82599ed7e30Saliguori set_ics(s, 0, E1000_ICR_LSC); 82699ed7e30Saliguori } 82799ed7e30Saliguori 828322fd48aSMichael S. Tsirkin static bool e1000_has_rxbufs(E1000State *s, size_t total_size) 829322fd48aSMichael S. Tsirkin { 830322fd48aSMichael S. Tsirkin int bufs; 831322fd48aSMichael S. Tsirkin /* Fast-path short packets */ 832322fd48aSMichael S. Tsirkin if (total_size <= s->rxbuf_size) { 833e5b8b0d4SDmitry Fleytman return s->mac_reg[RDH] != s->mac_reg[RDT]; 834322fd48aSMichael S. Tsirkin } 835322fd48aSMichael S. Tsirkin if (s->mac_reg[RDH] < s->mac_reg[RDT]) { 836322fd48aSMichael S. Tsirkin bufs = s->mac_reg[RDT] - s->mac_reg[RDH]; 837e5b8b0d4SDmitry Fleytman } else if (s->mac_reg[RDH] > s->mac_reg[RDT]) { 838322fd48aSMichael S. Tsirkin bufs = s->mac_reg[RDLEN] / sizeof(struct e1000_rx_desc) + 839322fd48aSMichael S. Tsirkin s->mac_reg[RDT] - s->mac_reg[RDH]; 840322fd48aSMichael S. Tsirkin } else { 841322fd48aSMichael S. Tsirkin return false; 842322fd48aSMichael S. Tsirkin } 843322fd48aSMichael S. Tsirkin return total_size <= bufs * s->rxbuf_size; 844322fd48aSMichael S. Tsirkin } 845322fd48aSMichael S. Tsirkin 8466cdfab28SMichael S. Tsirkin static int 8474e68f7a0SStefan Hajnoczi e1000_can_receive(NetClientState *nc) 8486cdfab28SMichael S. Tsirkin { 849cc1f0f45SJason Wang E1000State *s = qemu_get_nic_opaque(nc); 8506cdfab28SMichael S. Tsirkin 851093454e2SDmitry Fleytman return e1000x_rx_ready(&s->parent_obj, s->mac_reg) && 852*157628d0Syuchenlin e1000_has_rxbufs(s, 1) && !timer_pending(s->flush_queue_timer); 8536cdfab28SMichael S. Tsirkin } 8546cdfab28SMichael S. Tsirkin 855d17161f6SKevin Wolf static uint64_t rx_desc_base(E1000State *s) 856d17161f6SKevin Wolf { 857d17161f6SKevin Wolf uint64_t bah = s->mac_reg[RDBAH]; 858d17161f6SKevin Wolf uint64_t bal = s->mac_reg[RDBAL] & ~0xf; 859d17161f6SKevin Wolf 860d17161f6SKevin Wolf return (bah << 32) + bal; 861d17161f6SKevin Wolf } 862d17161f6SKevin Wolf 8631001cf45SJason Wang static void 8641001cf45SJason Wang e1000_receiver_overrun(E1000State *s, size_t size) 8651001cf45SJason Wang { 8661001cf45SJason Wang trace_e1000_receiver_overrun(size, s->mac_reg[RDH], s->mac_reg[RDT]); 8671001cf45SJason Wang e1000x_inc_reg_if_not_full(s->mac_reg, RNBC); 8681001cf45SJason Wang e1000x_inc_reg_if_not_full(s->mac_reg, MPC); 8691001cf45SJason Wang set_ics(s, 0, E1000_ICS_RXO); 8701001cf45SJason Wang } 8711001cf45SJason Wang 8724f1c942bSMark McLoughlin static ssize_t 87397410ddeSVincenzo Maffione e1000_receive_iov(NetClientState *nc, const struct iovec *iov, int iovcnt) 8747c23b892Sbalrog { 875cc1f0f45SJason Wang E1000State *s = qemu_get_nic_opaque(nc); 876b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 8777c23b892Sbalrog struct e1000_rx_desc desc; 87862ecbd35SEduard - Gabriel Munteanu dma_addr_t base; 8797c23b892Sbalrog unsigned int n, rdt; 8807c23b892Sbalrog uint32_t rdh_start; 8818f2e8d1fSaliguori uint16_t vlan_special = 0; 88297410ddeSVincenzo Maffione uint8_t vlan_status = 0; 88378aeb23eSStefan Hajnoczi uint8_t min_buf[MIN_BUF_SIZE]; 88497410ddeSVincenzo Maffione struct iovec min_iov; 88597410ddeSVincenzo Maffione uint8_t *filter_buf = iov->iov_base; 88697410ddeSVincenzo Maffione size_t size = iov_size(iov, iovcnt); 88797410ddeSVincenzo Maffione size_t iov_ofs = 0; 888b19487e2SMichael S. Tsirkin size_t desc_offset; 889b19487e2SMichael S. Tsirkin size_t desc_size; 890b19487e2SMichael S. Tsirkin size_t total_size; 8917c23b892Sbalrog 892093454e2SDmitry Fleytman if (!e1000x_hw_rx_enabled(s->mac_reg)) { 893ddcb73b7SMichael S. Tsirkin return -1; 894ddcb73b7SMichael S. Tsirkin } 8957c23b892Sbalrog 896*157628d0Syuchenlin if (timer_pending(s->flush_queue_timer)) { 897*157628d0Syuchenlin return 0; 898*157628d0Syuchenlin } 899*157628d0Syuchenlin 90078aeb23eSStefan Hajnoczi /* Pad to minimum Ethernet frame length */ 90178aeb23eSStefan Hajnoczi if (size < sizeof(min_buf)) { 90297410ddeSVincenzo Maffione iov_to_buf(iov, iovcnt, 0, min_buf, size); 90378aeb23eSStefan Hajnoczi memset(&min_buf[size], 0, sizeof(min_buf) - size); 904093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, RUC); 90597410ddeSVincenzo Maffione min_iov.iov_base = filter_buf = min_buf; 90697410ddeSVincenzo Maffione min_iov.iov_len = size = sizeof(min_buf); 90797410ddeSVincenzo Maffione iovcnt = 1; 90897410ddeSVincenzo Maffione iov = &min_iov; 90997410ddeSVincenzo Maffione } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) { 91097410ddeSVincenzo Maffione /* This is very unlikely, but may happen. */ 91197410ddeSVincenzo Maffione iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN); 91297410ddeSVincenzo Maffione filter_buf = min_buf; 91378aeb23eSStefan Hajnoczi } 91478aeb23eSStefan Hajnoczi 915b0d9ffcdSMichael Contreras /* Discard oversized packets if !LPE and !SBP. */ 916093454e2SDmitry Fleytman if (e1000x_is_oversized(s->mac_reg, size)) { 917b0d9ffcdSMichael Contreras return size; 918b0d9ffcdSMichael Contreras } 919b0d9ffcdSMichael Contreras 92097410ddeSVincenzo Maffione if (!receive_filter(s, filter_buf, size)) { 9214f1c942bSMark McLoughlin return size; 92297410ddeSVincenzo Maffione } 9237c23b892Sbalrog 924093454e2SDmitry Fleytman if (e1000x_vlan_enabled(s->mac_reg) && 925093454e2SDmitry Fleytman e1000x_is_vlan_packet(filter_buf, le16_to_cpu(s->mac_reg[VET]))) { 92614e60aaeSPeter Maydell vlan_special = cpu_to_le16(lduw_be_p(filter_buf + 14)); 92797410ddeSVincenzo Maffione iov_ofs = 4; 92897410ddeSVincenzo Maffione if (filter_buf == iov->iov_base) { 92997410ddeSVincenzo Maffione memmove(filter_buf + 4, filter_buf, 12); 93097410ddeSVincenzo Maffione } else { 93197410ddeSVincenzo Maffione iov_from_buf(iov, iovcnt, 4, filter_buf, 12); 93297410ddeSVincenzo Maffione while (iov->iov_len <= iov_ofs) { 93397410ddeSVincenzo Maffione iov_ofs -= iov->iov_len; 93497410ddeSVincenzo Maffione iov++; 93597410ddeSVincenzo Maffione } 93697410ddeSVincenzo Maffione } 9378f2e8d1fSaliguori vlan_status = E1000_RXD_STAT_VP; 9388f2e8d1fSaliguori size -= 4; 9398f2e8d1fSaliguori } 9408f2e8d1fSaliguori 9417c23b892Sbalrog rdh_start = s->mac_reg[RDH]; 942b19487e2SMichael S. Tsirkin desc_offset = 0; 943093454e2SDmitry Fleytman total_size = size + e1000x_fcs_len(s->mac_reg); 944322fd48aSMichael S. Tsirkin if (!e1000_has_rxbufs(s, total_size)) { 9451001cf45SJason Wang e1000_receiver_overrun(s, total_size); 946322fd48aSMichael S. Tsirkin return -1; 947322fd48aSMichael S. Tsirkin } 9487c23b892Sbalrog do { 949b19487e2SMichael S. Tsirkin desc_size = total_size - desc_offset; 950b19487e2SMichael S. Tsirkin if (desc_size > s->rxbuf_size) { 951b19487e2SMichael S. Tsirkin desc_size = s->rxbuf_size; 952b19487e2SMichael S. Tsirkin } 953d17161f6SKevin Wolf base = rx_desc_base(s) + sizeof(desc) * s->mac_reg[RDH]; 954b08340d5SAndreas Färber pci_dma_read(d, base, &desc, sizeof(desc)); 9558f2e8d1fSaliguori desc.special = vlan_special; 9568f2e8d1fSaliguori desc.status |= (vlan_status | E1000_RXD_STAT_DD); 9577c23b892Sbalrog if (desc.buffer_addr) { 958b19487e2SMichael S. Tsirkin if (desc_offset < size) { 95997410ddeSVincenzo Maffione size_t iov_copy; 96097410ddeSVincenzo Maffione hwaddr ba = le64_to_cpu(desc.buffer_addr); 961b19487e2SMichael S. Tsirkin size_t copy_size = size - desc_offset; 962b19487e2SMichael S. Tsirkin if (copy_size > s->rxbuf_size) { 963b19487e2SMichael S. Tsirkin copy_size = s->rxbuf_size; 964b19487e2SMichael S. Tsirkin } 96597410ddeSVincenzo Maffione do { 96697410ddeSVincenzo Maffione iov_copy = MIN(copy_size, iov->iov_len - iov_ofs); 96797410ddeSVincenzo Maffione pci_dma_write(d, ba, iov->iov_base + iov_ofs, iov_copy); 96897410ddeSVincenzo Maffione copy_size -= iov_copy; 96997410ddeSVincenzo Maffione ba += iov_copy; 97097410ddeSVincenzo Maffione iov_ofs += iov_copy; 97197410ddeSVincenzo Maffione if (iov_ofs == iov->iov_len) { 97297410ddeSVincenzo Maffione iov++; 97397410ddeSVincenzo Maffione iov_ofs = 0; 97497410ddeSVincenzo Maffione } 97597410ddeSVincenzo Maffione } while (copy_size); 976b19487e2SMichael S. Tsirkin } 977b19487e2SMichael S. Tsirkin desc_offset += desc_size; 978b19487e2SMichael S. Tsirkin desc.length = cpu_to_le16(desc_size); 979ee912ccfSMichael S. Tsirkin if (desc_offset >= total_size) { 9807c23b892Sbalrog desc.status |= E1000_RXD_STAT_EOP | E1000_RXD_STAT_IXSM; 981b19487e2SMichael S. Tsirkin } else { 982ee912ccfSMichael S. Tsirkin /* Guest zeroing out status is not a hardware requirement. 983ee912ccfSMichael S. Tsirkin Clear EOP in case guest didn't do it. */ 984ee912ccfSMichael S. Tsirkin desc.status &= ~E1000_RXD_STAT_EOP; 985b19487e2SMichael S. Tsirkin } 98643ad7e3eSJes Sorensen } else { // as per intel docs; skip descriptors with null buf addr 9877c23b892Sbalrog DBGOUT(RX, "Null RX descriptor!!\n"); 98843ad7e3eSJes Sorensen } 989b08340d5SAndreas Färber pci_dma_write(d, base, &desc, sizeof(desc)); 9907c23b892Sbalrog 9917c23b892Sbalrog if (++s->mac_reg[RDH] * sizeof(desc) >= s->mac_reg[RDLEN]) 9927c23b892Sbalrog s->mac_reg[RDH] = 0; 9937c23b892Sbalrog /* see comment in start_xmit; same here */ 994dd793a74SLaszlo Ersek if (s->mac_reg[RDH] == rdh_start || 995dd793a74SLaszlo Ersek rdh_start >= s->mac_reg[RDLEN] / sizeof(desc)) { 9967c23b892Sbalrog DBGOUT(RXERR, "RDH wraparound @%x, RDT %x, RDLEN %x\n", 9977c23b892Sbalrog rdh_start, s->mac_reg[RDT], s->mac_reg[RDLEN]); 9981001cf45SJason Wang e1000_receiver_overrun(s, total_size); 9994f1c942bSMark McLoughlin return -1; 10007c23b892Sbalrog } 1001b19487e2SMichael S. Tsirkin } while (desc_offset < total_size); 10027c23b892Sbalrog 1003093454e2SDmitry Fleytman e1000x_update_rx_total_stats(s->mac_reg, size, total_size); 10047c23b892Sbalrog 10057c23b892Sbalrog n = E1000_ICS_RXT0; 10067c23b892Sbalrog if ((rdt = s->mac_reg[RDT]) < s->mac_reg[RDH]) 10077c23b892Sbalrog rdt += s->mac_reg[RDLEN] / sizeof(desc); 1008bf16cc8fSaliguori if (((rdt - s->mac_reg[RDH]) * sizeof(desc)) <= s->mac_reg[RDLEN] >> 1009bf16cc8fSaliguori s->rxbuf_min_shift) 10107c23b892Sbalrog n |= E1000_ICS_RXDMT0; 10117c23b892Sbalrog 10127c23b892Sbalrog set_ics(s, 0, n); 10134f1c942bSMark McLoughlin 10144f1c942bSMark McLoughlin return size; 10157c23b892Sbalrog } 10167c23b892Sbalrog 101797410ddeSVincenzo Maffione static ssize_t 101897410ddeSVincenzo Maffione e1000_receive(NetClientState *nc, const uint8_t *buf, size_t size) 101997410ddeSVincenzo Maffione { 102097410ddeSVincenzo Maffione const struct iovec iov = { 102197410ddeSVincenzo Maffione .iov_base = (uint8_t *)buf, 102297410ddeSVincenzo Maffione .iov_len = size 102397410ddeSVincenzo Maffione }; 102497410ddeSVincenzo Maffione 102597410ddeSVincenzo Maffione return e1000_receive_iov(nc, &iov, 1); 102697410ddeSVincenzo Maffione } 102797410ddeSVincenzo Maffione 10287c23b892Sbalrog static uint32_t 10297c23b892Sbalrog mac_readreg(E1000State *s, int index) 10307c23b892Sbalrog { 10317c23b892Sbalrog return s->mac_reg[index]; 10327c23b892Sbalrog } 10337c23b892Sbalrog 10347c23b892Sbalrog static uint32_t 103572ea771cSLeonid Bloch mac_low4_read(E1000State *s, int index) 103672ea771cSLeonid Bloch { 103772ea771cSLeonid Bloch return s->mac_reg[index] & 0xf; 103872ea771cSLeonid Bloch } 103972ea771cSLeonid Bloch 104072ea771cSLeonid Bloch static uint32_t 104172ea771cSLeonid Bloch mac_low11_read(E1000State *s, int index) 104272ea771cSLeonid Bloch { 104372ea771cSLeonid Bloch return s->mac_reg[index] & 0x7ff; 104472ea771cSLeonid Bloch } 104572ea771cSLeonid Bloch 104672ea771cSLeonid Bloch static uint32_t 104772ea771cSLeonid Bloch mac_low13_read(E1000State *s, int index) 104872ea771cSLeonid Bloch { 104972ea771cSLeonid Bloch return s->mac_reg[index] & 0x1fff; 105072ea771cSLeonid Bloch } 105172ea771cSLeonid Bloch 105272ea771cSLeonid Bloch static uint32_t 105372ea771cSLeonid Bloch mac_low16_read(E1000State *s, int index) 105472ea771cSLeonid Bloch { 105572ea771cSLeonid Bloch return s->mac_reg[index] & 0xffff; 105672ea771cSLeonid Bloch } 105772ea771cSLeonid Bloch 105872ea771cSLeonid Bloch static uint32_t 10597c23b892Sbalrog mac_icr_read(E1000State *s, int index) 10607c23b892Sbalrog { 10617c23b892Sbalrog uint32_t ret = s->mac_reg[ICR]; 10627c23b892Sbalrog 10637c23b892Sbalrog DBGOUT(INTERRUPT, "ICR read: %x\n", ret); 10647c23b892Sbalrog set_interrupt_cause(s, 0, 0); 10657c23b892Sbalrog return ret; 10667c23b892Sbalrog } 10677c23b892Sbalrog 10687c23b892Sbalrog static uint32_t 10697c23b892Sbalrog mac_read_clr4(E1000State *s, int index) 10707c23b892Sbalrog { 10717c23b892Sbalrog uint32_t ret = s->mac_reg[index]; 10727c23b892Sbalrog 10737c23b892Sbalrog s->mac_reg[index] = 0; 10747c23b892Sbalrog return ret; 10757c23b892Sbalrog } 10767c23b892Sbalrog 10777c23b892Sbalrog static uint32_t 10787c23b892Sbalrog mac_read_clr8(E1000State *s, int index) 10797c23b892Sbalrog { 10807c23b892Sbalrog uint32_t ret = s->mac_reg[index]; 10817c23b892Sbalrog 10827c23b892Sbalrog s->mac_reg[index] = 0; 10837c23b892Sbalrog s->mac_reg[index-1] = 0; 10847c23b892Sbalrog return ret; 10857c23b892Sbalrog } 10867c23b892Sbalrog 10877c23b892Sbalrog static void 10887c23b892Sbalrog mac_writereg(E1000State *s, int index, uint32_t val) 10897c23b892Sbalrog { 10907c36507cSAmos Kong uint32_t macaddr[2]; 10917c36507cSAmos Kong 10927c23b892Sbalrog s->mac_reg[index] = val; 10937c36507cSAmos Kong 109490d131fbSMichael S. Tsirkin if (index == RA + 1) { 10957c36507cSAmos Kong macaddr[0] = cpu_to_le32(s->mac_reg[RA]); 10967c36507cSAmos Kong macaddr[1] = cpu_to_le32(s->mac_reg[RA + 1]); 10977c36507cSAmos Kong qemu_format_nic_info_str(qemu_get_queue(s->nic), (uint8_t *)macaddr); 10987c36507cSAmos Kong } 10997c23b892Sbalrog } 11007c23b892Sbalrog 11017c23b892Sbalrog static void 11027c23b892Sbalrog set_rdt(E1000State *s, int index, uint32_t val) 11037c23b892Sbalrog { 11047c23b892Sbalrog s->mac_reg[index] = val & 0xffff; 1105e8b4c680SPaolo Bonzini if (e1000_has_rxbufs(s, 1)) { 1106b356f76dSJason Wang qemu_flush_queued_packets(qemu_get_queue(s->nic)); 1107e8b4c680SPaolo Bonzini } 11087c23b892Sbalrog } 11097c23b892Sbalrog 11107c23b892Sbalrog static void 11117c23b892Sbalrog set_16bit(E1000State *s, int index, uint32_t val) 11127c23b892Sbalrog { 11137c23b892Sbalrog s->mac_reg[index] = val & 0xffff; 11147c23b892Sbalrog } 11157c23b892Sbalrog 11167c23b892Sbalrog static void 11177c23b892Sbalrog set_dlen(E1000State *s, int index, uint32_t val) 11187c23b892Sbalrog { 11197c23b892Sbalrog s->mac_reg[index] = val & 0xfff80; 11207c23b892Sbalrog } 11217c23b892Sbalrog 11227c23b892Sbalrog static void 11237c23b892Sbalrog set_tctl(E1000State *s, int index, uint32_t val) 11247c23b892Sbalrog { 11257c23b892Sbalrog s->mac_reg[index] = val; 11267c23b892Sbalrog s->mac_reg[TDT] &= 0xffff; 11277c23b892Sbalrog start_xmit(s); 11287c23b892Sbalrog } 11297c23b892Sbalrog 11307c23b892Sbalrog static void 11317c23b892Sbalrog set_icr(E1000State *s, int index, uint32_t val) 11327c23b892Sbalrog { 11337c23b892Sbalrog DBGOUT(INTERRUPT, "set_icr %x\n", val); 11347c23b892Sbalrog set_interrupt_cause(s, 0, s->mac_reg[ICR] & ~val); 11357c23b892Sbalrog } 11367c23b892Sbalrog 11377c23b892Sbalrog static void 11387c23b892Sbalrog set_imc(E1000State *s, int index, uint32_t val) 11397c23b892Sbalrog { 11407c23b892Sbalrog s->mac_reg[IMS] &= ~val; 11417c23b892Sbalrog set_ics(s, 0, 0); 11427c23b892Sbalrog } 11437c23b892Sbalrog 11447c23b892Sbalrog static void 11457c23b892Sbalrog set_ims(E1000State *s, int index, uint32_t val) 11467c23b892Sbalrog { 11477c23b892Sbalrog s->mac_reg[IMS] |= val; 11487c23b892Sbalrog set_ics(s, 0, 0); 11497c23b892Sbalrog } 11507c23b892Sbalrog 11517c23b892Sbalrog #define getreg(x) [x] = mac_readreg 11527c23b892Sbalrog static uint32_t (*macreg_readops[])(E1000State *, int) = { 11537c23b892Sbalrog getreg(PBA), getreg(RCTL), getreg(TDH), getreg(TXDCTL), 11547c23b892Sbalrog getreg(WUFC), getreg(TDT), getreg(CTRL), getreg(LEDCTL), 11557c23b892Sbalrog getreg(MANC), getreg(MDIC), getreg(SWSM), getreg(STATUS), 11567c23b892Sbalrog getreg(TORL), getreg(TOTL), getreg(IMS), getreg(TCTL), 1157b1332393SBill Paul getreg(RDH), getreg(RDT), getreg(VET), getreg(ICS), 1158a00b2335SKay Ackermann getreg(TDBAL), getreg(TDBAH), getreg(RDBAH), getreg(RDBAL), 1159e9845f09SVincenzo Maffione getreg(TDLEN), getreg(RDLEN), getreg(RDTR), getreg(RADV), 116072ea771cSLeonid Bloch getreg(TADV), getreg(ITR), getreg(FCRUC), getreg(IPAV), 116172ea771cSLeonid Bloch getreg(WUC), getreg(WUS), getreg(SCC), getreg(ECOL), 116272ea771cSLeonid Bloch getreg(MCC), getreg(LATECOL), getreg(COLC), getreg(DC), 1163757704f1SKamil Rytarowski getreg(TNCRS), getreg(SEQEC), getreg(CEXTERR), getreg(RLEC), 116472ea771cSLeonid Bloch getreg(XONRXC), getreg(XONTXC), getreg(XOFFRXC), getreg(XOFFTXC), 116572ea771cSLeonid Bloch getreg(RFC), getreg(RJC), getreg(RNBC), getreg(TSCTFC), 11663b274301SLeonid Bloch getreg(MGTPRC), getreg(MGTPDC), getreg(MGTPTC), getreg(GORCL), 11673b274301SLeonid Bloch getreg(GOTCL), 11687c23b892Sbalrog 116920f3e863SLeonid Bloch [TOTH] = mac_read_clr8, [TORH] = mac_read_clr8, 11703b274301SLeonid Bloch [GOTCH] = mac_read_clr8, [GORCH] = mac_read_clr8, 11713b274301SLeonid Bloch [PRC64] = mac_read_clr4, [PRC127] = mac_read_clr4, 11723b274301SLeonid Bloch [PRC255] = mac_read_clr4, [PRC511] = mac_read_clr4, 11733b274301SLeonid Bloch [PRC1023] = mac_read_clr4, [PRC1522] = mac_read_clr4, 11743b274301SLeonid Bloch [PTC64] = mac_read_clr4, [PTC127] = mac_read_clr4, 11753b274301SLeonid Bloch [PTC255] = mac_read_clr4, [PTC511] = mac_read_clr4, 11763b274301SLeonid Bloch [PTC1023] = mac_read_clr4, [PTC1522] = mac_read_clr4, 117720f3e863SLeonid Bloch [GPRC] = mac_read_clr4, [GPTC] = mac_read_clr4, 117820f3e863SLeonid Bloch [TPT] = mac_read_clr4, [TPR] = mac_read_clr4, 11793b274301SLeonid Bloch [RUC] = mac_read_clr4, [ROC] = mac_read_clr4, 11803b274301SLeonid Bloch [BPRC] = mac_read_clr4, [MPRC] = mac_read_clr4, 11813b274301SLeonid Bloch [TSCTC] = mac_read_clr4, [BPTC] = mac_read_clr4, 11823b274301SLeonid Bloch [MPTC] = mac_read_clr4, 118320f3e863SLeonid Bloch [ICR] = mac_icr_read, [EECD] = get_eecd, 118420f3e863SLeonid Bloch [EERD] = flash_eerd_read, 118572ea771cSLeonid Bloch [RDFH] = mac_low13_read, [RDFT] = mac_low13_read, 118672ea771cSLeonid Bloch [RDFHS] = mac_low13_read, [RDFTS] = mac_low13_read, 118772ea771cSLeonid Bloch [RDFPC] = mac_low13_read, 118872ea771cSLeonid Bloch [TDFH] = mac_low11_read, [TDFT] = mac_low11_read, 118972ea771cSLeonid Bloch [TDFHS] = mac_low13_read, [TDFTS] = mac_low13_read, 119072ea771cSLeonid Bloch [TDFPC] = mac_low13_read, 119172ea771cSLeonid Bloch [AIT] = mac_low16_read, 119220f3e863SLeonid Bloch 11937c23b892Sbalrog [CRCERRS ... MPC] = &mac_readreg, 119472ea771cSLeonid Bloch [IP6AT ... IP6AT+3] = &mac_readreg, [IP4AT ... IP4AT+6] = &mac_readreg, 119572ea771cSLeonid Bloch [FFLT ... FFLT+6] = &mac_low11_read, 11967c23b892Sbalrog [RA ... RA+31] = &mac_readreg, 119772ea771cSLeonid Bloch [WUPM ... WUPM+31] = &mac_readreg, 11987c23b892Sbalrog [MTA ... MTA+127] = &mac_readreg, 11998f2e8d1fSaliguori [VFTA ... VFTA+127] = &mac_readreg, 120072ea771cSLeonid Bloch [FFMT ... FFMT+254] = &mac_low4_read, 120172ea771cSLeonid Bloch [FFVT ... FFVT+254] = &mac_readreg, 120272ea771cSLeonid Bloch [PBM ... PBM+16383] = &mac_readreg, 12037c23b892Sbalrog }; 1204b1503cdaSmalc enum { NREADOPS = ARRAY_SIZE(macreg_readops) }; 12057c23b892Sbalrog 12067c23b892Sbalrog #define putreg(x) [x] = mac_writereg 12077c23b892Sbalrog static void (*macreg_writeops[])(E1000State *, int, uint32_t) = { 12087c23b892Sbalrog putreg(PBA), putreg(EERD), putreg(SWSM), putreg(WUFC), 12097c23b892Sbalrog putreg(TDBAL), putreg(TDBAH), putreg(TXDCTL), putreg(RDBAH), 121072ea771cSLeonid Bloch putreg(RDBAL), putreg(LEDCTL), putreg(VET), putreg(FCRUC), 121172ea771cSLeonid Bloch putreg(TDFH), putreg(TDFT), putreg(TDFHS), putreg(TDFTS), 121272ea771cSLeonid Bloch putreg(TDFPC), putreg(RDFH), putreg(RDFT), putreg(RDFHS), 121372ea771cSLeonid Bloch putreg(RDFTS), putreg(RDFPC), putreg(IPAV), putreg(WUC), 121472ea771cSLeonid Bloch putreg(WUS), putreg(AIT), 121520f3e863SLeonid Bloch 12167c23b892Sbalrog [TDLEN] = set_dlen, [RDLEN] = set_dlen, [TCTL] = set_tctl, 12177c23b892Sbalrog [TDT] = set_tctl, [MDIC] = set_mdic, [ICS] = set_ics, 12187c23b892Sbalrog [TDH] = set_16bit, [RDH] = set_16bit, [RDT] = set_rdt, 12197c23b892Sbalrog [IMC] = set_imc, [IMS] = set_ims, [ICR] = set_icr, 1220cab3c825SKevin Wolf [EECD] = set_eecd, [RCTL] = set_rx_control, [CTRL] = set_ctrl, 1221e9845f09SVincenzo Maffione [RDTR] = set_16bit, [RADV] = set_16bit, [TADV] = set_16bit, 1222e9845f09SVincenzo Maffione [ITR] = set_16bit, 122320f3e863SLeonid Bloch 122472ea771cSLeonid Bloch [IP6AT ... IP6AT+3] = &mac_writereg, [IP4AT ... IP4AT+6] = &mac_writereg, 122572ea771cSLeonid Bloch [FFLT ... FFLT+6] = &mac_writereg, 12267c23b892Sbalrog [RA ... RA+31] = &mac_writereg, 122772ea771cSLeonid Bloch [WUPM ... WUPM+31] = &mac_writereg, 12287c23b892Sbalrog [MTA ... MTA+127] = &mac_writereg, 12298f2e8d1fSaliguori [VFTA ... VFTA+127] = &mac_writereg, 123072ea771cSLeonid Bloch [FFMT ... FFMT+254] = &mac_writereg, [FFVT ... FFVT+254] = &mac_writereg, 123172ea771cSLeonid Bloch [PBM ... PBM+16383] = &mac_writereg, 12327c23b892Sbalrog }; 1233b9d03e35SJason Wang 1234b1503cdaSmalc enum { NWRITEOPS = ARRAY_SIZE(macreg_writeops) }; 12357c23b892Sbalrog 1236bc0f0674SLeonid Bloch enum { MAC_ACCESS_PARTIAL = 1, MAC_ACCESS_FLAG_NEEDED = 2 }; 1237bc0f0674SLeonid Bloch 1238bc0f0674SLeonid Bloch #define markflag(x) ((E1000_FLAG_##x << 2) | MAC_ACCESS_FLAG_NEEDED) 1239bc0f0674SLeonid Bloch /* In the array below the meaning of the bits is: [f|f|f|f|f|f|n|p] 1240bc0f0674SLeonid Bloch * f - flag bits (up to 6 possible flags) 1241bc0f0674SLeonid Bloch * n - flag needed 1242bc0f0674SLeonid Bloch * p - partially implenented */ 1243bc0f0674SLeonid Bloch static const uint8_t mac_reg_access[0x8000] = { 1244bc0f0674SLeonid Bloch [RDTR] = markflag(MIT), [TADV] = markflag(MIT), 1245bc0f0674SLeonid Bloch [RADV] = markflag(MIT), [ITR] = markflag(MIT), 124672ea771cSLeonid Bloch 124772ea771cSLeonid Bloch [IPAV] = markflag(MAC), [WUC] = markflag(MAC), 124872ea771cSLeonid Bloch [IP6AT] = markflag(MAC), [IP4AT] = markflag(MAC), 124972ea771cSLeonid Bloch [FFVT] = markflag(MAC), [WUPM] = markflag(MAC), 125072ea771cSLeonid Bloch [ECOL] = markflag(MAC), [MCC] = markflag(MAC), 125172ea771cSLeonid Bloch [DC] = markflag(MAC), [TNCRS] = markflag(MAC), 125272ea771cSLeonid Bloch [RLEC] = markflag(MAC), [XONRXC] = markflag(MAC), 125372ea771cSLeonid Bloch [XOFFTXC] = markflag(MAC), [RFC] = markflag(MAC), 125472ea771cSLeonid Bloch [TSCTFC] = markflag(MAC), [MGTPRC] = markflag(MAC), 125572ea771cSLeonid Bloch [WUS] = markflag(MAC), [AIT] = markflag(MAC), 125672ea771cSLeonid Bloch [FFLT] = markflag(MAC), [FFMT] = markflag(MAC), 125772ea771cSLeonid Bloch [SCC] = markflag(MAC), [FCRUC] = markflag(MAC), 125872ea771cSLeonid Bloch [LATECOL] = markflag(MAC), [COLC] = markflag(MAC), 1259757704f1SKamil Rytarowski [SEQEC] = markflag(MAC), [CEXTERR] = markflag(MAC), 126072ea771cSLeonid Bloch [XONTXC] = markflag(MAC), [XOFFRXC] = markflag(MAC), 126172ea771cSLeonid Bloch [RJC] = markflag(MAC), [RNBC] = markflag(MAC), 126272ea771cSLeonid Bloch [MGTPDC] = markflag(MAC), [MGTPTC] = markflag(MAC), 12633b274301SLeonid Bloch [RUC] = markflag(MAC), [ROC] = markflag(MAC), 12643b274301SLeonid Bloch [GORCL] = markflag(MAC), [GORCH] = markflag(MAC), 12653b274301SLeonid Bloch [GOTCL] = markflag(MAC), [GOTCH] = markflag(MAC), 12663b274301SLeonid Bloch [BPRC] = markflag(MAC), [MPRC] = markflag(MAC), 12673b274301SLeonid Bloch [TSCTC] = markflag(MAC), [PRC64] = markflag(MAC), 12683b274301SLeonid Bloch [PRC127] = markflag(MAC), [PRC255] = markflag(MAC), 12693b274301SLeonid Bloch [PRC511] = markflag(MAC), [PRC1023] = markflag(MAC), 12703b274301SLeonid Bloch [PRC1522] = markflag(MAC), [PTC64] = markflag(MAC), 12713b274301SLeonid Bloch [PTC127] = markflag(MAC), [PTC255] = markflag(MAC), 12723b274301SLeonid Bloch [PTC511] = markflag(MAC), [PTC1023] = markflag(MAC), 12733b274301SLeonid Bloch [PTC1522] = markflag(MAC), [MPTC] = markflag(MAC), 12743b274301SLeonid Bloch [BPTC] = markflag(MAC), 127572ea771cSLeonid Bloch 127672ea771cSLeonid Bloch [TDFH] = markflag(MAC) | MAC_ACCESS_PARTIAL, 127772ea771cSLeonid Bloch [TDFT] = markflag(MAC) | MAC_ACCESS_PARTIAL, 127872ea771cSLeonid Bloch [TDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL, 127972ea771cSLeonid Bloch [TDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL, 128072ea771cSLeonid Bloch [TDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL, 128172ea771cSLeonid Bloch [RDFH] = markflag(MAC) | MAC_ACCESS_PARTIAL, 128272ea771cSLeonid Bloch [RDFT] = markflag(MAC) | MAC_ACCESS_PARTIAL, 128372ea771cSLeonid Bloch [RDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL, 128472ea771cSLeonid Bloch [RDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL, 128572ea771cSLeonid Bloch [RDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL, 128672ea771cSLeonid Bloch [PBM] = markflag(MAC) | MAC_ACCESS_PARTIAL, 1287bc0f0674SLeonid Bloch }; 1288bc0f0674SLeonid Bloch 12897c23b892Sbalrog static void 1290a8170e5eSAvi Kivity e1000_mmio_write(void *opaque, hwaddr addr, uint64_t val, 1291ad00a9b9SAvi Kivity unsigned size) 12927c23b892Sbalrog { 12937c23b892Sbalrog E1000State *s = opaque; 12948da3ff18Spbrook unsigned int index = (addr & 0x1ffff) >> 2; 12957c23b892Sbalrog 129643ad7e3eSJes Sorensen if (index < NWRITEOPS && macreg_writeops[index]) { 1297bc0f0674SLeonid Bloch if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED) 1298bc0f0674SLeonid Bloch || (s->compat_flags & (mac_reg_access[index] >> 2))) { 1299bc0f0674SLeonid Bloch if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) { 1300bc0f0674SLeonid Bloch DBGOUT(GENERAL, "Writing to register at offset: 0x%08x. " 1301bc0f0674SLeonid Bloch "It is not fully implemented.\n", index<<2); 1302bc0f0674SLeonid Bloch } 13036b59fc74Saurel32 macreg_writeops[index](s, index, val); 1304bc0f0674SLeonid Bloch } else { /* "flag needed" bit is set, but the flag is not active */ 1305bc0f0674SLeonid Bloch DBGOUT(MMIO, "MMIO write attempt to disabled reg. addr=0x%08x\n", 1306bc0f0674SLeonid Bloch index<<2); 1307bc0f0674SLeonid Bloch } 130843ad7e3eSJes Sorensen } else if (index < NREADOPS && macreg_readops[index]) { 1309bc0f0674SLeonid Bloch DBGOUT(MMIO, "e1000_mmio_writel RO %x: 0x%04"PRIx64"\n", 1310bc0f0674SLeonid Bloch index<<2, val); 131143ad7e3eSJes Sorensen } else { 1312ad00a9b9SAvi Kivity DBGOUT(UNKNOWN, "MMIO unknown write addr=0x%08x,val=0x%08"PRIx64"\n", 13137c23b892Sbalrog index<<2, val); 13147c23b892Sbalrog } 131543ad7e3eSJes Sorensen } 13167c23b892Sbalrog 1317ad00a9b9SAvi Kivity static uint64_t 1318a8170e5eSAvi Kivity e1000_mmio_read(void *opaque, hwaddr addr, unsigned size) 13197c23b892Sbalrog { 13207c23b892Sbalrog E1000State *s = opaque; 13218da3ff18Spbrook unsigned int index = (addr & 0x1ffff) >> 2; 13227c23b892Sbalrog 1323bc0f0674SLeonid Bloch if (index < NREADOPS && macreg_readops[index]) { 1324bc0f0674SLeonid Bloch if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED) 1325bc0f0674SLeonid Bloch || (s->compat_flags & (mac_reg_access[index] >> 2))) { 1326bc0f0674SLeonid Bloch if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) { 1327bc0f0674SLeonid Bloch DBGOUT(GENERAL, "Reading register at offset: 0x%08x. " 1328bc0f0674SLeonid Bloch "It is not fully implemented.\n", index<<2); 13296b59fc74Saurel32 } 1330bc0f0674SLeonid Bloch return macreg_readops[index](s, index); 1331bc0f0674SLeonid Bloch } else { /* "flag needed" bit is set, but the flag is not active */ 1332bc0f0674SLeonid Bloch DBGOUT(MMIO, "MMIO read attempt of disabled reg. addr=0x%08x\n", 1333bc0f0674SLeonid Bloch index<<2); 1334bc0f0674SLeonid Bloch } 1335bc0f0674SLeonid Bloch } else { 13367c23b892Sbalrog DBGOUT(UNKNOWN, "MMIO unknown read addr=0x%08x\n", index<<2); 1337bc0f0674SLeonid Bloch } 13387c23b892Sbalrog return 0; 13397c23b892Sbalrog } 13407c23b892Sbalrog 1341ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_mmio_ops = { 1342ad00a9b9SAvi Kivity .read = e1000_mmio_read, 1343ad00a9b9SAvi Kivity .write = e1000_mmio_write, 1344ad00a9b9SAvi Kivity .endianness = DEVICE_LITTLE_ENDIAN, 1345ad00a9b9SAvi Kivity .impl = { 1346ad00a9b9SAvi Kivity .min_access_size = 4, 1347ad00a9b9SAvi Kivity .max_access_size = 4, 1348ad00a9b9SAvi Kivity }, 1349ad00a9b9SAvi Kivity }; 1350ad00a9b9SAvi Kivity 1351a8170e5eSAvi Kivity static uint64_t e1000_io_read(void *opaque, hwaddr addr, 1352ad00a9b9SAvi Kivity unsigned size) 13537c23b892Sbalrog { 1354ad00a9b9SAvi Kivity E1000State *s = opaque; 1355ad00a9b9SAvi Kivity 1356ad00a9b9SAvi Kivity (void)s; 1357ad00a9b9SAvi Kivity return 0; 13587c23b892Sbalrog } 13597c23b892Sbalrog 1360a8170e5eSAvi Kivity static void e1000_io_write(void *opaque, hwaddr addr, 1361ad00a9b9SAvi Kivity uint64_t val, unsigned size) 13627c23b892Sbalrog { 1363ad00a9b9SAvi Kivity E1000State *s = opaque; 1364ad00a9b9SAvi Kivity 1365ad00a9b9SAvi Kivity (void)s; 13667c23b892Sbalrog } 13677c23b892Sbalrog 1368ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_io_ops = { 1369ad00a9b9SAvi Kivity .read = e1000_io_read, 1370ad00a9b9SAvi Kivity .write = e1000_io_write, 1371ad00a9b9SAvi Kivity .endianness = DEVICE_LITTLE_ENDIAN, 1372ad00a9b9SAvi Kivity }; 1373ad00a9b9SAvi Kivity 1374e482dc3eSJuan Quintela static bool is_version_1(void *opaque, int version_id) 13757c23b892Sbalrog { 1376e482dc3eSJuan Quintela return version_id == 1; 13777c23b892Sbalrog } 13787c23b892Sbalrog 137944b1ff31SDr. David Alan Gilbert static int e1000_pre_save(void *opaque) 1380ddcb73b7SMichael S. Tsirkin { 1381ddcb73b7SMichael S. Tsirkin E1000State *s = opaque; 1382ddcb73b7SMichael S. Tsirkin NetClientState *nc = qemu_get_queue(s->nic); 13832af234e6SMichael S. Tsirkin 1384e9845f09SVincenzo Maffione /* If the mitigation timer is active, emulate a timeout now. */ 1385e9845f09SVincenzo Maffione if (s->mit_timer_on) { 1386e9845f09SVincenzo Maffione e1000_mit_timer(s); 1387e9845f09SVincenzo Maffione } 1388e9845f09SVincenzo Maffione 1389ddcb73b7SMichael S. Tsirkin /* 13906a2acedbSGabriel L. Somlo * If link is down and auto-negotiation is supported and ongoing, 13916a2acedbSGabriel L. Somlo * complete auto-negotiation immediately. This allows us to look 13926a2acedbSGabriel L. Somlo * at MII_SR_AUTONEG_COMPLETE to infer link status on load. 1393ddcb73b7SMichael S. Tsirkin */ 1394d7a41552SGabriel L. Somlo if (nc->link_down && have_autoneg(s)) { 1395ddcb73b7SMichael S. Tsirkin s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE; 1396ddcb73b7SMichael S. Tsirkin } 139744b1ff31SDr. David Alan Gilbert 1398ff214d42SDr. David Alan Gilbert /* Decide which set of props to migrate in the main structure */ 1399ff214d42SDr. David Alan Gilbert if (chkflag(TSO) || !s->use_tso_for_migration) { 1400ff214d42SDr. David Alan Gilbert /* Either we're migrating with the extra subsection, in which 1401ff214d42SDr. David Alan Gilbert * case the mig_props is always 'props' OR 1402ff214d42SDr. David Alan Gilbert * we've not got the subsection, but 'props' was the last 1403ff214d42SDr. David Alan Gilbert * updated. 1404ff214d42SDr. David Alan Gilbert */ 140559354484SDr. David Alan Gilbert s->mig_props = s->tx.props; 1406ff214d42SDr. David Alan Gilbert } else { 1407ff214d42SDr. David Alan Gilbert /* We're not using the subsection, and 'tso_props' was 1408ff214d42SDr. David Alan Gilbert * the last updated. 1409ff214d42SDr. David Alan Gilbert */ 1410ff214d42SDr. David Alan Gilbert s->mig_props = s->tx.tso_props; 1411ff214d42SDr. David Alan Gilbert } 141244b1ff31SDr. David Alan Gilbert return 0; 1413ddcb73b7SMichael S. Tsirkin } 1414ddcb73b7SMichael S. Tsirkin 1415e4b82364SAmos Kong static int e1000_post_load(void *opaque, int version_id) 1416e4b82364SAmos Kong { 1417e4b82364SAmos Kong E1000State *s = opaque; 1418b356f76dSJason Wang NetClientState *nc = qemu_get_queue(s->nic); 1419e4b82364SAmos Kong 1420bc0f0674SLeonid Bloch if (!chkflag(MIT)) { 1421e9845f09SVincenzo Maffione s->mac_reg[ITR] = s->mac_reg[RDTR] = s->mac_reg[RADV] = 1422e9845f09SVincenzo Maffione s->mac_reg[TADV] = 0; 1423e9845f09SVincenzo Maffione s->mit_irq_level = false; 1424e9845f09SVincenzo Maffione } 1425e9845f09SVincenzo Maffione s->mit_ide = 0; 1426e9845f09SVincenzo Maffione s->mit_timer_on = false; 1427e9845f09SVincenzo Maffione 1428e4b82364SAmos Kong /* nc.link_down can't be migrated, so infer link_down according 1429ddcb73b7SMichael S. Tsirkin * to link status bit in mac_reg[STATUS]. 1430ddcb73b7SMichael S. Tsirkin * Alternatively, restart link negotiation if it was in progress. */ 1431b356f76dSJason Wang nc->link_down = (s->mac_reg[STATUS] & E1000_STATUS_LU) == 0; 14322af234e6SMichael S. Tsirkin 1433d7a41552SGabriel L. Somlo if (have_autoneg(s) && 1434ddcb73b7SMichael S. Tsirkin !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) { 1435ddcb73b7SMichael S. Tsirkin nc->link_down = false; 1436d7a41552SGabriel L. Somlo timer_mod(s->autoneg_timer, 1437d7a41552SGabriel L. Somlo qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500); 1438ddcb73b7SMichael S. Tsirkin } 1439e4b82364SAmos Kong 144059354484SDr. David Alan Gilbert s->tx.props = s->mig_props; 14413c4053c5SDr. David Alan Gilbert if (!s->received_tx_tso) { 14423c4053c5SDr. David Alan Gilbert /* We received only one set of offload data (tx.props) 14433c4053c5SDr. David Alan Gilbert * and haven't got tx.tso_props. The best we can do 14443c4053c5SDr. David Alan Gilbert * is dupe the data. 14453c4053c5SDr. David Alan Gilbert */ 144659354484SDr. David Alan Gilbert s->tx.tso_props = s->mig_props; 14473c4053c5SDr. David Alan Gilbert } 14483c4053c5SDr. David Alan Gilbert return 0; 14493c4053c5SDr. David Alan Gilbert } 14503c4053c5SDr. David Alan Gilbert 14513c4053c5SDr. David Alan Gilbert static int e1000_tx_tso_post_load(void *opaque, int version_id) 14523c4053c5SDr. David Alan Gilbert { 14533c4053c5SDr. David Alan Gilbert E1000State *s = opaque; 14543c4053c5SDr. David Alan Gilbert s->received_tx_tso = true; 1455e4b82364SAmos Kong return 0; 1456e4b82364SAmos Kong } 1457e4b82364SAmos Kong 1458e9845f09SVincenzo Maffione static bool e1000_mit_state_needed(void *opaque) 1459e9845f09SVincenzo Maffione { 1460e9845f09SVincenzo Maffione E1000State *s = opaque; 1461e9845f09SVincenzo Maffione 1462bc0f0674SLeonid Bloch return chkflag(MIT); 1463e9845f09SVincenzo Maffione } 1464e9845f09SVincenzo Maffione 14659e117734SLeonid Bloch static bool e1000_full_mac_needed(void *opaque) 14669e117734SLeonid Bloch { 14679e117734SLeonid Bloch E1000State *s = opaque; 14689e117734SLeonid Bloch 1469bc0f0674SLeonid Bloch return chkflag(MAC); 14709e117734SLeonid Bloch } 14719e117734SLeonid Bloch 147246f2a9ecSDr. David Alan Gilbert static bool e1000_tso_state_needed(void *opaque) 147346f2a9ecSDr. David Alan Gilbert { 147446f2a9ecSDr. David Alan Gilbert E1000State *s = opaque; 147546f2a9ecSDr. David Alan Gilbert 147646f2a9ecSDr. David Alan Gilbert return chkflag(TSO); 147746f2a9ecSDr. David Alan Gilbert } 147846f2a9ecSDr. David Alan Gilbert 1479e9845f09SVincenzo Maffione static const VMStateDescription vmstate_e1000_mit_state = { 1480e9845f09SVincenzo Maffione .name = "e1000/mit_state", 1481e9845f09SVincenzo Maffione .version_id = 1, 1482e9845f09SVincenzo Maffione .minimum_version_id = 1, 14835cd8cadaSJuan Quintela .needed = e1000_mit_state_needed, 1484e9845f09SVincenzo Maffione .fields = (VMStateField[]) { 1485e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[RDTR], E1000State), 1486e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[RADV], E1000State), 1487e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[TADV], E1000State), 1488e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[ITR], E1000State), 1489e9845f09SVincenzo Maffione VMSTATE_BOOL(mit_irq_level, E1000State), 1490e9845f09SVincenzo Maffione VMSTATE_END_OF_LIST() 1491e9845f09SVincenzo Maffione } 1492e9845f09SVincenzo Maffione }; 1493e9845f09SVincenzo Maffione 14949e117734SLeonid Bloch static const VMStateDescription vmstate_e1000_full_mac_state = { 14959e117734SLeonid Bloch .name = "e1000/full_mac_state", 14969e117734SLeonid Bloch .version_id = 1, 14979e117734SLeonid Bloch .minimum_version_id = 1, 14989e117734SLeonid Bloch .needed = e1000_full_mac_needed, 14999e117734SLeonid Bloch .fields = (VMStateField[]) { 15009e117734SLeonid Bloch VMSTATE_UINT32_ARRAY(mac_reg, E1000State, 0x8000), 15019e117734SLeonid Bloch VMSTATE_END_OF_LIST() 15029e117734SLeonid Bloch } 15039e117734SLeonid Bloch }; 15049e117734SLeonid Bloch 15054ae4bf5bSDr. David Alan Gilbert static const VMStateDescription vmstate_e1000_tx_tso_state = { 15064ae4bf5bSDr. David Alan Gilbert .name = "e1000/tx_tso_state", 15074ae4bf5bSDr. David Alan Gilbert .version_id = 1, 15084ae4bf5bSDr. David Alan Gilbert .minimum_version_id = 1, 150946f2a9ecSDr. David Alan Gilbert .needed = e1000_tso_state_needed, 15103c4053c5SDr. David Alan Gilbert .post_load = e1000_tx_tso_post_load, 15114ae4bf5bSDr. David Alan Gilbert .fields = (VMStateField[]) { 15124ae4bf5bSDr. David Alan Gilbert VMSTATE_UINT8(tx.tso_props.ipcss, E1000State), 15134ae4bf5bSDr. David Alan Gilbert VMSTATE_UINT8(tx.tso_props.ipcso, E1000State), 15144ae4bf5bSDr. David Alan Gilbert VMSTATE_UINT16(tx.tso_props.ipcse, E1000State), 15154ae4bf5bSDr. David Alan Gilbert VMSTATE_UINT8(tx.tso_props.tucss, E1000State), 15164ae4bf5bSDr. David Alan Gilbert VMSTATE_UINT8(tx.tso_props.tucso, E1000State), 15174ae4bf5bSDr. David Alan Gilbert VMSTATE_UINT16(tx.tso_props.tucse, E1000State), 15184ae4bf5bSDr. David Alan Gilbert VMSTATE_UINT32(tx.tso_props.paylen, E1000State), 15194ae4bf5bSDr. David Alan Gilbert VMSTATE_UINT8(tx.tso_props.hdr_len, E1000State), 15204ae4bf5bSDr. David Alan Gilbert VMSTATE_UINT16(tx.tso_props.mss, E1000State), 15214ae4bf5bSDr. David Alan Gilbert VMSTATE_INT8(tx.tso_props.ip, E1000State), 15224ae4bf5bSDr. David Alan Gilbert VMSTATE_INT8(tx.tso_props.tcp, E1000State), 15234ae4bf5bSDr. David Alan Gilbert VMSTATE_END_OF_LIST() 15244ae4bf5bSDr. David Alan Gilbert } 15254ae4bf5bSDr. David Alan Gilbert }; 15264ae4bf5bSDr. David Alan Gilbert 1527e482dc3eSJuan Quintela static const VMStateDescription vmstate_e1000 = { 1528e482dc3eSJuan Quintela .name = "e1000", 15294ae4bf5bSDr. David Alan Gilbert .version_id = 2, 1530e482dc3eSJuan Quintela .minimum_version_id = 1, 1531ddcb73b7SMichael S. Tsirkin .pre_save = e1000_pre_save, 1532e4b82364SAmos Kong .post_load = e1000_post_load, 1533e482dc3eSJuan Quintela .fields = (VMStateField[]) { 1534b08340d5SAndreas Färber VMSTATE_PCI_DEVICE(parent_obj, E1000State), 1535e482dc3eSJuan Quintela VMSTATE_UNUSED_TEST(is_version_1, 4), /* was instance id */ 1536e482dc3eSJuan Quintela VMSTATE_UNUSED(4), /* Was mmio_base. */ 1537e482dc3eSJuan Quintela VMSTATE_UINT32(rxbuf_size, E1000State), 1538e482dc3eSJuan Quintela VMSTATE_UINT32(rxbuf_min_shift, E1000State), 1539e482dc3eSJuan Quintela VMSTATE_UINT32(eecd_state.val_in, E1000State), 1540e482dc3eSJuan Quintela VMSTATE_UINT16(eecd_state.bitnum_in, E1000State), 1541e482dc3eSJuan Quintela VMSTATE_UINT16(eecd_state.bitnum_out, E1000State), 1542e482dc3eSJuan Quintela VMSTATE_UINT16(eecd_state.reading, E1000State), 1543e482dc3eSJuan Quintela VMSTATE_UINT32(eecd_state.old_eecd, E1000State), 154459354484SDr. David Alan Gilbert VMSTATE_UINT8(mig_props.ipcss, E1000State), 154559354484SDr. David Alan Gilbert VMSTATE_UINT8(mig_props.ipcso, E1000State), 154659354484SDr. David Alan Gilbert VMSTATE_UINT16(mig_props.ipcse, E1000State), 154759354484SDr. David Alan Gilbert VMSTATE_UINT8(mig_props.tucss, E1000State), 154859354484SDr. David Alan Gilbert VMSTATE_UINT8(mig_props.tucso, E1000State), 154959354484SDr. David Alan Gilbert VMSTATE_UINT16(mig_props.tucse, E1000State), 155059354484SDr. David Alan Gilbert VMSTATE_UINT32(mig_props.paylen, E1000State), 155159354484SDr. David Alan Gilbert VMSTATE_UINT8(mig_props.hdr_len, E1000State), 155259354484SDr. David Alan Gilbert VMSTATE_UINT16(mig_props.mss, E1000State), 1553e482dc3eSJuan Quintela VMSTATE_UINT16(tx.size, E1000State), 1554e482dc3eSJuan Quintela VMSTATE_UINT16(tx.tso_frames, E1000State), 15557d08c73eSEd Swierk via Qemu-devel VMSTATE_UINT8(tx.sum_needed, E1000State), 155659354484SDr. David Alan Gilbert VMSTATE_INT8(mig_props.ip, E1000State), 155759354484SDr. David Alan Gilbert VMSTATE_INT8(mig_props.tcp, E1000State), 1558e482dc3eSJuan Quintela VMSTATE_BUFFER(tx.header, E1000State), 1559e482dc3eSJuan Quintela VMSTATE_BUFFER(tx.data, E1000State), 1560e482dc3eSJuan Quintela VMSTATE_UINT16_ARRAY(eeprom_data, E1000State, 64), 1561e482dc3eSJuan Quintela VMSTATE_UINT16_ARRAY(phy_reg, E1000State, 0x20), 1562e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[CTRL], E1000State), 1563e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[EECD], E1000State), 1564e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[EERD], E1000State), 1565e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[GPRC], E1000State), 1566e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[GPTC], E1000State), 1567e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[ICR], E1000State), 1568e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[ICS], E1000State), 1569e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[IMC], E1000State), 1570e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[IMS], E1000State), 1571e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[LEDCTL], E1000State), 1572e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[MANC], E1000State), 1573e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[MDIC], E1000State), 1574e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[MPC], E1000State), 1575e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[PBA], E1000State), 1576e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RCTL], E1000State), 1577e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDBAH], E1000State), 1578e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDBAL], E1000State), 1579e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDH], E1000State), 1580e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDLEN], E1000State), 1581e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDT], E1000State), 1582e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[STATUS], E1000State), 1583e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[SWSM], E1000State), 1584e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TCTL], E1000State), 1585e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDBAH], E1000State), 1586e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDBAL], E1000State), 1587e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDH], E1000State), 1588e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDLEN], E1000State), 1589e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDT], E1000State), 1590e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TORH], E1000State), 1591e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TORL], E1000State), 1592e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TOTH], E1000State), 1593e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TOTL], E1000State), 1594e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TPR], E1000State), 1595e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TPT], E1000State), 1596e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TXDCTL], E1000State), 1597e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[WUFC], E1000State), 1598e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[VET], E1000State), 1599e482dc3eSJuan Quintela VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, RA, 32), 1600e482dc3eSJuan Quintela VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, MTA, 128), 1601e482dc3eSJuan Quintela VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, VFTA, 128), 1602e482dc3eSJuan Quintela VMSTATE_END_OF_LIST() 1603e9845f09SVincenzo Maffione }, 16045cd8cadaSJuan Quintela .subsections = (const VMStateDescription*[]) { 16055cd8cadaSJuan Quintela &vmstate_e1000_mit_state, 16069e117734SLeonid Bloch &vmstate_e1000_full_mac_state, 16074ae4bf5bSDr. David Alan Gilbert &vmstate_e1000_tx_tso_state, 16085cd8cadaSJuan Quintela NULL 16097c23b892Sbalrog } 1610e482dc3eSJuan Quintela }; 16117c23b892Sbalrog 16128597f2e1SGabriel L. Somlo /* 16138597f2e1SGabriel L. Somlo * EEPROM contents documented in Tables 5-2 and 5-3, pp. 98-102. 16148597f2e1SGabriel L. Somlo * Note: A valid DevId will be inserted during pci_e1000_init(). 16158597f2e1SGabriel L. Somlo */ 161688b4e9dbSblueswir1 static const uint16_t e1000_eeprom_template[64] = { 16177c23b892Sbalrog 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0x0000, 16188597f2e1SGabriel L. Somlo 0x3000, 0x1000, 0x6403, 0 /*DevId*/, 0x8086, 0 /*DevId*/, 0x8086, 0x3040, 16197c23b892Sbalrog 0x0008, 0x2000, 0x7e14, 0x0048, 0x1000, 0x00d8, 0x0000, 0x2700, 16207c23b892Sbalrog 0x6cc9, 0x3150, 0x0722, 0x040b, 0x0984, 0x0000, 0xc000, 0x0706, 16217c23b892Sbalrog 0x1008, 0x0000, 0x0f04, 0x7fff, 0x4d01, 0xffff, 0xffff, 0xffff, 16227c23b892Sbalrog 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 16237c23b892Sbalrog 0x0100, 0x4000, 0x121c, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 16247c23b892Sbalrog 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x0000, 16257c23b892Sbalrog }; 16267c23b892Sbalrog 16277c23b892Sbalrog /* PCI interface */ 16287c23b892Sbalrog 16297c23b892Sbalrog static void 1630ad00a9b9SAvi Kivity e1000_mmio_setup(E1000State *d) 16317c23b892Sbalrog { 1632f65ed4c1Saliguori int i; 1633f65ed4c1Saliguori const uint32_t excluded_regs[] = { 1634f65ed4c1Saliguori E1000_MDIC, E1000_ICR, E1000_ICS, E1000_IMS, 1635f65ed4c1Saliguori E1000_IMC, E1000_TCTL, E1000_TDT, PNPMMIO_SIZE 1636f65ed4c1Saliguori }; 1637f65ed4c1Saliguori 1638eedfac6fSPaolo Bonzini memory_region_init_io(&d->mmio, OBJECT(d), &e1000_mmio_ops, d, 1639eedfac6fSPaolo Bonzini "e1000-mmio", PNPMMIO_SIZE); 1640ad00a9b9SAvi Kivity memory_region_add_coalescing(&d->mmio, 0, excluded_regs[0]); 1641f65ed4c1Saliguori for (i = 0; excluded_regs[i] != PNPMMIO_SIZE; i++) 1642ad00a9b9SAvi Kivity memory_region_add_coalescing(&d->mmio, excluded_regs[i] + 4, 1643ad00a9b9SAvi Kivity excluded_regs[i+1] - excluded_regs[i] - 4); 1644eedfac6fSPaolo Bonzini memory_region_init_io(&d->io, OBJECT(d), &e1000_io_ops, d, "e1000-io", IOPORT_SIZE); 16457c23b892Sbalrog } 16467c23b892Sbalrog 1647b946a153Saliguori static void 16484b09be85Saliguori pci_e1000_uninit(PCIDevice *dev) 16494b09be85Saliguori { 1650567a3c9eSPeter Crosthwaite E1000State *d = E1000(dev); 16514b09be85Saliguori 1652bc72ad67SAlex Bligh timer_del(d->autoneg_timer); 1653bc72ad67SAlex Bligh timer_free(d->autoneg_timer); 1654e9845f09SVincenzo Maffione timer_del(d->mit_timer); 1655e9845f09SVincenzo Maffione timer_free(d->mit_timer); 1656*157628d0Syuchenlin timer_del(d->flush_queue_timer); 1657*157628d0Syuchenlin timer_free(d->flush_queue_timer); 1658948ecf21SJason Wang qemu_del_nic(d->nic); 16594b09be85Saliguori } 16604b09be85Saliguori 1661a03e2aecSMark McLoughlin static NetClientInfo net_e1000_info = { 1662f394b2e2SEric Blake .type = NET_CLIENT_DRIVER_NIC, 1663a03e2aecSMark McLoughlin .size = sizeof(NICState), 1664a03e2aecSMark McLoughlin .can_receive = e1000_can_receive, 1665a03e2aecSMark McLoughlin .receive = e1000_receive, 166697410ddeSVincenzo Maffione .receive_iov = e1000_receive_iov, 1667a03e2aecSMark McLoughlin .link_status_changed = e1000_set_link_status, 1668a03e2aecSMark McLoughlin }; 1669a03e2aecSMark McLoughlin 167020302e71SMichael S. Tsirkin static void e1000_write_config(PCIDevice *pci_dev, uint32_t address, 167120302e71SMichael S. Tsirkin uint32_t val, int len) 167220302e71SMichael S. Tsirkin { 167320302e71SMichael S. Tsirkin E1000State *s = E1000(pci_dev); 167420302e71SMichael S. Tsirkin 167520302e71SMichael S. Tsirkin pci_default_write_config(pci_dev, address, val, len); 167620302e71SMichael S. Tsirkin 167720302e71SMichael S. Tsirkin if (range_covers_byte(address, len, PCI_COMMAND) && 167820302e71SMichael S. Tsirkin (pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { 167920302e71SMichael S. Tsirkin qemu_flush_queued_packets(qemu_get_queue(s->nic)); 168020302e71SMichael S. Tsirkin } 168120302e71SMichael S. Tsirkin } 168220302e71SMichael S. Tsirkin 16839af21dbeSMarkus Armbruster static void pci_e1000_realize(PCIDevice *pci_dev, Error **errp) 16847c23b892Sbalrog { 1685567a3c9eSPeter Crosthwaite DeviceState *dev = DEVICE(pci_dev); 1686567a3c9eSPeter Crosthwaite E1000State *d = E1000(pci_dev); 16877c23b892Sbalrog uint8_t *pci_conf; 1688fbdaa002SGerd Hoffmann uint8_t *macaddr; 1689aff427a1SChris Wright 169020302e71SMichael S. Tsirkin pci_dev->config_write = e1000_write_config; 169120302e71SMichael S. Tsirkin 1692b08340d5SAndreas Färber pci_conf = pci_dev->config; 16937c23b892Sbalrog 1694a9cbacb0SMichael S. Tsirkin /* TODO: RST# value should be 0, PCI spec 6.2.4 */ 1695a9cbacb0SMichael S. Tsirkin pci_conf[PCI_CACHE_LINE_SIZE] = 0x10; 16967c23b892Sbalrog 1697817e0b6fSMichael S. Tsirkin pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */ 16987c23b892Sbalrog 1699ad00a9b9SAvi Kivity e1000_mmio_setup(d); 17007c23b892Sbalrog 1701b08340d5SAndreas Färber pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio); 17027c23b892Sbalrog 1703b08340d5SAndreas Färber pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->io); 17047c23b892Sbalrog 1705fbdaa002SGerd Hoffmann qemu_macaddr_default_if_unset(&d->conf.macaddr); 1706fbdaa002SGerd Hoffmann macaddr = d->conf.macaddr.a; 1707093454e2SDmitry Fleytman 1708093454e2SDmitry Fleytman e1000x_core_prepare_eeprom(d->eeprom_data, 1709093454e2SDmitry Fleytman e1000_eeprom_template, 1710093454e2SDmitry Fleytman sizeof(e1000_eeprom_template), 1711093454e2SDmitry Fleytman PCI_DEVICE_GET_CLASS(pci_dev)->device_id, 1712093454e2SDmitry Fleytman macaddr); 17137c23b892Sbalrog 1714a03e2aecSMark McLoughlin d->nic = qemu_new_nic(&net_e1000_info, &d->conf, 1715567a3c9eSPeter Crosthwaite object_get_typename(OBJECT(d)), dev->id, d); 17167c23b892Sbalrog 1717b356f76dSJason Wang qemu_format_nic_info_str(qemu_get_queue(d->nic), macaddr); 17181ca4d09aSGleb Natapov 1719bc72ad67SAlex Bligh d->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, e1000_autoneg_timer, d); 1720e9845f09SVincenzo Maffione d->mit_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000_mit_timer, d); 1721*157628d0Syuchenlin d->flush_queue_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, 1722*157628d0Syuchenlin e1000_flush_queue_timer, d); 17237c23b892Sbalrog } 17249d07d757SPaul Brook 1725fbdaa002SGerd Hoffmann static void qdev_e1000_reset(DeviceState *dev) 1726fbdaa002SGerd Hoffmann { 1727567a3c9eSPeter Crosthwaite E1000State *d = E1000(dev); 1728fbdaa002SGerd Hoffmann e1000_reset(d); 1729fbdaa002SGerd Hoffmann } 1730fbdaa002SGerd Hoffmann 173140021f08SAnthony Liguori static Property e1000_properties[] = { 1732fbdaa002SGerd Hoffmann DEFINE_NIC_PROPERTIES(E1000State, conf), 17332af234e6SMichael S. Tsirkin DEFINE_PROP_BIT("autonegotiation", E1000State, 17342af234e6SMichael S. Tsirkin compat_flags, E1000_FLAG_AUTONEG_BIT, true), 1735e9845f09SVincenzo Maffione DEFINE_PROP_BIT("mitigation", E1000State, 1736e9845f09SVincenzo Maffione compat_flags, E1000_FLAG_MIT_BIT, true), 1737ba63ec85SLeonid Bloch DEFINE_PROP_BIT("extra_mac_registers", E1000State, 1738ba63ec85SLeonid Bloch compat_flags, E1000_FLAG_MAC_BIT, true), 173946f2a9ecSDr. David Alan Gilbert DEFINE_PROP_BIT("migrate_tso_props", E1000State, 174046f2a9ecSDr. David Alan Gilbert compat_flags, E1000_FLAG_TSO_BIT, true), 1741fbdaa002SGerd Hoffmann DEFINE_PROP_END_OF_LIST(), 174240021f08SAnthony Liguori }; 174340021f08SAnthony Liguori 17448597f2e1SGabriel L. Somlo typedef struct E1000Info { 17458597f2e1SGabriel L. Somlo const char *name; 17468597f2e1SGabriel L. Somlo uint16_t device_id; 17478597f2e1SGabriel L. Somlo uint8_t revision; 17488597f2e1SGabriel L. Somlo uint16_t phy_id2; 17498597f2e1SGabriel L. Somlo } E1000Info; 17508597f2e1SGabriel L. Somlo 175140021f08SAnthony Liguori static void e1000_class_init(ObjectClass *klass, void *data) 175240021f08SAnthony Liguori { 175339bffca2SAnthony Liguori DeviceClass *dc = DEVICE_CLASS(klass); 175440021f08SAnthony Liguori PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 17558597f2e1SGabriel L. Somlo E1000BaseClass *e = E1000_DEVICE_CLASS(klass); 17568597f2e1SGabriel L. Somlo const E1000Info *info = data; 175740021f08SAnthony Liguori 17589af21dbeSMarkus Armbruster k->realize = pci_e1000_realize; 175940021f08SAnthony Liguori k->exit = pci_e1000_uninit; 1760c45e5b5bSGerd Hoffmann k->romfile = "efi-e1000.rom"; 176140021f08SAnthony Liguori k->vendor_id = PCI_VENDOR_ID_INTEL; 17628597f2e1SGabriel L. Somlo k->device_id = info->device_id; 17638597f2e1SGabriel L. Somlo k->revision = info->revision; 17648597f2e1SGabriel L. Somlo e->phy_id2 = info->phy_id2; 176540021f08SAnthony Liguori k->class_id = PCI_CLASS_NETWORK_ETHERNET; 1766125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 176739bffca2SAnthony Liguori dc->desc = "Intel Gigabit Ethernet"; 176839bffca2SAnthony Liguori dc->reset = qdev_e1000_reset; 176939bffca2SAnthony Liguori dc->vmsd = &vmstate_e1000; 177039bffca2SAnthony Liguori dc->props = e1000_properties; 1771fbdaa002SGerd Hoffmann } 177240021f08SAnthony Liguori 17735df3bf62SGonglei static void e1000_instance_init(Object *obj) 17745df3bf62SGonglei { 17755df3bf62SGonglei E1000State *n = E1000(obj); 17765df3bf62SGonglei device_add_bootindex_property(obj, &n->conf.bootindex, 17775df3bf62SGonglei "bootindex", "/ethernet-phy@0", 17785df3bf62SGonglei DEVICE(n), NULL); 17795df3bf62SGonglei } 17805df3bf62SGonglei 17818597f2e1SGabriel L. Somlo static const TypeInfo e1000_base_info = { 17828597f2e1SGabriel L. Somlo .name = TYPE_E1000_BASE, 178339bffca2SAnthony Liguori .parent = TYPE_PCI_DEVICE, 178439bffca2SAnthony Liguori .instance_size = sizeof(E1000State), 17855df3bf62SGonglei .instance_init = e1000_instance_init, 17868597f2e1SGabriel L. Somlo .class_size = sizeof(E1000BaseClass), 17878597f2e1SGabriel L. Somlo .abstract = true, 1788fd3b02c8SEduardo Habkost .interfaces = (InterfaceInfo[]) { 1789fd3b02c8SEduardo Habkost { INTERFACE_CONVENTIONAL_PCI_DEVICE }, 1790fd3b02c8SEduardo Habkost { }, 1791fd3b02c8SEduardo Habkost }, 17928597f2e1SGabriel L. Somlo }; 17938597f2e1SGabriel L. Somlo 17948597f2e1SGabriel L. Somlo static const E1000Info e1000_devices[] = { 17958597f2e1SGabriel L. Somlo { 179683044020SJason Wang .name = "e1000", 17978597f2e1SGabriel L. Somlo .device_id = E1000_DEV_ID_82540EM, 17988597f2e1SGabriel L. Somlo .revision = 0x03, 17998597f2e1SGabriel L. Somlo .phy_id2 = E1000_PHY_ID2_8254xx_DEFAULT, 18008597f2e1SGabriel L. Somlo }, 18018597f2e1SGabriel L. Somlo { 18028597f2e1SGabriel L. Somlo .name = "e1000-82544gc", 18038597f2e1SGabriel L. Somlo .device_id = E1000_DEV_ID_82544GC_COPPER, 18048597f2e1SGabriel L. Somlo .revision = 0x03, 18058597f2e1SGabriel L. Somlo .phy_id2 = E1000_PHY_ID2_82544x, 18068597f2e1SGabriel L. Somlo }, 18078597f2e1SGabriel L. Somlo { 18088597f2e1SGabriel L. Somlo .name = "e1000-82545em", 18098597f2e1SGabriel L. Somlo .device_id = E1000_DEV_ID_82545EM_COPPER, 18108597f2e1SGabriel L. Somlo .revision = 0x03, 18118597f2e1SGabriel L. Somlo .phy_id2 = E1000_PHY_ID2_8254xx_DEFAULT, 18128597f2e1SGabriel L. Somlo }, 18138597f2e1SGabriel L. Somlo }; 18148597f2e1SGabriel L. Somlo 181583f7d43aSAndreas Färber static void e1000_register_types(void) 18169d07d757SPaul Brook { 18178597f2e1SGabriel L. Somlo int i; 18188597f2e1SGabriel L. Somlo 18198597f2e1SGabriel L. Somlo type_register_static(&e1000_base_info); 18208597f2e1SGabriel L. Somlo for (i = 0; i < ARRAY_SIZE(e1000_devices); i++) { 18218597f2e1SGabriel L. Somlo const E1000Info *info = &e1000_devices[i]; 18228597f2e1SGabriel L. Somlo TypeInfo type_info = {}; 18238597f2e1SGabriel L. Somlo 18248597f2e1SGabriel L. Somlo type_info.name = info->name; 18258597f2e1SGabriel L. Somlo type_info.parent = TYPE_E1000_BASE; 18268597f2e1SGabriel L. Somlo type_info.class_data = (void *)info; 18278597f2e1SGabriel L. Somlo type_info.class_init = e1000_class_init; 18285df3bf62SGonglei type_info.instance_init = e1000_instance_init; 18298597f2e1SGabriel L. Somlo 18308597f2e1SGabriel L. Somlo type_register(&type_info); 18318597f2e1SGabriel L. Somlo } 18329d07d757SPaul Brook } 18339d07d757SPaul Brook 183483f7d43aSAndreas Färber type_init(e1000_register_types) 1835