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" 3383c9f4caSPaolo Bonzini #include "hw/loader.h" 349c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 359c17d615SPaolo Bonzini #include "sysemu/dma.h" 3697410ddeSVincenzo Maffione #include "qemu/iov.h" 3720302e71SMichael S. Tsirkin #include "qemu/range.h" 387c23b892Sbalrog 39*093454e2SDmitry Fleytman #include "e1000x_common.h" 407c23b892Sbalrog 413b274301SLeonid Bloch static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 423b274301SLeonid Bloch 4327124888SJes Sorensen #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; 101*093454e2SDmitry Fleytman e1000x_txd_props props; 1027c23b892Sbalrog uint16_t tso_frames; 1037c23b892Sbalrog } tx; 1047c23b892Sbalrog 1057c23b892Sbalrog struct { 10620f3e863SLeonid Bloch uint32_t val_in; /* shifted in from guest driver */ 1077c23b892Sbalrog uint16_t bitnum_in; 1087c23b892Sbalrog uint16_t bitnum_out; 1097c23b892Sbalrog uint16_t reading; 1107c23b892Sbalrog uint32_t old_eecd; 1117c23b892Sbalrog } eecd_state; 112b9d03e35SJason Wang 113b9d03e35SJason Wang QEMUTimer *autoneg_timer; 1142af234e6SMichael S. Tsirkin 115e9845f09SVincenzo Maffione QEMUTimer *mit_timer; /* Mitigation timer. */ 116e9845f09SVincenzo Maffione bool mit_timer_on; /* Mitigation timer is running. */ 117e9845f09SVincenzo Maffione bool mit_irq_level; /* Tracks interrupt pin level. */ 118e9845f09SVincenzo Maffione uint32_t mit_ide; /* Tracks E1000_TXD_CMD_IDE bit. */ 119e9845f09SVincenzo Maffione 1202af234e6SMichael S. Tsirkin /* Compatibility flags for migration to/from qemu 1.3.0 and older */ 1212af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG_BIT 0 122e9845f09SVincenzo Maffione #define E1000_FLAG_MIT_BIT 1 1239e117734SLeonid Bloch #define E1000_FLAG_MAC_BIT 2 1242af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG (1 << E1000_FLAG_AUTONEG_BIT) 125e9845f09SVincenzo Maffione #define E1000_FLAG_MIT (1 << E1000_FLAG_MIT_BIT) 1269e117734SLeonid Bloch #define E1000_FLAG_MAC (1 << E1000_FLAG_MAC_BIT) 1272af234e6SMichael S. Tsirkin uint32_t compat_flags; 1287c23b892Sbalrog } E1000State; 1297c23b892Sbalrog 130bc0f0674SLeonid Bloch #define chkflag(x) (s->compat_flags & E1000_FLAG_##x) 131bc0f0674SLeonid Bloch 1328597f2e1SGabriel L. Somlo typedef struct E1000BaseClass { 1338597f2e1SGabriel L. Somlo PCIDeviceClass parent_class; 1348597f2e1SGabriel L. Somlo uint16_t phy_id2; 1358597f2e1SGabriel L. Somlo } E1000BaseClass; 1368597f2e1SGabriel L. Somlo 1378597f2e1SGabriel L. Somlo #define TYPE_E1000_BASE "e1000-base" 138567a3c9eSPeter Crosthwaite 139567a3c9eSPeter Crosthwaite #define E1000(obj) \ 1408597f2e1SGabriel L. Somlo OBJECT_CHECK(E1000State, (obj), TYPE_E1000_BASE) 1418597f2e1SGabriel L. Somlo 1428597f2e1SGabriel L. Somlo #define E1000_DEVICE_CLASS(klass) \ 1438597f2e1SGabriel L. Somlo OBJECT_CLASS_CHECK(E1000BaseClass, (klass), TYPE_E1000_BASE) 1448597f2e1SGabriel L. Somlo #define E1000_DEVICE_GET_CLASS(obj) \ 1458597f2e1SGabriel L. Somlo OBJECT_GET_CLASS(E1000BaseClass, (obj), TYPE_E1000_BASE) 146567a3c9eSPeter Crosthwaite 14771aadd3cSJason Wang static void 14871aadd3cSJason Wang e1000_link_up(E1000State *s) 14971aadd3cSJason Wang { 150*093454e2SDmitry Fleytman e1000x_update_regs_on_link_up(s->mac_reg, s->phy_reg); 151*093454e2SDmitry Fleytman 152*093454e2SDmitry Fleytman /* E1000_STATUS_LU is tested by e1000_can_receive() */ 153*093454e2SDmitry Fleytman qemu_flush_queued_packets(qemu_get_queue(s->nic)); 154*093454e2SDmitry Fleytman } 155*093454e2SDmitry Fleytman 156*093454e2SDmitry Fleytman static void 157*093454e2SDmitry Fleytman e1000_autoneg_done(E1000State *s) 158*093454e2SDmitry Fleytman { 159*093454e2SDmitry Fleytman e1000x_update_regs_on_autoneg_done(s->mac_reg, s->phy_reg); 1605df6a185SStefan Hajnoczi 1615df6a185SStefan Hajnoczi /* E1000_STATUS_LU is tested by e1000_can_receive() */ 1625df6a185SStefan Hajnoczi qemu_flush_queued_packets(qemu_get_queue(s->nic)); 16371aadd3cSJason Wang } 16471aadd3cSJason Wang 1651195fed9SGabriel L. Somlo static bool 1661195fed9SGabriel L. Somlo have_autoneg(E1000State *s) 1671195fed9SGabriel L. Somlo { 168bc0f0674SLeonid Bloch return chkflag(AUTONEG) && (s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN); 1691195fed9SGabriel L. Somlo } 1701195fed9SGabriel L. Somlo 171b9d03e35SJason Wang static void 172b9d03e35SJason Wang set_phy_ctrl(E1000State *s, int index, uint16_t val) 173b9d03e35SJason Wang { 1741195fed9SGabriel L. Somlo /* bits 0-5 reserved; MII_CR_[RESTART_AUTO_NEG,RESET] are self clearing */ 1751195fed9SGabriel L. Somlo s->phy_reg[PHY_CTRL] = val & ~(0x3f | 1761195fed9SGabriel L. Somlo MII_CR_RESET | 1771195fed9SGabriel L. Somlo MII_CR_RESTART_AUTO_NEG); 1781195fed9SGabriel L. Somlo 1792af234e6SMichael S. Tsirkin /* 1802af234e6SMichael S. Tsirkin * QEMU 1.3 does not support link auto-negotiation emulation, so if we 1812af234e6SMichael S. Tsirkin * migrate during auto negotiation, after migration the link will be 1822af234e6SMichael S. Tsirkin * down. 1832af234e6SMichael S. Tsirkin */ 1841195fed9SGabriel L. Somlo if (have_autoneg(s) && (val & MII_CR_RESTART_AUTO_NEG)) { 185*093454e2SDmitry Fleytman e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer); 186b9d03e35SJason Wang } 187b9d03e35SJason Wang } 188b9d03e35SJason Wang 189b9d03e35SJason Wang static void (*phyreg_writeops[])(E1000State *, int, uint16_t) = { 190b9d03e35SJason Wang [PHY_CTRL] = set_phy_ctrl, 191b9d03e35SJason Wang }; 192b9d03e35SJason Wang 193b9d03e35SJason Wang enum { NPHYWRITEOPS = ARRAY_SIZE(phyreg_writeops) }; 194b9d03e35SJason Wang 1957c23b892Sbalrog enum { PHY_R = 1, PHY_W = 2, PHY_RW = PHY_R | PHY_W }; 19688b4e9dbSblueswir1 static const char phy_regcap[0x20] = { 1977c23b892Sbalrog [PHY_STATUS] = PHY_R, [M88E1000_EXT_PHY_SPEC_CTRL] = PHY_RW, 1987c23b892Sbalrog [PHY_ID1] = PHY_R, [M88E1000_PHY_SPEC_CTRL] = PHY_RW, 1997c23b892Sbalrog [PHY_CTRL] = PHY_RW, [PHY_1000T_CTRL] = PHY_RW, 2007c23b892Sbalrog [PHY_LP_ABILITY] = PHY_R, [PHY_1000T_STATUS] = PHY_R, 2017c23b892Sbalrog [PHY_AUTONEG_ADV] = PHY_RW, [M88E1000_RX_ERR_CNTR] = PHY_R, 2026883b591SGabriel L. Somlo [PHY_ID2] = PHY_R, [M88E1000_PHY_SPEC_STATUS] = PHY_R, 2036883b591SGabriel L. Somlo [PHY_AUTONEG_EXP] = PHY_R, 2047c23b892Sbalrog }; 2057c23b892Sbalrog 2068597f2e1SGabriel L. Somlo /* PHY_ID2 documented in 8254x_GBe_SDM.pdf, pp. 250 */ 207814cd3acSMichael S. Tsirkin static const uint16_t phy_reg_init[] = { 2089616c290SGabriel L. Somlo [PHY_CTRL] = MII_CR_SPEED_SELECT_MSB | 2099616c290SGabriel L. Somlo MII_CR_FULL_DUPLEX | 2109616c290SGabriel L. Somlo MII_CR_AUTO_NEG_EN, 2119616c290SGabriel L. Somlo 2129616c290SGabriel L. Somlo [PHY_STATUS] = MII_SR_EXTENDED_CAPS | 2139616c290SGabriel L. Somlo MII_SR_LINK_STATUS | /* link initially up */ 2149616c290SGabriel L. Somlo MII_SR_AUTONEG_CAPS | 2159616c290SGabriel L. Somlo /* MII_SR_AUTONEG_COMPLETE: initially NOT completed */ 2169616c290SGabriel L. Somlo MII_SR_PREAMBLE_SUPPRESS | 2179616c290SGabriel L. Somlo MII_SR_EXTENDED_STATUS | 2189616c290SGabriel L. Somlo MII_SR_10T_HD_CAPS | 2199616c290SGabriel L. Somlo MII_SR_10T_FD_CAPS | 2209616c290SGabriel L. Somlo MII_SR_100X_HD_CAPS | 2219616c290SGabriel L. Somlo MII_SR_100X_FD_CAPS, 2229616c290SGabriel L. Somlo 2239616c290SGabriel L. Somlo [PHY_ID1] = 0x141, 2249616c290SGabriel L. Somlo /* [PHY_ID2] configured per DevId, from e1000_reset() */ 2259616c290SGabriel L. Somlo [PHY_AUTONEG_ADV] = 0xde1, 2269616c290SGabriel L. Somlo [PHY_LP_ABILITY] = 0x1e0, 2279616c290SGabriel L. Somlo [PHY_1000T_CTRL] = 0x0e00, 2289616c290SGabriel L. Somlo [PHY_1000T_STATUS] = 0x3c00, 2299616c290SGabriel L. Somlo [M88E1000_PHY_SPEC_CTRL] = 0x360, 230814cd3acSMichael S. Tsirkin [M88E1000_PHY_SPEC_STATUS] = 0xac00, 2319616c290SGabriel L. Somlo [M88E1000_EXT_PHY_SPEC_CTRL] = 0x0d60, 232814cd3acSMichael S. Tsirkin }; 233814cd3acSMichael S. Tsirkin 234814cd3acSMichael S. Tsirkin static const uint32_t mac_reg_init[] = { 235814cd3acSMichael S. Tsirkin [PBA] = 0x00100030, 236814cd3acSMichael S. Tsirkin [LEDCTL] = 0x602, 237814cd3acSMichael S. Tsirkin [CTRL] = E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN0 | 238814cd3acSMichael S. Tsirkin E1000_CTRL_SPD_1000 | E1000_CTRL_SLU, 239814cd3acSMichael S. Tsirkin [STATUS] = 0x80000000 | E1000_STATUS_GIO_MASTER_ENABLE | 240814cd3acSMichael S. Tsirkin E1000_STATUS_ASDV | E1000_STATUS_MTXCKOK | 241814cd3acSMichael S. Tsirkin E1000_STATUS_SPEED_1000 | E1000_STATUS_FD | 242814cd3acSMichael S. Tsirkin E1000_STATUS_LU, 243814cd3acSMichael S. Tsirkin [MANC] = E1000_MANC_EN_MNG2HOST | E1000_MANC_RCV_TCO_EN | 244814cd3acSMichael S. Tsirkin E1000_MANC_ARP_EN | E1000_MANC_0298_EN | 245814cd3acSMichael S. Tsirkin E1000_MANC_RMCP_EN, 246814cd3acSMichael S. Tsirkin }; 247814cd3acSMichael S. Tsirkin 248e9845f09SVincenzo Maffione /* Helper function, *curr == 0 means the value is not set */ 249e9845f09SVincenzo Maffione static inline void 250e9845f09SVincenzo Maffione mit_update_delay(uint32_t *curr, uint32_t value) 251e9845f09SVincenzo Maffione { 252e9845f09SVincenzo Maffione if (value && (*curr == 0 || value < *curr)) { 253e9845f09SVincenzo Maffione *curr = value; 254e9845f09SVincenzo Maffione } 255e9845f09SVincenzo Maffione } 256e9845f09SVincenzo Maffione 2577c23b892Sbalrog static void 2587c23b892Sbalrog set_interrupt_cause(E1000State *s, int index, uint32_t val) 2597c23b892Sbalrog { 260b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 261e9845f09SVincenzo Maffione uint32_t pending_ints; 262e9845f09SVincenzo Maffione uint32_t mit_delay; 263b08340d5SAndreas Färber 2647c23b892Sbalrog s->mac_reg[ICR] = val; 265a52a8841SMichael S. Tsirkin 266a52a8841SMichael S. Tsirkin /* 267a52a8841SMichael S. Tsirkin * Make sure ICR and ICS registers have the same value. 268a52a8841SMichael S. Tsirkin * The spec says that the ICS register is write-only. However in practice, 269a52a8841SMichael S. Tsirkin * on real hardware ICS is readable, and for reads it has the same value as 270a52a8841SMichael S. Tsirkin * ICR (except that ICS does not have the clear on read behaviour of ICR). 271a52a8841SMichael S. Tsirkin * 272a52a8841SMichael S. Tsirkin * The VxWorks PRO/1000 driver uses this behaviour. 273a52a8841SMichael S. Tsirkin */ 274b1332393SBill Paul s->mac_reg[ICS] = val; 275a52a8841SMichael S. Tsirkin 276e9845f09SVincenzo Maffione pending_ints = (s->mac_reg[IMS] & s->mac_reg[ICR]); 277e9845f09SVincenzo Maffione if (!s->mit_irq_level && pending_ints) { 278e9845f09SVincenzo Maffione /* 279e9845f09SVincenzo Maffione * Here we detect a potential raising edge. We postpone raising the 280e9845f09SVincenzo Maffione * interrupt line if we are inside the mitigation delay window 281e9845f09SVincenzo Maffione * (s->mit_timer_on == 1). 282e9845f09SVincenzo Maffione * We provide a partial implementation of interrupt mitigation, 283e9845f09SVincenzo Maffione * emulating only RADV, TADV and ITR (lower 16 bits, 1024ns units for 284e9845f09SVincenzo Maffione * RADV and TADV, 256ns units for ITR). RDTR is only used to enable 285e9845f09SVincenzo Maffione * RADV; relative timers based on TIDV and RDTR are not implemented. 286e9845f09SVincenzo Maffione */ 287e9845f09SVincenzo Maffione if (s->mit_timer_on) { 288e9845f09SVincenzo Maffione return; 289e9845f09SVincenzo Maffione } 290bc0f0674SLeonid Bloch if (chkflag(MIT)) { 291e9845f09SVincenzo Maffione /* Compute the next mitigation delay according to pending 292e9845f09SVincenzo Maffione * interrupts and the current values of RADV (provided 293e9845f09SVincenzo Maffione * RDTR!=0), TADV and ITR. 294e9845f09SVincenzo Maffione * Then rearm the timer. 295e9845f09SVincenzo Maffione */ 296e9845f09SVincenzo Maffione mit_delay = 0; 297e9845f09SVincenzo Maffione if (s->mit_ide && 298e9845f09SVincenzo Maffione (pending_ints & (E1000_ICR_TXQE | E1000_ICR_TXDW))) { 299e9845f09SVincenzo Maffione mit_update_delay(&mit_delay, s->mac_reg[TADV] * 4); 300e9845f09SVincenzo Maffione } 301e9845f09SVincenzo Maffione if (s->mac_reg[RDTR] && (pending_ints & E1000_ICS_RXT0)) { 302e9845f09SVincenzo Maffione mit_update_delay(&mit_delay, s->mac_reg[RADV] * 4); 303e9845f09SVincenzo Maffione } 304e9845f09SVincenzo Maffione mit_update_delay(&mit_delay, s->mac_reg[ITR]); 305e9845f09SVincenzo Maffione 30674004e8cSSameeh Jubran /* 30774004e8cSSameeh Jubran * According to e1000 SPEC, the Ethernet controller guarantees 30874004e8cSSameeh Jubran * a maximum observable interrupt rate of 7813 interrupts/sec. 30974004e8cSSameeh Jubran * Thus if mit_delay < 500 then the delay should be set to the 31074004e8cSSameeh Jubran * minimum delay possible which is 500. 31174004e8cSSameeh Jubran */ 31274004e8cSSameeh Jubran mit_delay = (mit_delay < 500) ? 500 : mit_delay; 31374004e8cSSameeh Jubran 314e9845f09SVincenzo Maffione if (mit_delay) { 315e9845f09SVincenzo Maffione s->mit_timer_on = 1; 316e9845f09SVincenzo Maffione timer_mod(s->mit_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 317e9845f09SVincenzo Maffione mit_delay * 256); 318e9845f09SVincenzo Maffione } 319e9845f09SVincenzo Maffione s->mit_ide = 0; 320e9845f09SVincenzo Maffione } 321e9845f09SVincenzo Maffione } 322e9845f09SVincenzo Maffione 323e9845f09SVincenzo Maffione s->mit_irq_level = (pending_ints != 0); 3249e64f8a3SMarcel Apfelbaum pci_set_irq(d, s->mit_irq_level); 325e9845f09SVincenzo Maffione } 326e9845f09SVincenzo Maffione 327e9845f09SVincenzo Maffione static void 328e9845f09SVincenzo Maffione e1000_mit_timer(void *opaque) 329e9845f09SVincenzo Maffione { 330e9845f09SVincenzo Maffione E1000State *s = opaque; 331e9845f09SVincenzo Maffione 332e9845f09SVincenzo Maffione s->mit_timer_on = 0; 333e9845f09SVincenzo Maffione /* Call set_interrupt_cause to update the irq level (if necessary). */ 334e9845f09SVincenzo Maffione set_interrupt_cause(s, 0, s->mac_reg[ICR]); 3357c23b892Sbalrog } 3367c23b892Sbalrog 3377c23b892Sbalrog static void 3387c23b892Sbalrog set_ics(E1000State *s, int index, uint32_t val) 3397c23b892Sbalrog { 3407c23b892Sbalrog DBGOUT(INTERRUPT, "set_ics %x, ICR %x, IMR %x\n", val, s->mac_reg[ICR], 3417c23b892Sbalrog s->mac_reg[IMS]); 3427c23b892Sbalrog set_interrupt_cause(s, 0, val | s->mac_reg[ICR]); 3437c23b892Sbalrog } 3447c23b892Sbalrog 345d52aec95SGabriel L. Somlo static void 346d52aec95SGabriel L. Somlo e1000_autoneg_timer(void *opaque) 347d52aec95SGabriel L. Somlo { 348d52aec95SGabriel L. Somlo E1000State *s = opaque; 349d52aec95SGabriel L. Somlo if (!qemu_get_queue(s->nic)->link_down) { 350*093454e2SDmitry Fleytman e1000_autoneg_done(s); 351d52aec95SGabriel L. Somlo set_ics(s, 0, E1000_ICS_LSC); /* signal link status change to guest */ 352d52aec95SGabriel L. Somlo } 353d52aec95SGabriel L. Somlo } 354d52aec95SGabriel L. Somlo 355814cd3acSMichael S. Tsirkin static void e1000_reset(void *opaque) 356814cd3acSMichael S. Tsirkin { 357814cd3acSMichael S. Tsirkin E1000State *d = opaque; 3588597f2e1SGabriel L. Somlo E1000BaseClass *edc = E1000_DEVICE_GET_CLASS(d); 359372254c6SGabriel L. Somlo uint8_t *macaddr = d->conf.macaddr.a; 360814cd3acSMichael S. Tsirkin 361bc72ad67SAlex Bligh timer_del(d->autoneg_timer); 362e9845f09SVincenzo Maffione timer_del(d->mit_timer); 363e9845f09SVincenzo Maffione d->mit_timer_on = 0; 364e9845f09SVincenzo Maffione d->mit_irq_level = 0; 365e9845f09SVincenzo Maffione d->mit_ide = 0; 366814cd3acSMichael S. Tsirkin memset(d->phy_reg, 0, sizeof d->phy_reg); 367814cd3acSMichael S. Tsirkin memmove(d->phy_reg, phy_reg_init, sizeof phy_reg_init); 3688597f2e1SGabriel L. Somlo d->phy_reg[PHY_ID2] = edc->phy_id2; 369814cd3acSMichael S. Tsirkin memset(d->mac_reg, 0, sizeof d->mac_reg); 370814cd3acSMichael S. Tsirkin memmove(d->mac_reg, mac_reg_init, sizeof mac_reg_init); 371814cd3acSMichael S. Tsirkin d->rxbuf_min_shift = 1; 372814cd3acSMichael S. Tsirkin memset(&d->tx, 0, sizeof d->tx); 373814cd3acSMichael S. Tsirkin 374b356f76dSJason Wang if (qemu_get_queue(d->nic)->link_down) { 375*093454e2SDmitry Fleytman e1000x_update_regs_on_link_down(d->mac_reg, d->phy_reg); 376814cd3acSMichael S. Tsirkin } 377372254c6SGabriel L. Somlo 378*093454e2SDmitry Fleytman e1000x_reset_mac_addr(d->nic, d->mac_reg, macaddr); 379814cd3acSMichael S. Tsirkin } 380814cd3acSMichael S. Tsirkin 3817c23b892Sbalrog static void 382cab3c825SKevin Wolf set_ctrl(E1000State *s, int index, uint32_t val) 383cab3c825SKevin Wolf { 384cab3c825SKevin Wolf /* RST is self clearing */ 385cab3c825SKevin Wolf s->mac_reg[CTRL] = val & ~E1000_CTRL_RST; 386cab3c825SKevin Wolf } 387cab3c825SKevin Wolf 388cab3c825SKevin Wolf static void 3897c23b892Sbalrog set_rx_control(E1000State *s, int index, uint32_t val) 3907c23b892Sbalrog { 3917c23b892Sbalrog s->mac_reg[RCTL] = val; 392*093454e2SDmitry Fleytman s->rxbuf_size = e1000x_rxbufsize(val); 3937c23b892Sbalrog s->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1; 3947c23b892Sbalrog DBGOUT(RX, "RCTL: %d, mac_reg[RCTL] = 0x%x\n", s->mac_reg[RDT], 3957c23b892Sbalrog s->mac_reg[RCTL]); 396b356f76dSJason Wang qemu_flush_queued_packets(qemu_get_queue(s->nic)); 3977c23b892Sbalrog } 3987c23b892Sbalrog 3997c23b892Sbalrog static void 4007c23b892Sbalrog set_mdic(E1000State *s, int index, uint32_t val) 4017c23b892Sbalrog { 4027c23b892Sbalrog uint32_t data = val & E1000_MDIC_DATA_MASK; 4037c23b892Sbalrog uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT); 4047c23b892Sbalrog 4057c23b892Sbalrog if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) // phy # 4067c23b892Sbalrog val = s->mac_reg[MDIC] | E1000_MDIC_ERROR; 4077c23b892Sbalrog else if (val & E1000_MDIC_OP_READ) { 4087c23b892Sbalrog DBGOUT(MDIC, "MDIC read reg 0x%x\n", addr); 4097c23b892Sbalrog if (!(phy_regcap[addr] & PHY_R)) { 4107c23b892Sbalrog DBGOUT(MDIC, "MDIC read reg %x unhandled\n", addr); 4117c23b892Sbalrog val |= E1000_MDIC_ERROR; 4127c23b892Sbalrog } else 4137c23b892Sbalrog val = (val ^ data) | s->phy_reg[addr]; 4147c23b892Sbalrog } else if (val & E1000_MDIC_OP_WRITE) { 4157c23b892Sbalrog DBGOUT(MDIC, "MDIC write reg 0x%x, value 0x%x\n", addr, data); 4167c23b892Sbalrog if (!(phy_regcap[addr] & PHY_W)) { 4177c23b892Sbalrog DBGOUT(MDIC, "MDIC write reg %x unhandled\n", addr); 4187c23b892Sbalrog val |= E1000_MDIC_ERROR; 419b9d03e35SJason Wang } else { 420b9d03e35SJason Wang if (addr < NPHYWRITEOPS && phyreg_writeops[addr]) { 421b9d03e35SJason Wang phyreg_writeops[addr](s, index, data); 4221195fed9SGabriel L. Somlo } else { 4237c23b892Sbalrog s->phy_reg[addr] = data; 4247c23b892Sbalrog } 425b9d03e35SJason Wang } 4261195fed9SGabriel L. Somlo } 4277c23b892Sbalrog s->mac_reg[MDIC] = val | E1000_MDIC_READY; 42817fbbb0bSJason Wang 42917fbbb0bSJason Wang if (val & E1000_MDIC_INT_EN) { 4307c23b892Sbalrog set_ics(s, 0, E1000_ICR_MDAC); 4317c23b892Sbalrog } 43217fbbb0bSJason Wang } 4337c23b892Sbalrog 4347c23b892Sbalrog static uint32_t 4357c23b892Sbalrog get_eecd(E1000State *s, int index) 4367c23b892Sbalrog { 4377c23b892Sbalrog uint32_t ret = E1000_EECD_PRES|E1000_EECD_GNT | s->eecd_state.old_eecd; 4387c23b892Sbalrog 4397c23b892Sbalrog DBGOUT(EEPROM, "reading eeprom bit %d (reading %d)\n", 4407c23b892Sbalrog s->eecd_state.bitnum_out, s->eecd_state.reading); 4417c23b892Sbalrog if (!s->eecd_state.reading || 4427c23b892Sbalrog ((s->eeprom_data[(s->eecd_state.bitnum_out >> 4) & 0x3f] >> 4437c23b892Sbalrog ((s->eecd_state.bitnum_out & 0xf) ^ 0xf))) & 1) 4447c23b892Sbalrog ret |= E1000_EECD_DO; 4457c23b892Sbalrog return ret; 4467c23b892Sbalrog } 4477c23b892Sbalrog 4487c23b892Sbalrog static void 4497c23b892Sbalrog set_eecd(E1000State *s, int index, uint32_t val) 4507c23b892Sbalrog { 4517c23b892Sbalrog uint32_t oldval = s->eecd_state.old_eecd; 4527c23b892Sbalrog 4537c23b892Sbalrog s->eecd_state.old_eecd = val & (E1000_EECD_SK | E1000_EECD_CS | 4547c23b892Sbalrog E1000_EECD_DI|E1000_EECD_FWE_MASK|E1000_EECD_REQ); 45520f3e863SLeonid Bloch if (!(E1000_EECD_CS & val)) { /* CS inactive; nothing to do */ 4569651ac55SIzumi Tsutsui return; 45720f3e863SLeonid Bloch } 45820f3e863SLeonid Bloch if (E1000_EECD_CS & (val ^ oldval)) { /* CS rise edge; reset state */ 4599651ac55SIzumi Tsutsui s->eecd_state.val_in = 0; 4609651ac55SIzumi Tsutsui s->eecd_state.bitnum_in = 0; 4619651ac55SIzumi Tsutsui s->eecd_state.bitnum_out = 0; 4629651ac55SIzumi Tsutsui s->eecd_state.reading = 0; 4639651ac55SIzumi Tsutsui } 46420f3e863SLeonid Bloch if (!(E1000_EECD_SK & (val ^ oldval))) { /* no clock edge */ 4657c23b892Sbalrog return; 46620f3e863SLeonid Bloch } 46720f3e863SLeonid Bloch if (!(E1000_EECD_SK & val)) { /* falling edge */ 4687c23b892Sbalrog s->eecd_state.bitnum_out++; 4697c23b892Sbalrog return; 4707c23b892Sbalrog } 4717c23b892Sbalrog s->eecd_state.val_in <<= 1; 4727c23b892Sbalrog if (val & E1000_EECD_DI) 4737c23b892Sbalrog s->eecd_state.val_in |= 1; 4747c23b892Sbalrog if (++s->eecd_state.bitnum_in == 9 && !s->eecd_state.reading) { 4757c23b892Sbalrog s->eecd_state.bitnum_out = ((s->eecd_state.val_in & 0x3f)<<4)-1; 4767c23b892Sbalrog s->eecd_state.reading = (((s->eecd_state.val_in >> 6) & 7) == 4777c23b892Sbalrog EEPROM_READ_OPCODE_MICROWIRE); 4787c23b892Sbalrog } 4797c23b892Sbalrog DBGOUT(EEPROM, "eeprom bitnum in %d out %d, reading %d\n", 4807c23b892Sbalrog s->eecd_state.bitnum_in, s->eecd_state.bitnum_out, 4817c23b892Sbalrog s->eecd_state.reading); 4827c23b892Sbalrog } 4837c23b892Sbalrog 4847c23b892Sbalrog static uint32_t 4857c23b892Sbalrog flash_eerd_read(E1000State *s, int x) 4867c23b892Sbalrog { 4877c23b892Sbalrog unsigned int index, r = s->mac_reg[EERD] & ~E1000_EEPROM_RW_REG_START; 4887c23b892Sbalrog 489b1332393SBill Paul if ((s->mac_reg[EERD] & E1000_EEPROM_RW_REG_START) == 0) 490b1332393SBill Paul return (s->mac_reg[EERD]); 491b1332393SBill Paul 4927c23b892Sbalrog if ((index = r >> E1000_EEPROM_RW_ADDR_SHIFT) > EEPROM_CHECKSUM_REG) 493b1332393SBill Paul return (E1000_EEPROM_RW_REG_DONE | r); 494b1332393SBill Paul 495b1332393SBill Paul return ((s->eeprom_data[index] << E1000_EEPROM_RW_REG_DATA) | 496b1332393SBill Paul E1000_EEPROM_RW_REG_DONE | r); 4977c23b892Sbalrog } 4987c23b892Sbalrog 4997c23b892Sbalrog static void 5007c23b892Sbalrog putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t css, uint32_t cse) 5017c23b892Sbalrog { 502c6a6a5e3Saliguori uint32_t sum; 503c6a6a5e3Saliguori 5047c23b892Sbalrog if (cse && cse < n) 5057c23b892Sbalrog n = cse + 1; 506c6a6a5e3Saliguori if (sloc < n-1) { 507c6a6a5e3Saliguori sum = net_checksum_add(n-css, data+css); 508d8ee2591SPeter Maydell stw_be_p(data + sloc, net_checksum_finish(sum)); 509c6a6a5e3Saliguori } 5107c23b892Sbalrog } 5117c23b892Sbalrog 5121f67f92cSLeonid Bloch static inline void 5133b274301SLeonid Bloch inc_tx_bcast_or_mcast_count(E1000State *s, const unsigned char *arr) 5143b274301SLeonid Bloch { 5153b274301SLeonid Bloch if (!memcmp(arr, bcast, sizeof bcast)) { 516*093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, BPTC); 5173b274301SLeonid Bloch } else if (arr[0] & 1) { 518*093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, MPTC); 5193b274301SLeonid Bloch } 5203b274301SLeonid Bloch } 5213b274301SLeonid Bloch 52245e93764SLeonid Bloch static void 52393e37d76SJason Wang e1000_send_packet(E1000State *s, const uint8_t *buf, int size) 52493e37d76SJason Wang { 5253b274301SLeonid Bloch static const int PTCregs[6] = { PTC64, PTC127, PTC255, PTC511, 5263b274301SLeonid Bloch PTC1023, PTC1522 }; 5273b274301SLeonid Bloch 528b356f76dSJason Wang NetClientState *nc = qemu_get_queue(s->nic); 52993e37d76SJason Wang if (s->phy_reg[PHY_CTRL] & MII_CR_LOOPBACK) { 530b356f76dSJason Wang nc->info->receive(nc, buf, size); 53193e37d76SJason Wang } else { 532b356f76dSJason Wang qemu_send_packet(nc, buf, size); 53393e37d76SJason Wang } 5343b274301SLeonid Bloch inc_tx_bcast_or_mcast_count(s, buf); 535*093454e2SDmitry Fleytman e1000x_increase_size_stats(s->mac_reg, PTCregs, size); 53693e37d76SJason Wang } 53793e37d76SJason Wang 53893e37d76SJason Wang static void 5397c23b892Sbalrog xmit_seg(E1000State *s) 5407c23b892Sbalrog { 5417c23b892Sbalrog uint16_t len, *sp; 54245e93764SLeonid Bloch unsigned int frames = s->tx.tso_frames, css, sofar; 5437c23b892Sbalrog struct e1000_tx *tp = &s->tx; 5447c23b892Sbalrog 545*093454e2SDmitry Fleytman if (tp->props.tse && tp->props.cptse) { 546*093454e2SDmitry Fleytman css = tp->props.ipcss; 5477c23b892Sbalrog DBGOUT(TXSUM, "frames %d size %d ipcss %d\n", 5487c23b892Sbalrog frames, tp->size, css); 549*093454e2SDmitry Fleytman if (tp->props.ip) { /* IPv4 */ 550d8ee2591SPeter Maydell stw_be_p(tp->data+css+2, tp->size - css); 551d8ee2591SPeter Maydell stw_be_p(tp->data+css+4, 5527c23b892Sbalrog be16_to_cpup((uint16_t *)(tp->data+css+4))+frames); 55320f3e863SLeonid Bloch } else { /* IPv6 */ 554d8ee2591SPeter Maydell stw_be_p(tp->data+css+4, tp->size - css); 55520f3e863SLeonid Bloch } 556*093454e2SDmitry Fleytman css = tp->props.tucss; 5577c23b892Sbalrog len = tp->size - css; 558*093454e2SDmitry Fleytman DBGOUT(TXSUM, "tcp %d tucss %d len %d\n", tp->props.tcp, css, len); 559*093454e2SDmitry Fleytman if (tp->props.tcp) { 560*093454e2SDmitry Fleytman sofar = frames * tp->props.mss; 5616bd194abSPeter Maydell stl_be_p(tp->data+css+4, ldl_be_p(tp->data+css+4)+sofar); /* seq */ 562*093454e2SDmitry Fleytman if (tp->props.paylen - sofar > tp->props.mss) { 56320f3e863SLeonid Bloch tp->data[css + 13] &= ~9; /* PSH, FIN */ 5643b274301SLeonid Bloch } else if (frames) { 565*093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, TSCTC); 5663b274301SLeonid Bloch } 56720f3e863SLeonid Bloch } else /* UDP */ 568d8ee2591SPeter Maydell stw_be_p(tp->data+css+4, len); 569*093454e2SDmitry Fleytman if (tp->props.sum_needed & E1000_TXD_POPTS_TXSM) { 570e685b4ebSAlex Williamson unsigned int phsum; 5717c23b892Sbalrog // add pseudo-header length before checksum calculation 572*093454e2SDmitry Fleytman sp = (uint16_t *)(tp->data + tp->props.tucso); 573e685b4ebSAlex Williamson phsum = be16_to_cpup(sp) + len; 574e685b4ebSAlex Williamson phsum = (phsum >> 16) + (phsum & 0xffff); 575d8ee2591SPeter Maydell stw_be_p(sp, phsum); 5767c23b892Sbalrog } 5777c23b892Sbalrog tp->tso_frames++; 5787c23b892Sbalrog } 5797c23b892Sbalrog 580*093454e2SDmitry Fleytman if (tp->props.sum_needed & E1000_TXD_POPTS_TXSM) { 581*093454e2SDmitry Fleytman putsum(tp->data, tp->size, tp->props.tucso, 582*093454e2SDmitry Fleytman tp->props.tucss, tp->props.tucse); 583*093454e2SDmitry Fleytman } 584*093454e2SDmitry Fleytman if (tp->props.sum_needed & E1000_TXD_POPTS_IXSM) { 585*093454e2SDmitry Fleytman putsum(tp->data, tp->size, tp->props.ipcso, 586*093454e2SDmitry Fleytman tp->props.ipcss, tp->props.ipcse); 587*093454e2SDmitry Fleytman } 5888f2e8d1fSaliguori if (tp->vlan_needed) { 589b10fec9bSStefan Weil memmove(tp->vlan, tp->data, 4); 590b10fec9bSStefan Weil memmove(tp->data, tp->data + 4, 8); 5918f2e8d1fSaliguori memcpy(tp->data + 8, tp->vlan_header, 4); 59293e37d76SJason Wang e1000_send_packet(s, tp->vlan, tp->size + 4); 59320f3e863SLeonid Bloch } else { 59493e37d76SJason Wang e1000_send_packet(s, tp->data, tp->size); 59520f3e863SLeonid Bloch } 59620f3e863SLeonid Bloch 597*093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, TPT); 598*093454e2SDmitry Fleytman e1000x_grow_8reg_if_not_full(s->mac_reg, TOTL, s->tx.size); 5991f67f92cSLeonid Bloch s->mac_reg[GPTC] = s->mac_reg[TPT]; 6003b274301SLeonid Bloch s->mac_reg[GOTCL] = s->mac_reg[TOTL]; 6013b274301SLeonid Bloch s->mac_reg[GOTCH] = s->mac_reg[TOTH]; 6027c23b892Sbalrog } 6037c23b892Sbalrog 6047c23b892Sbalrog static void 6057c23b892Sbalrog process_tx_desc(E1000State *s, struct e1000_tx_desc *dp) 6067c23b892Sbalrog { 607b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 6087c23b892Sbalrog uint32_t txd_lower = le32_to_cpu(dp->lower.data); 6097c23b892Sbalrog uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D); 610*093454e2SDmitry Fleytman unsigned int split_size = txd_lower & 0xffff, bytes, sz; 611a0ae17a6SAndrew Jones unsigned int msh = 0xfffff; 6127c23b892Sbalrog uint64_t addr; 6137c23b892Sbalrog struct e1000_context_desc *xp = (struct e1000_context_desc *)dp; 6147c23b892Sbalrog struct e1000_tx *tp = &s->tx; 6157c23b892Sbalrog 616e9845f09SVincenzo Maffione s->mit_ide |= (txd_lower & E1000_TXD_CMD_IDE); 61720f3e863SLeonid Bloch if (dtype == E1000_TXD_CMD_DEXT) { /* context descriptor */ 618*093454e2SDmitry Fleytman e1000x_read_tx_ctx_descr(xp, &tp->props); 6197c23b892Sbalrog tp->tso_frames = 0; 620*093454e2SDmitry Fleytman if (tp->props.tucso == 0) { /* this is probably wrong */ 6217c23b892Sbalrog DBGOUT(TXSUM, "TCP/UDP: cso 0!\n"); 622*093454e2SDmitry Fleytman tp->props.tucso = tp->props.tucss + (tp->props.tcp ? 16 : 6); 6237c23b892Sbalrog } 6247c23b892Sbalrog return; 6251b0009dbSbalrog } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) { 6261b0009dbSbalrog // data descriptor 627735e77ecSStefan Hajnoczi if (tp->size == 0) { 628*093454e2SDmitry Fleytman tp->props.sum_needed = le32_to_cpu(dp->upper.data) >> 8; 629735e77ecSStefan Hajnoczi } 630*093454e2SDmitry Fleytman tp->props.cptse = (txd_lower & E1000_TXD_CMD_TSE) ? 1 : 0; 63143ad7e3eSJes Sorensen } else { 6321b0009dbSbalrog // legacy descriptor 633*093454e2SDmitry Fleytman tp->props.cptse = 0; 63443ad7e3eSJes Sorensen } 6357c23b892Sbalrog 636*093454e2SDmitry Fleytman if (e1000x_vlan_enabled(s->mac_reg) && 637*093454e2SDmitry Fleytman e1000x_is_vlan_txd(txd_lower) && 638*093454e2SDmitry Fleytman (tp->props.cptse || txd_lower & E1000_TXD_CMD_EOP)) { 6398f2e8d1fSaliguori tp->vlan_needed = 1; 640d8ee2591SPeter Maydell stw_be_p(tp->vlan_header, 6414e60a250SShannon Zhao le16_to_cpu(s->mac_reg[VET])); 642d8ee2591SPeter Maydell stw_be_p(tp->vlan_header + 2, 6438f2e8d1fSaliguori le16_to_cpu(dp->upper.fields.special)); 6448f2e8d1fSaliguori } 6458f2e8d1fSaliguori 6467c23b892Sbalrog addr = le64_to_cpu(dp->buffer_addr); 647*093454e2SDmitry Fleytman if (tp->props.tse && tp->props.cptse) { 648*093454e2SDmitry Fleytman msh = tp->props.hdr_len + tp->props.mss; 6497c23b892Sbalrog do { 6507c23b892Sbalrog bytes = split_size; 6517c23b892Sbalrog if (tp->size + bytes > msh) 6527c23b892Sbalrog bytes = msh - tp->size; 65365f82df0SAnthony Liguori 65465f82df0SAnthony Liguori bytes = MIN(sizeof(tp->data) - tp->size, bytes); 655b08340d5SAndreas Färber pci_dma_read(d, addr, tp->data + tp->size, bytes); 656a0ae17a6SAndrew Jones sz = tp->size + bytes; 657*093454e2SDmitry Fleytman if (sz >= tp->props.hdr_len && tp->size < tp->props.hdr_len) { 658*093454e2SDmitry Fleytman memmove(tp->header, tp->data, tp->props.hdr_len); 659a0ae17a6SAndrew Jones } 6607c23b892Sbalrog tp->size = sz; 6617c23b892Sbalrog addr += bytes; 6627c23b892Sbalrog if (sz == msh) { 6637c23b892Sbalrog xmit_seg(s); 664*093454e2SDmitry Fleytman memmove(tp->data, tp->header, tp->props.hdr_len); 665*093454e2SDmitry Fleytman tp->size = tp->props.hdr_len; 6667c23b892Sbalrog } 667b947ac2bSP J P split_size -= bytes; 668b947ac2bSP J P } while (bytes && split_size); 669*093454e2SDmitry Fleytman } else if (!tp->props.tse && tp->props.cptse) { 6701b0009dbSbalrog // context descriptor TSE is not set, while data descriptor TSE is set 671362f5fb5SStefan Weil DBGOUT(TXERR, "TCP segmentation error\n"); 6721b0009dbSbalrog } else { 67365f82df0SAnthony Liguori split_size = MIN(sizeof(tp->data) - tp->size, split_size); 674b08340d5SAndreas Färber pci_dma_read(d, addr, tp->data + tp->size, split_size); 6751b0009dbSbalrog tp->size += split_size; 6761b0009dbSbalrog } 6777c23b892Sbalrog 6787c23b892Sbalrog if (!(txd_lower & E1000_TXD_CMD_EOP)) 6797c23b892Sbalrog return; 680*093454e2SDmitry Fleytman if (!(tp->props.tse && tp->props.cptse && tp->size < tp->props.hdr_len)) { 6817c23b892Sbalrog xmit_seg(s); 682a0ae17a6SAndrew Jones } 6837c23b892Sbalrog tp->tso_frames = 0; 684*093454e2SDmitry Fleytman tp->props.sum_needed = 0; 6858f2e8d1fSaliguori tp->vlan_needed = 0; 6867c23b892Sbalrog tp->size = 0; 687*093454e2SDmitry Fleytman tp->props.cptse = 0; 6887c23b892Sbalrog } 6897c23b892Sbalrog 6907c23b892Sbalrog static uint32_t 69162ecbd35SEduard - Gabriel Munteanu txdesc_writeback(E1000State *s, dma_addr_t base, struct e1000_tx_desc *dp) 6927c23b892Sbalrog { 693b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 6947c23b892Sbalrog uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data); 6957c23b892Sbalrog 6967c23b892Sbalrog if (!(txd_lower & (E1000_TXD_CMD_RS|E1000_TXD_CMD_RPS))) 6977c23b892Sbalrog return 0; 6987c23b892Sbalrog txd_upper = (le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD) & 6997c23b892Sbalrog ~(E1000_TXD_STAT_EC | E1000_TXD_STAT_LC | E1000_TXD_STAT_TU); 7007c23b892Sbalrog dp->upper.data = cpu_to_le32(txd_upper); 701b08340d5SAndreas Färber pci_dma_write(d, base + ((char *)&dp->upper - (char *)dp), 70200c3a05bSDavid Gibson &dp->upper, sizeof(dp->upper)); 7037c23b892Sbalrog return E1000_ICR_TXDW; 7047c23b892Sbalrog } 7057c23b892Sbalrog 706d17161f6SKevin Wolf static uint64_t tx_desc_base(E1000State *s) 707d17161f6SKevin Wolf { 708d17161f6SKevin Wolf uint64_t bah = s->mac_reg[TDBAH]; 709d17161f6SKevin Wolf uint64_t bal = s->mac_reg[TDBAL] & ~0xf; 710d17161f6SKevin Wolf 711d17161f6SKevin Wolf return (bah << 32) + bal; 712d17161f6SKevin Wolf } 713d17161f6SKevin Wolf 7147c23b892Sbalrog static void 7157c23b892Sbalrog start_xmit(E1000State *s) 7167c23b892Sbalrog { 717b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 71862ecbd35SEduard - Gabriel Munteanu dma_addr_t base; 7197c23b892Sbalrog struct e1000_tx_desc desc; 7207c23b892Sbalrog uint32_t tdh_start = s->mac_reg[TDH], cause = E1000_ICS_TXQE; 7217c23b892Sbalrog 7227c23b892Sbalrog if (!(s->mac_reg[TCTL] & E1000_TCTL_EN)) { 7237c23b892Sbalrog DBGOUT(TX, "tx disabled\n"); 7247c23b892Sbalrog return; 7257c23b892Sbalrog } 7267c23b892Sbalrog 7277c23b892Sbalrog while (s->mac_reg[TDH] != s->mac_reg[TDT]) { 728d17161f6SKevin Wolf base = tx_desc_base(s) + 7297c23b892Sbalrog sizeof(struct e1000_tx_desc) * s->mac_reg[TDH]; 730b08340d5SAndreas Färber pci_dma_read(d, base, &desc, sizeof(desc)); 7317c23b892Sbalrog 7327c23b892Sbalrog DBGOUT(TX, "index %d: %p : %x %x\n", s->mac_reg[TDH], 7336106075bSths (void *)(intptr_t)desc.buffer_addr, desc.lower.data, 7347c23b892Sbalrog desc.upper.data); 7357c23b892Sbalrog 7367c23b892Sbalrog process_tx_desc(s, &desc); 73762ecbd35SEduard - Gabriel Munteanu cause |= txdesc_writeback(s, base, &desc); 7387c23b892Sbalrog 7397c23b892Sbalrog if (++s->mac_reg[TDH] * sizeof(desc) >= s->mac_reg[TDLEN]) 7407c23b892Sbalrog s->mac_reg[TDH] = 0; 7417c23b892Sbalrog /* 7427c23b892Sbalrog * the following could happen only if guest sw assigns 7437c23b892Sbalrog * bogus values to TDT/TDLEN. 7447c23b892Sbalrog * there's nothing too intelligent we could do about this. 7457c23b892Sbalrog */ 746dd793a74SLaszlo Ersek if (s->mac_reg[TDH] == tdh_start || 747dd793a74SLaszlo Ersek tdh_start >= s->mac_reg[TDLEN] / sizeof(desc)) { 7487c23b892Sbalrog DBGOUT(TXERR, "TDH wraparound @%x, TDT %x, TDLEN %x\n", 7497c23b892Sbalrog tdh_start, s->mac_reg[TDT], s->mac_reg[TDLEN]); 7507c23b892Sbalrog break; 7517c23b892Sbalrog } 7527c23b892Sbalrog } 7537c23b892Sbalrog set_ics(s, 0, cause); 7547c23b892Sbalrog } 7557c23b892Sbalrog 7567c23b892Sbalrog static int 7577c23b892Sbalrog receive_filter(E1000State *s, const uint8_t *buf, int size) 7587c23b892Sbalrog { 759*093454e2SDmitry Fleytman uint32_t rctl = s->mac_reg[RCTL]; 7604aeea330SLeonid Bloch int isbcast = !memcmp(buf, bcast, sizeof bcast), ismcast = (buf[0] & 1); 7617c23b892Sbalrog 762*093454e2SDmitry Fleytman if (e1000x_is_vlan_packet(buf, le16_to_cpu(s->mac_reg[VET])) && 763*093454e2SDmitry Fleytman e1000x_vlan_rx_filter_enabled(s->mac_reg)) { 7648f2e8d1fSaliguori uint16_t vid = be16_to_cpup((uint16_t *)(buf + 14)); 7658f2e8d1fSaliguori uint32_t vfta = le32_to_cpup((uint32_t *)(s->mac_reg + VFTA) + 7668f2e8d1fSaliguori ((vid >> 5) & 0x7f)); 7678f2e8d1fSaliguori if ((vfta & (1 << (vid & 0x1f))) == 0) 7688f2e8d1fSaliguori return 0; 7698f2e8d1fSaliguori } 7708f2e8d1fSaliguori 7714aeea330SLeonid Bloch if (!isbcast && !ismcast && (rctl & E1000_RCTL_UPE)) { /* promiscuous ucast */ 7727c23b892Sbalrog return 1; 7734aeea330SLeonid Bloch } 7747c23b892Sbalrog 7754aeea330SLeonid Bloch if (ismcast && (rctl & E1000_RCTL_MPE)) { /* promiscuous mcast */ 776*093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, MPRC); 7777c23b892Sbalrog return 1; 7784aeea330SLeonid Bloch } 7797c23b892Sbalrog 7804aeea330SLeonid Bloch if (isbcast && (rctl & E1000_RCTL_BAM)) { /* broadcast enabled */ 781*093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, BPRC); 7827c23b892Sbalrog return 1; 7834aeea330SLeonid Bloch } 7847c23b892Sbalrog 785*093454e2SDmitry Fleytman return e1000x_rx_group_filter(s->mac_reg, buf); 7867c23b892Sbalrog } 7877c23b892Sbalrog 78899ed7e30Saliguori static void 7894e68f7a0SStefan Hajnoczi e1000_set_link_status(NetClientState *nc) 79099ed7e30Saliguori { 791cc1f0f45SJason Wang E1000State *s = qemu_get_nic_opaque(nc); 79299ed7e30Saliguori uint32_t old_status = s->mac_reg[STATUS]; 79399ed7e30Saliguori 794d4044c2aSBjørn Mork if (nc->link_down) { 795*093454e2SDmitry Fleytman e1000x_update_regs_on_link_down(s->mac_reg, s->phy_reg); 796d4044c2aSBjørn Mork } else { 797d7a41552SGabriel L. Somlo if (have_autoneg(s) && 7986a2acedbSGabriel L. Somlo !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) { 799*093454e2SDmitry Fleytman e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer); 8006a2acedbSGabriel L. Somlo } else { 80171aadd3cSJason Wang e1000_link_up(s); 802d4044c2aSBjørn Mork } 8036a2acedbSGabriel L. Somlo } 80499ed7e30Saliguori 80599ed7e30Saliguori if (s->mac_reg[STATUS] != old_status) 80699ed7e30Saliguori set_ics(s, 0, E1000_ICR_LSC); 80799ed7e30Saliguori } 80899ed7e30Saliguori 809322fd48aSMichael S. Tsirkin static bool e1000_has_rxbufs(E1000State *s, size_t total_size) 810322fd48aSMichael S. Tsirkin { 811322fd48aSMichael S. Tsirkin int bufs; 812322fd48aSMichael S. Tsirkin /* Fast-path short packets */ 813322fd48aSMichael S. Tsirkin if (total_size <= s->rxbuf_size) { 814e5b8b0d4SDmitry Fleytman return s->mac_reg[RDH] != s->mac_reg[RDT]; 815322fd48aSMichael S. Tsirkin } 816322fd48aSMichael S. Tsirkin if (s->mac_reg[RDH] < s->mac_reg[RDT]) { 817322fd48aSMichael S. Tsirkin bufs = s->mac_reg[RDT] - s->mac_reg[RDH]; 818e5b8b0d4SDmitry Fleytman } else if (s->mac_reg[RDH] > s->mac_reg[RDT]) { 819322fd48aSMichael S. Tsirkin bufs = s->mac_reg[RDLEN] / sizeof(struct e1000_rx_desc) + 820322fd48aSMichael S. Tsirkin s->mac_reg[RDT] - s->mac_reg[RDH]; 821322fd48aSMichael S. Tsirkin } else { 822322fd48aSMichael S. Tsirkin return false; 823322fd48aSMichael S. Tsirkin } 824322fd48aSMichael S. Tsirkin return total_size <= bufs * s->rxbuf_size; 825322fd48aSMichael S. Tsirkin } 826322fd48aSMichael S. Tsirkin 8276cdfab28SMichael S. Tsirkin static int 8284e68f7a0SStefan Hajnoczi e1000_can_receive(NetClientState *nc) 8296cdfab28SMichael S. Tsirkin { 830cc1f0f45SJason Wang E1000State *s = qemu_get_nic_opaque(nc); 8316cdfab28SMichael S. Tsirkin 832*093454e2SDmitry Fleytman return e1000x_rx_ready(&s->parent_obj, s->mac_reg) && 83320302e71SMichael S. Tsirkin e1000_has_rxbufs(s, 1); 8346cdfab28SMichael S. Tsirkin } 8356cdfab28SMichael S. Tsirkin 836d17161f6SKevin Wolf static uint64_t rx_desc_base(E1000State *s) 837d17161f6SKevin Wolf { 838d17161f6SKevin Wolf uint64_t bah = s->mac_reg[RDBAH]; 839d17161f6SKevin Wolf uint64_t bal = s->mac_reg[RDBAL] & ~0xf; 840d17161f6SKevin Wolf 841d17161f6SKevin Wolf return (bah << 32) + bal; 842d17161f6SKevin Wolf } 843d17161f6SKevin Wolf 8444f1c942bSMark McLoughlin static ssize_t 84597410ddeSVincenzo Maffione e1000_receive_iov(NetClientState *nc, const struct iovec *iov, int iovcnt) 8467c23b892Sbalrog { 847cc1f0f45SJason Wang E1000State *s = qemu_get_nic_opaque(nc); 848b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 8497c23b892Sbalrog struct e1000_rx_desc desc; 85062ecbd35SEduard - Gabriel Munteanu dma_addr_t base; 8517c23b892Sbalrog unsigned int n, rdt; 8527c23b892Sbalrog uint32_t rdh_start; 8538f2e8d1fSaliguori uint16_t vlan_special = 0; 85497410ddeSVincenzo Maffione uint8_t vlan_status = 0; 85578aeb23eSStefan Hajnoczi uint8_t min_buf[MIN_BUF_SIZE]; 85697410ddeSVincenzo Maffione struct iovec min_iov; 85797410ddeSVincenzo Maffione uint8_t *filter_buf = iov->iov_base; 85897410ddeSVincenzo Maffione size_t size = iov_size(iov, iovcnt); 85997410ddeSVincenzo Maffione size_t iov_ofs = 0; 860b19487e2SMichael S. Tsirkin size_t desc_offset; 861b19487e2SMichael S. Tsirkin size_t desc_size; 862b19487e2SMichael S. Tsirkin size_t total_size; 8637c23b892Sbalrog 864*093454e2SDmitry Fleytman if (!e1000x_hw_rx_enabled(s->mac_reg)) { 865ddcb73b7SMichael S. Tsirkin return -1; 866ddcb73b7SMichael S. Tsirkin } 8677c23b892Sbalrog 86878aeb23eSStefan Hajnoczi /* Pad to minimum Ethernet frame length */ 86978aeb23eSStefan Hajnoczi if (size < sizeof(min_buf)) { 87097410ddeSVincenzo Maffione iov_to_buf(iov, iovcnt, 0, min_buf, size); 87178aeb23eSStefan Hajnoczi memset(&min_buf[size], 0, sizeof(min_buf) - size); 872*093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, RUC); 87397410ddeSVincenzo Maffione min_iov.iov_base = filter_buf = min_buf; 87497410ddeSVincenzo Maffione min_iov.iov_len = size = sizeof(min_buf); 87597410ddeSVincenzo Maffione iovcnt = 1; 87697410ddeSVincenzo Maffione iov = &min_iov; 87797410ddeSVincenzo Maffione } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) { 87897410ddeSVincenzo Maffione /* This is very unlikely, but may happen. */ 87997410ddeSVincenzo Maffione iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN); 88097410ddeSVincenzo Maffione filter_buf = min_buf; 88178aeb23eSStefan Hajnoczi } 88278aeb23eSStefan Hajnoczi 883b0d9ffcdSMichael Contreras /* Discard oversized packets if !LPE and !SBP. */ 884*093454e2SDmitry Fleytman if (e1000x_is_oversized(s->mac_reg, size)) { 885b0d9ffcdSMichael Contreras return size; 886b0d9ffcdSMichael Contreras } 887b0d9ffcdSMichael Contreras 88897410ddeSVincenzo Maffione if (!receive_filter(s, filter_buf, size)) { 8894f1c942bSMark McLoughlin return size; 89097410ddeSVincenzo Maffione } 8917c23b892Sbalrog 892*093454e2SDmitry Fleytman if (e1000x_vlan_enabled(s->mac_reg) && 893*093454e2SDmitry Fleytman e1000x_is_vlan_packet(filter_buf, le16_to_cpu(s->mac_reg[VET]))) { 89497410ddeSVincenzo Maffione vlan_special = cpu_to_le16(be16_to_cpup((uint16_t *)(filter_buf 89597410ddeSVincenzo Maffione + 14))); 89697410ddeSVincenzo Maffione iov_ofs = 4; 89797410ddeSVincenzo Maffione if (filter_buf == iov->iov_base) { 89897410ddeSVincenzo Maffione memmove(filter_buf + 4, filter_buf, 12); 89997410ddeSVincenzo Maffione } else { 90097410ddeSVincenzo Maffione iov_from_buf(iov, iovcnt, 4, filter_buf, 12); 90197410ddeSVincenzo Maffione while (iov->iov_len <= iov_ofs) { 90297410ddeSVincenzo Maffione iov_ofs -= iov->iov_len; 90397410ddeSVincenzo Maffione iov++; 90497410ddeSVincenzo Maffione } 90597410ddeSVincenzo Maffione } 9068f2e8d1fSaliguori vlan_status = E1000_RXD_STAT_VP; 9078f2e8d1fSaliguori size -= 4; 9088f2e8d1fSaliguori } 9098f2e8d1fSaliguori 9107c23b892Sbalrog rdh_start = s->mac_reg[RDH]; 911b19487e2SMichael S. Tsirkin desc_offset = 0; 912*093454e2SDmitry Fleytman total_size = size + e1000x_fcs_len(s->mac_reg); 913322fd48aSMichael S. Tsirkin if (!e1000_has_rxbufs(s, total_size)) { 914322fd48aSMichael S. Tsirkin set_ics(s, 0, E1000_ICS_RXO); 915322fd48aSMichael S. Tsirkin return -1; 916322fd48aSMichael S. Tsirkin } 9177c23b892Sbalrog do { 918b19487e2SMichael S. Tsirkin desc_size = total_size - desc_offset; 919b19487e2SMichael S. Tsirkin if (desc_size > s->rxbuf_size) { 920b19487e2SMichael S. Tsirkin desc_size = s->rxbuf_size; 921b19487e2SMichael S. Tsirkin } 922d17161f6SKevin Wolf base = rx_desc_base(s) + sizeof(desc) * s->mac_reg[RDH]; 923b08340d5SAndreas Färber pci_dma_read(d, base, &desc, sizeof(desc)); 9248f2e8d1fSaliguori desc.special = vlan_special; 9258f2e8d1fSaliguori desc.status |= (vlan_status | E1000_RXD_STAT_DD); 9267c23b892Sbalrog if (desc.buffer_addr) { 927b19487e2SMichael S. Tsirkin if (desc_offset < size) { 92897410ddeSVincenzo Maffione size_t iov_copy; 92997410ddeSVincenzo Maffione hwaddr ba = le64_to_cpu(desc.buffer_addr); 930b19487e2SMichael S. Tsirkin size_t copy_size = size - desc_offset; 931b19487e2SMichael S. Tsirkin if (copy_size > s->rxbuf_size) { 932b19487e2SMichael S. Tsirkin copy_size = s->rxbuf_size; 933b19487e2SMichael S. Tsirkin } 93497410ddeSVincenzo Maffione do { 93597410ddeSVincenzo Maffione iov_copy = MIN(copy_size, iov->iov_len - iov_ofs); 93697410ddeSVincenzo Maffione pci_dma_write(d, ba, iov->iov_base + iov_ofs, iov_copy); 93797410ddeSVincenzo Maffione copy_size -= iov_copy; 93897410ddeSVincenzo Maffione ba += iov_copy; 93997410ddeSVincenzo Maffione iov_ofs += iov_copy; 94097410ddeSVincenzo Maffione if (iov_ofs == iov->iov_len) { 94197410ddeSVincenzo Maffione iov++; 94297410ddeSVincenzo Maffione iov_ofs = 0; 94397410ddeSVincenzo Maffione } 94497410ddeSVincenzo Maffione } while (copy_size); 945b19487e2SMichael S. Tsirkin } 946b19487e2SMichael S. Tsirkin desc_offset += desc_size; 947b19487e2SMichael S. Tsirkin desc.length = cpu_to_le16(desc_size); 948ee912ccfSMichael S. Tsirkin if (desc_offset >= total_size) { 9497c23b892Sbalrog desc.status |= E1000_RXD_STAT_EOP | E1000_RXD_STAT_IXSM; 950b19487e2SMichael S. Tsirkin } else { 951ee912ccfSMichael S. Tsirkin /* Guest zeroing out status is not a hardware requirement. 952ee912ccfSMichael S. Tsirkin Clear EOP in case guest didn't do it. */ 953ee912ccfSMichael S. Tsirkin desc.status &= ~E1000_RXD_STAT_EOP; 954b19487e2SMichael S. Tsirkin } 95543ad7e3eSJes Sorensen } else { // as per intel docs; skip descriptors with null buf addr 9567c23b892Sbalrog DBGOUT(RX, "Null RX descriptor!!\n"); 95743ad7e3eSJes Sorensen } 958b08340d5SAndreas Färber pci_dma_write(d, base, &desc, sizeof(desc)); 9597c23b892Sbalrog 9607c23b892Sbalrog if (++s->mac_reg[RDH] * sizeof(desc) >= s->mac_reg[RDLEN]) 9617c23b892Sbalrog s->mac_reg[RDH] = 0; 9627c23b892Sbalrog /* see comment in start_xmit; same here */ 963dd793a74SLaszlo Ersek if (s->mac_reg[RDH] == rdh_start || 964dd793a74SLaszlo Ersek rdh_start >= s->mac_reg[RDLEN] / sizeof(desc)) { 9657c23b892Sbalrog DBGOUT(RXERR, "RDH wraparound @%x, RDT %x, RDLEN %x\n", 9667c23b892Sbalrog rdh_start, s->mac_reg[RDT], s->mac_reg[RDLEN]); 9677c23b892Sbalrog set_ics(s, 0, E1000_ICS_RXO); 9684f1c942bSMark McLoughlin return -1; 9697c23b892Sbalrog } 970b19487e2SMichael S. Tsirkin } while (desc_offset < total_size); 9717c23b892Sbalrog 972*093454e2SDmitry Fleytman e1000x_update_rx_total_stats(s->mac_reg, size, total_size); 9737c23b892Sbalrog 9747c23b892Sbalrog n = E1000_ICS_RXT0; 9757c23b892Sbalrog if ((rdt = s->mac_reg[RDT]) < s->mac_reg[RDH]) 9767c23b892Sbalrog rdt += s->mac_reg[RDLEN] / sizeof(desc); 977bf16cc8fSaliguori if (((rdt - s->mac_reg[RDH]) * sizeof(desc)) <= s->mac_reg[RDLEN] >> 978bf16cc8fSaliguori s->rxbuf_min_shift) 9797c23b892Sbalrog n |= E1000_ICS_RXDMT0; 9807c23b892Sbalrog 9817c23b892Sbalrog set_ics(s, 0, n); 9824f1c942bSMark McLoughlin 9834f1c942bSMark McLoughlin return size; 9847c23b892Sbalrog } 9857c23b892Sbalrog 98697410ddeSVincenzo Maffione static ssize_t 98797410ddeSVincenzo Maffione e1000_receive(NetClientState *nc, const uint8_t *buf, size_t size) 98897410ddeSVincenzo Maffione { 98997410ddeSVincenzo Maffione const struct iovec iov = { 99097410ddeSVincenzo Maffione .iov_base = (uint8_t *)buf, 99197410ddeSVincenzo Maffione .iov_len = size 99297410ddeSVincenzo Maffione }; 99397410ddeSVincenzo Maffione 99497410ddeSVincenzo Maffione return e1000_receive_iov(nc, &iov, 1); 99597410ddeSVincenzo Maffione } 99697410ddeSVincenzo Maffione 9977c23b892Sbalrog static uint32_t 9987c23b892Sbalrog mac_readreg(E1000State *s, int index) 9997c23b892Sbalrog { 10007c23b892Sbalrog return s->mac_reg[index]; 10017c23b892Sbalrog } 10027c23b892Sbalrog 10037c23b892Sbalrog static uint32_t 100472ea771cSLeonid Bloch mac_low4_read(E1000State *s, int index) 100572ea771cSLeonid Bloch { 100672ea771cSLeonid Bloch return s->mac_reg[index] & 0xf; 100772ea771cSLeonid Bloch } 100872ea771cSLeonid Bloch 100972ea771cSLeonid Bloch static uint32_t 101072ea771cSLeonid Bloch mac_low11_read(E1000State *s, int index) 101172ea771cSLeonid Bloch { 101272ea771cSLeonid Bloch return s->mac_reg[index] & 0x7ff; 101372ea771cSLeonid Bloch } 101472ea771cSLeonid Bloch 101572ea771cSLeonid Bloch static uint32_t 101672ea771cSLeonid Bloch mac_low13_read(E1000State *s, int index) 101772ea771cSLeonid Bloch { 101872ea771cSLeonid Bloch return s->mac_reg[index] & 0x1fff; 101972ea771cSLeonid Bloch } 102072ea771cSLeonid Bloch 102172ea771cSLeonid Bloch static uint32_t 102272ea771cSLeonid Bloch mac_low16_read(E1000State *s, int index) 102372ea771cSLeonid Bloch { 102472ea771cSLeonid Bloch return s->mac_reg[index] & 0xffff; 102572ea771cSLeonid Bloch } 102672ea771cSLeonid Bloch 102772ea771cSLeonid Bloch static uint32_t 10287c23b892Sbalrog mac_icr_read(E1000State *s, int index) 10297c23b892Sbalrog { 10307c23b892Sbalrog uint32_t ret = s->mac_reg[ICR]; 10317c23b892Sbalrog 10327c23b892Sbalrog DBGOUT(INTERRUPT, "ICR read: %x\n", ret); 10337c23b892Sbalrog set_interrupt_cause(s, 0, 0); 10347c23b892Sbalrog return ret; 10357c23b892Sbalrog } 10367c23b892Sbalrog 10377c23b892Sbalrog static uint32_t 10387c23b892Sbalrog mac_read_clr4(E1000State *s, int index) 10397c23b892Sbalrog { 10407c23b892Sbalrog uint32_t ret = s->mac_reg[index]; 10417c23b892Sbalrog 10427c23b892Sbalrog s->mac_reg[index] = 0; 10437c23b892Sbalrog return ret; 10447c23b892Sbalrog } 10457c23b892Sbalrog 10467c23b892Sbalrog static uint32_t 10477c23b892Sbalrog mac_read_clr8(E1000State *s, int index) 10487c23b892Sbalrog { 10497c23b892Sbalrog uint32_t ret = s->mac_reg[index]; 10507c23b892Sbalrog 10517c23b892Sbalrog s->mac_reg[index] = 0; 10527c23b892Sbalrog s->mac_reg[index-1] = 0; 10537c23b892Sbalrog return ret; 10547c23b892Sbalrog } 10557c23b892Sbalrog 10567c23b892Sbalrog static void 10577c23b892Sbalrog mac_writereg(E1000State *s, int index, uint32_t val) 10587c23b892Sbalrog { 10597c36507cSAmos Kong uint32_t macaddr[2]; 10607c36507cSAmos Kong 10617c23b892Sbalrog s->mac_reg[index] = val; 10627c36507cSAmos Kong 106390d131fbSMichael S. Tsirkin if (index == RA + 1) { 10647c36507cSAmos Kong macaddr[0] = cpu_to_le32(s->mac_reg[RA]); 10657c36507cSAmos Kong macaddr[1] = cpu_to_le32(s->mac_reg[RA + 1]); 10667c36507cSAmos Kong qemu_format_nic_info_str(qemu_get_queue(s->nic), (uint8_t *)macaddr); 10677c36507cSAmos Kong } 10687c23b892Sbalrog } 10697c23b892Sbalrog 10707c23b892Sbalrog static void 10717c23b892Sbalrog set_rdt(E1000State *s, int index, uint32_t val) 10727c23b892Sbalrog { 10737c23b892Sbalrog s->mac_reg[index] = val & 0xffff; 1074e8b4c680SPaolo Bonzini if (e1000_has_rxbufs(s, 1)) { 1075b356f76dSJason Wang qemu_flush_queued_packets(qemu_get_queue(s->nic)); 1076e8b4c680SPaolo Bonzini } 10777c23b892Sbalrog } 10787c23b892Sbalrog 10797c23b892Sbalrog static void 10807c23b892Sbalrog set_16bit(E1000State *s, int index, uint32_t val) 10817c23b892Sbalrog { 10827c23b892Sbalrog s->mac_reg[index] = val & 0xffff; 10837c23b892Sbalrog } 10847c23b892Sbalrog 10857c23b892Sbalrog static void 10867c23b892Sbalrog set_dlen(E1000State *s, int index, uint32_t val) 10877c23b892Sbalrog { 10887c23b892Sbalrog s->mac_reg[index] = val & 0xfff80; 10897c23b892Sbalrog } 10907c23b892Sbalrog 10917c23b892Sbalrog static void 10927c23b892Sbalrog set_tctl(E1000State *s, int index, uint32_t val) 10937c23b892Sbalrog { 10947c23b892Sbalrog s->mac_reg[index] = val; 10957c23b892Sbalrog s->mac_reg[TDT] &= 0xffff; 10967c23b892Sbalrog start_xmit(s); 10977c23b892Sbalrog } 10987c23b892Sbalrog 10997c23b892Sbalrog static void 11007c23b892Sbalrog set_icr(E1000State *s, int index, uint32_t val) 11017c23b892Sbalrog { 11027c23b892Sbalrog DBGOUT(INTERRUPT, "set_icr %x\n", val); 11037c23b892Sbalrog set_interrupt_cause(s, 0, s->mac_reg[ICR] & ~val); 11047c23b892Sbalrog } 11057c23b892Sbalrog 11067c23b892Sbalrog static void 11077c23b892Sbalrog set_imc(E1000State *s, int index, uint32_t val) 11087c23b892Sbalrog { 11097c23b892Sbalrog s->mac_reg[IMS] &= ~val; 11107c23b892Sbalrog set_ics(s, 0, 0); 11117c23b892Sbalrog } 11127c23b892Sbalrog 11137c23b892Sbalrog static void 11147c23b892Sbalrog set_ims(E1000State *s, int index, uint32_t val) 11157c23b892Sbalrog { 11167c23b892Sbalrog s->mac_reg[IMS] |= val; 11177c23b892Sbalrog set_ics(s, 0, 0); 11187c23b892Sbalrog } 11197c23b892Sbalrog 11207c23b892Sbalrog #define getreg(x) [x] = mac_readreg 11217c23b892Sbalrog static uint32_t (*macreg_readops[])(E1000State *, int) = { 11227c23b892Sbalrog getreg(PBA), getreg(RCTL), getreg(TDH), getreg(TXDCTL), 11237c23b892Sbalrog getreg(WUFC), getreg(TDT), getreg(CTRL), getreg(LEDCTL), 11247c23b892Sbalrog getreg(MANC), getreg(MDIC), getreg(SWSM), getreg(STATUS), 11257c23b892Sbalrog getreg(TORL), getreg(TOTL), getreg(IMS), getreg(TCTL), 1126b1332393SBill Paul getreg(RDH), getreg(RDT), getreg(VET), getreg(ICS), 1127a00b2335SKay Ackermann getreg(TDBAL), getreg(TDBAH), getreg(RDBAH), getreg(RDBAL), 1128e9845f09SVincenzo Maffione getreg(TDLEN), getreg(RDLEN), getreg(RDTR), getreg(RADV), 112972ea771cSLeonid Bloch getreg(TADV), getreg(ITR), getreg(FCRUC), getreg(IPAV), 113072ea771cSLeonid Bloch getreg(WUC), getreg(WUS), getreg(SCC), getreg(ECOL), 113172ea771cSLeonid Bloch getreg(MCC), getreg(LATECOL), getreg(COLC), getreg(DC), 113272ea771cSLeonid Bloch getreg(TNCRS), getreg(SEC), getreg(CEXTERR), getreg(RLEC), 113372ea771cSLeonid Bloch getreg(XONRXC), getreg(XONTXC), getreg(XOFFRXC), getreg(XOFFTXC), 113472ea771cSLeonid Bloch getreg(RFC), getreg(RJC), getreg(RNBC), getreg(TSCTFC), 11353b274301SLeonid Bloch getreg(MGTPRC), getreg(MGTPDC), getreg(MGTPTC), getreg(GORCL), 11363b274301SLeonid Bloch getreg(GOTCL), 11377c23b892Sbalrog 113820f3e863SLeonid Bloch [TOTH] = mac_read_clr8, [TORH] = mac_read_clr8, 11393b274301SLeonid Bloch [GOTCH] = mac_read_clr8, [GORCH] = mac_read_clr8, 11403b274301SLeonid Bloch [PRC64] = mac_read_clr4, [PRC127] = mac_read_clr4, 11413b274301SLeonid Bloch [PRC255] = mac_read_clr4, [PRC511] = mac_read_clr4, 11423b274301SLeonid Bloch [PRC1023] = mac_read_clr4, [PRC1522] = mac_read_clr4, 11433b274301SLeonid Bloch [PTC64] = mac_read_clr4, [PTC127] = mac_read_clr4, 11443b274301SLeonid Bloch [PTC255] = mac_read_clr4, [PTC511] = mac_read_clr4, 11453b274301SLeonid Bloch [PTC1023] = mac_read_clr4, [PTC1522] = mac_read_clr4, 114620f3e863SLeonid Bloch [GPRC] = mac_read_clr4, [GPTC] = mac_read_clr4, 114720f3e863SLeonid Bloch [TPT] = mac_read_clr4, [TPR] = mac_read_clr4, 11483b274301SLeonid Bloch [RUC] = mac_read_clr4, [ROC] = mac_read_clr4, 11493b274301SLeonid Bloch [BPRC] = mac_read_clr4, [MPRC] = mac_read_clr4, 11503b274301SLeonid Bloch [TSCTC] = mac_read_clr4, [BPTC] = mac_read_clr4, 11513b274301SLeonid Bloch [MPTC] = mac_read_clr4, 115220f3e863SLeonid Bloch [ICR] = mac_icr_read, [EECD] = get_eecd, 115320f3e863SLeonid Bloch [EERD] = flash_eerd_read, 115472ea771cSLeonid Bloch [RDFH] = mac_low13_read, [RDFT] = mac_low13_read, 115572ea771cSLeonid Bloch [RDFHS] = mac_low13_read, [RDFTS] = mac_low13_read, 115672ea771cSLeonid Bloch [RDFPC] = mac_low13_read, 115772ea771cSLeonid Bloch [TDFH] = mac_low11_read, [TDFT] = mac_low11_read, 115872ea771cSLeonid Bloch [TDFHS] = mac_low13_read, [TDFTS] = mac_low13_read, 115972ea771cSLeonid Bloch [TDFPC] = mac_low13_read, 116072ea771cSLeonid Bloch [AIT] = mac_low16_read, 116120f3e863SLeonid Bloch 11627c23b892Sbalrog [CRCERRS ... MPC] = &mac_readreg, 116372ea771cSLeonid Bloch [IP6AT ... IP6AT+3] = &mac_readreg, [IP4AT ... IP4AT+6] = &mac_readreg, 116472ea771cSLeonid Bloch [FFLT ... FFLT+6] = &mac_low11_read, 11657c23b892Sbalrog [RA ... RA+31] = &mac_readreg, 116672ea771cSLeonid Bloch [WUPM ... WUPM+31] = &mac_readreg, 11677c23b892Sbalrog [MTA ... MTA+127] = &mac_readreg, 11688f2e8d1fSaliguori [VFTA ... VFTA+127] = &mac_readreg, 116972ea771cSLeonid Bloch [FFMT ... FFMT+254] = &mac_low4_read, 117072ea771cSLeonid Bloch [FFVT ... FFVT+254] = &mac_readreg, 117172ea771cSLeonid Bloch [PBM ... PBM+16383] = &mac_readreg, 11727c23b892Sbalrog }; 1173b1503cdaSmalc enum { NREADOPS = ARRAY_SIZE(macreg_readops) }; 11747c23b892Sbalrog 11757c23b892Sbalrog #define putreg(x) [x] = mac_writereg 11767c23b892Sbalrog static void (*macreg_writeops[])(E1000State *, int, uint32_t) = { 11777c23b892Sbalrog putreg(PBA), putreg(EERD), putreg(SWSM), putreg(WUFC), 11787c23b892Sbalrog putreg(TDBAL), putreg(TDBAH), putreg(TXDCTL), putreg(RDBAH), 117972ea771cSLeonid Bloch putreg(RDBAL), putreg(LEDCTL), putreg(VET), putreg(FCRUC), 118072ea771cSLeonid Bloch putreg(TDFH), putreg(TDFT), putreg(TDFHS), putreg(TDFTS), 118172ea771cSLeonid Bloch putreg(TDFPC), putreg(RDFH), putreg(RDFT), putreg(RDFHS), 118272ea771cSLeonid Bloch putreg(RDFTS), putreg(RDFPC), putreg(IPAV), putreg(WUC), 118372ea771cSLeonid Bloch putreg(WUS), putreg(AIT), 118420f3e863SLeonid Bloch 11857c23b892Sbalrog [TDLEN] = set_dlen, [RDLEN] = set_dlen, [TCTL] = set_tctl, 11867c23b892Sbalrog [TDT] = set_tctl, [MDIC] = set_mdic, [ICS] = set_ics, 11877c23b892Sbalrog [TDH] = set_16bit, [RDH] = set_16bit, [RDT] = set_rdt, 11887c23b892Sbalrog [IMC] = set_imc, [IMS] = set_ims, [ICR] = set_icr, 1189cab3c825SKevin Wolf [EECD] = set_eecd, [RCTL] = set_rx_control, [CTRL] = set_ctrl, 1190e9845f09SVincenzo Maffione [RDTR] = set_16bit, [RADV] = set_16bit, [TADV] = set_16bit, 1191e9845f09SVincenzo Maffione [ITR] = set_16bit, 119220f3e863SLeonid Bloch 119372ea771cSLeonid Bloch [IP6AT ... IP6AT+3] = &mac_writereg, [IP4AT ... IP4AT+6] = &mac_writereg, 119472ea771cSLeonid Bloch [FFLT ... FFLT+6] = &mac_writereg, 11957c23b892Sbalrog [RA ... RA+31] = &mac_writereg, 119672ea771cSLeonid Bloch [WUPM ... WUPM+31] = &mac_writereg, 11977c23b892Sbalrog [MTA ... MTA+127] = &mac_writereg, 11988f2e8d1fSaliguori [VFTA ... VFTA+127] = &mac_writereg, 119972ea771cSLeonid Bloch [FFMT ... FFMT+254] = &mac_writereg, [FFVT ... FFVT+254] = &mac_writereg, 120072ea771cSLeonid Bloch [PBM ... PBM+16383] = &mac_writereg, 12017c23b892Sbalrog }; 1202b9d03e35SJason Wang 1203b1503cdaSmalc enum { NWRITEOPS = ARRAY_SIZE(macreg_writeops) }; 12047c23b892Sbalrog 1205bc0f0674SLeonid Bloch enum { MAC_ACCESS_PARTIAL = 1, MAC_ACCESS_FLAG_NEEDED = 2 }; 1206bc0f0674SLeonid Bloch 1207bc0f0674SLeonid Bloch #define markflag(x) ((E1000_FLAG_##x << 2) | MAC_ACCESS_FLAG_NEEDED) 1208bc0f0674SLeonid Bloch /* In the array below the meaning of the bits is: [f|f|f|f|f|f|n|p] 1209bc0f0674SLeonid Bloch * f - flag bits (up to 6 possible flags) 1210bc0f0674SLeonid Bloch * n - flag needed 1211bc0f0674SLeonid Bloch * p - partially implenented */ 1212bc0f0674SLeonid Bloch static const uint8_t mac_reg_access[0x8000] = { 1213bc0f0674SLeonid Bloch [RDTR] = markflag(MIT), [TADV] = markflag(MIT), 1214bc0f0674SLeonid Bloch [RADV] = markflag(MIT), [ITR] = markflag(MIT), 121572ea771cSLeonid Bloch 121672ea771cSLeonid Bloch [IPAV] = markflag(MAC), [WUC] = markflag(MAC), 121772ea771cSLeonid Bloch [IP6AT] = markflag(MAC), [IP4AT] = markflag(MAC), 121872ea771cSLeonid Bloch [FFVT] = markflag(MAC), [WUPM] = markflag(MAC), 121972ea771cSLeonid Bloch [ECOL] = markflag(MAC), [MCC] = markflag(MAC), 122072ea771cSLeonid Bloch [DC] = markflag(MAC), [TNCRS] = markflag(MAC), 122172ea771cSLeonid Bloch [RLEC] = markflag(MAC), [XONRXC] = markflag(MAC), 122272ea771cSLeonid Bloch [XOFFTXC] = markflag(MAC), [RFC] = markflag(MAC), 122372ea771cSLeonid Bloch [TSCTFC] = markflag(MAC), [MGTPRC] = markflag(MAC), 122472ea771cSLeonid Bloch [WUS] = markflag(MAC), [AIT] = markflag(MAC), 122572ea771cSLeonid Bloch [FFLT] = markflag(MAC), [FFMT] = markflag(MAC), 122672ea771cSLeonid Bloch [SCC] = markflag(MAC), [FCRUC] = markflag(MAC), 122772ea771cSLeonid Bloch [LATECOL] = markflag(MAC), [COLC] = markflag(MAC), 122872ea771cSLeonid Bloch [SEC] = markflag(MAC), [CEXTERR] = markflag(MAC), 122972ea771cSLeonid Bloch [XONTXC] = markflag(MAC), [XOFFRXC] = markflag(MAC), 123072ea771cSLeonid Bloch [RJC] = markflag(MAC), [RNBC] = markflag(MAC), 123172ea771cSLeonid Bloch [MGTPDC] = markflag(MAC), [MGTPTC] = markflag(MAC), 12323b274301SLeonid Bloch [RUC] = markflag(MAC), [ROC] = markflag(MAC), 12333b274301SLeonid Bloch [GORCL] = markflag(MAC), [GORCH] = markflag(MAC), 12343b274301SLeonid Bloch [GOTCL] = markflag(MAC), [GOTCH] = markflag(MAC), 12353b274301SLeonid Bloch [BPRC] = markflag(MAC), [MPRC] = markflag(MAC), 12363b274301SLeonid Bloch [TSCTC] = markflag(MAC), [PRC64] = markflag(MAC), 12373b274301SLeonid Bloch [PRC127] = markflag(MAC), [PRC255] = markflag(MAC), 12383b274301SLeonid Bloch [PRC511] = markflag(MAC), [PRC1023] = markflag(MAC), 12393b274301SLeonid Bloch [PRC1522] = markflag(MAC), [PTC64] = markflag(MAC), 12403b274301SLeonid Bloch [PTC127] = markflag(MAC), [PTC255] = markflag(MAC), 12413b274301SLeonid Bloch [PTC511] = markflag(MAC), [PTC1023] = markflag(MAC), 12423b274301SLeonid Bloch [PTC1522] = markflag(MAC), [MPTC] = markflag(MAC), 12433b274301SLeonid Bloch [BPTC] = markflag(MAC), 124472ea771cSLeonid Bloch 124572ea771cSLeonid Bloch [TDFH] = markflag(MAC) | MAC_ACCESS_PARTIAL, 124672ea771cSLeonid Bloch [TDFT] = markflag(MAC) | MAC_ACCESS_PARTIAL, 124772ea771cSLeonid Bloch [TDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL, 124872ea771cSLeonid Bloch [TDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL, 124972ea771cSLeonid Bloch [TDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL, 125072ea771cSLeonid Bloch [RDFH] = markflag(MAC) | MAC_ACCESS_PARTIAL, 125172ea771cSLeonid Bloch [RDFT] = markflag(MAC) | MAC_ACCESS_PARTIAL, 125272ea771cSLeonid Bloch [RDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL, 125372ea771cSLeonid Bloch [RDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL, 125472ea771cSLeonid Bloch [RDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL, 125572ea771cSLeonid Bloch [PBM] = markflag(MAC) | MAC_ACCESS_PARTIAL, 1256bc0f0674SLeonid Bloch }; 1257bc0f0674SLeonid Bloch 12587c23b892Sbalrog static void 1259a8170e5eSAvi Kivity e1000_mmio_write(void *opaque, hwaddr addr, uint64_t val, 1260ad00a9b9SAvi Kivity unsigned size) 12617c23b892Sbalrog { 12627c23b892Sbalrog E1000State *s = opaque; 12638da3ff18Spbrook unsigned int index = (addr & 0x1ffff) >> 2; 12647c23b892Sbalrog 126543ad7e3eSJes Sorensen if (index < NWRITEOPS && macreg_writeops[index]) { 1266bc0f0674SLeonid Bloch if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED) 1267bc0f0674SLeonid Bloch || (s->compat_flags & (mac_reg_access[index] >> 2))) { 1268bc0f0674SLeonid Bloch if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) { 1269bc0f0674SLeonid Bloch DBGOUT(GENERAL, "Writing to register at offset: 0x%08x. " 1270bc0f0674SLeonid Bloch "It is not fully implemented.\n", index<<2); 1271bc0f0674SLeonid Bloch } 12726b59fc74Saurel32 macreg_writeops[index](s, index, val); 1273bc0f0674SLeonid Bloch } else { /* "flag needed" bit is set, but the flag is not active */ 1274bc0f0674SLeonid Bloch DBGOUT(MMIO, "MMIO write attempt to disabled reg. addr=0x%08x\n", 1275bc0f0674SLeonid Bloch index<<2); 1276bc0f0674SLeonid Bloch } 127743ad7e3eSJes Sorensen } else if (index < NREADOPS && macreg_readops[index]) { 1278bc0f0674SLeonid Bloch DBGOUT(MMIO, "e1000_mmio_writel RO %x: 0x%04"PRIx64"\n", 1279bc0f0674SLeonid Bloch index<<2, val); 128043ad7e3eSJes Sorensen } else { 1281ad00a9b9SAvi Kivity DBGOUT(UNKNOWN, "MMIO unknown write addr=0x%08x,val=0x%08"PRIx64"\n", 12827c23b892Sbalrog index<<2, val); 12837c23b892Sbalrog } 128443ad7e3eSJes Sorensen } 12857c23b892Sbalrog 1286ad00a9b9SAvi Kivity static uint64_t 1287a8170e5eSAvi Kivity e1000_mmio_read(void *opaque, hwaddr addr, unsigned size) 12887c23b892Sbalrog { 12897c23b892Sbalrog E1000State *s = opaque; 12908da3ff18Spbrook unsigned int index = (addr & 0x1ffff) >> 2; 12917c23b892Sbalrog 1292bc0f0674SLeonid Bloch if (index < NREADOPS && macreg_readops[index]) { 1293bc0f0674SLeonid Bloch if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED) 1294bc0f0674SLeonid Bloch || (s->compat_flags & (mac_reg_access[index] >> 2))) { 1295bc0f0674SLeonid Bloch if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) { 1296bc0f0674SLeonid Bloch DBGOUT(GENERAL, "Reading register at offset: 0x%08x. " 1297bc0f0674SLeonid Bloch "It is not fully implemented.\n", index<<2); 12986b59fc74Saurel32 } 1299bc0f0674SLeonid Bloch return macreg_readops[index](s, index); 1300bc0f0674SLeonid Bloch } else { /* "flag needed" bit is set, but the flag is not active */ 1301bc0f0674SLeonid Bloch DBGOUT(MMIO, "MMIO read attempt of disabled reg. addr=0x%08x\n", 1302bc0f0674SLeonid Bloch index<<2); 1303bc0f0674SLeonid Bloch } 1304bc0f0674SLeonid Bloch } else { 13057c23b892Sbalrog DBGOUT(UNKNOWN, "MMIO unknown read addr=0x%08x\n", index<<2); 1306bc0f0674SLeonid Bloch } 13077c23b892Sbalrog return 0; 13087c23b892Sbalrog } 13097c23b892Sbalrog 1310ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_mmio_ops = { 1311ad00a9b9SAvi Kivity .read = e1000_mmio_read, 1312ad00a9b9SAvi Kivity .write = e1000_mmio_write, 1313ad00a9b9SAvi Kivity .endianness = DEVICE_LITTLE_ENDIAN, 1314ad00a9b9SAvi Kivity .impl = { 1315ad00a9b9SAvi Kivity .min_access_size = 4, 1316ad00a9b9SAvi Kivity .max_access_size = 4, 1317ad00a9b9SAvi Kivity }, 1318ad00a9b9SAvi Kivity }; 1319ad00a9b9SAvi Kivity 1320a8170e5eSAvi Kivity static uint64_t e1000_io_read(void *opaque, hwaddr addr, 1321ad00a9b9SAvi Kivity unsigned size) 13227c23b892Sbalrog { 1323ad00a9b9SAvi Kivity E1000State *s = opaque; 1324ad00a9b9SAvi Kivity 1325ad00a9b9SAvi Kivity (void)s; 1326ad00a9b9SAvi Kivity return 0; 13277c23b892Sbalrog } 13287c23b892Sbalrog 1329a8170e5eSAvi Kivity static void e1000_io_write(void *opaque, hwaddr addr, 1330ad00a9b9SAvi Kivity uint64_t val, unsigned size) 13317c23b892Sbalrog { 1332ad00a9b9SAvi Kivity E1000State *s = opaque; 1333ad00a9b9SAvi Kivity 1334ad00a9b9SAvi Kivity (void)s; 13357c23b892Sbalrog } 13367c23b892Sbalrog 1337ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_io_ops = { 1338ad00a9b9SAvi Kivity .read = e1000_io_read, 1339ad00a9b9SAvi Kivity .write = e1000_io_write, 1340ad00a9b9SAvi Kivity .endianness = DEVICE_LITTLE_ENDIAN, 1341ad00a9b9SAvi Kivity }; 1342ad00a9b9SAvi Kivity 1343e482dc3eSJuan Quintela static bool is_version_1(void *opaque, int version_id) 13447c23b892Sbalrog { 1345e482dc3eSJuan Quintela return version_id == 1; 13467c23b892Sbalrog } 13477c23b892Sbalrog 1348ddcb73b7SMichael S. Tsirkin static void e1000_pre_save(void *opaque) 1349ddcb73b7SMichael S. Tsirkin { 1350ddcb73b7SMichael S. Tsirkin E1000State *s = opaque; 1351ddcb73b7SMichael S. Tsirkin NetClientState *nc = qemu_get_queue(s->nic); 13522af234e6SMichael S. Tsirkin 1353e9845f09SVincenzo Maffione /* If the mitigation timer is active, emulate a timeout now. */ 1354e9845f09SVincenzo Maffione if (s->mit_timer_on) { 1355e9845f09SVincenzo Maffione e1000_mit_timer(s); 1356e9845f09SVincenzo Maffione } 1357e9845f09SVincenzo Maffione 1358ddcb73b7SMichael S. Tsirkin /* 13596a2acedbSGabriel L. Somlo * If link is down and auto-negotiation is supported and ongoing, 13606a2acedbSGabriel L. Somlo * complete auto-negotiation immediately. This allows us to look 13616a2acedbSGabriel L. Somlo * at MII_SR_AUTONEG_COMPLETE to infer link status on load. 1362ddcb73b7SMichael S. Tsirkin */ 1363d7a41552SGabriel L. Somlo if (nc->link_down && have_autoneg(s)) { 1364ddcb73b7SMichael S. Tsirkin s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE; 1365ddcb73b7SMichael S. Tsirkin } 1366ddcb73b7SMichael S. Tsirkin } 1367ddcb73b7SMichael S. Tsirkin 1368e4b82364SAmos Kong static int e1000_post_load(void *opaque, int version_id) 1369e4b82364SAmos Kong { 1370e4b82364SAmos Kong E1000State *s = opaque; 1371b356f76dSJason Wang NetClientState *nc = qemu_get_queue(s->nic); 1372e4b82364SAmos Kong 1373bc0f0674SLeonid Bloch if (!chkflag(MIT)) { 1374e9845f09SVincenzo Maffione s->mac_reg[ITR] = s->mac_reg[RDTR] = s->mac_reg[RADV] = 1375e9845f09SVincenzo Maffione s->mac_reg[TADV] = 0; 1376e9845f09SVincenzo Maffione s->mit_irq_level = false; 1377e9845f09SVincenzo Maffione } 1378e9845f09SVincenzo Maffione s->mit_ide = 0; 1379e9845f09SVincenzo Maffione s->mit_timer_on = false; 1380e9845f09SVincenzo Maffione 1381e4b82364SAmos Kong /* nc.link_down can't be migrated, so infer link_down according 1382ddcb73b7SMichael S. Tsirkin * to link status bit in mac_reg[STATUS]. 1383ddcb73b7SMichael S. Tsirkin * Alternatively, restart link negotiation if it was in progress. */ 1384b356f76dSJason Wang nc->link_down = (s->mac_reg[STATUS] & E1000_STATUS_LU) == 0; 13852af234e6SMichael S. Tsirkin 1386d7a41552SGabriel L. Somlo if (have_autoneg(s) && 1387ddcb73b7SMichael S. Tsirkin !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) { 1388ddcb73b7SMichael S. Tsirkin nc->link_down = false; 1389d7a41552SGabriel L. Somlo timer_mod(s->autoneg_timer, 1390d7a41552SGabriel L. Somlo qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500); 1391ddcb73b7SMichael S. Tsirkin } 1392e4b82364SAmos Kong 1393e4b82364SAmos Kong return 0; 1394e4b82364SAmos Kong } 1395e4b82364SAmos Kong 1396e9845f09SVincenzo Maffione static bool e1000_mit_state_needed(void *opaque) 1397e9845f09SVincenzo Maffione { 1398e9845f09SVincenzo Maffione E1000State *s = opaque; 1399e9845f09SVincenzo Maffione 1400bc0f0674SLeonid Bloch return chkflag(MIT); 1401e9845f09SVincenzo Maffione } 1402e9845f09SVincenzo Maffione 14039e117734SLeonid Bloch static bool e1000_full_mac_needed(void *opaque) 14049e117734SLeonid Bloch { 14059e117734SLeonid Bloch E1000State *s = opaque; 14069e117734SLeonid Bloch 1407bc0f0674SLeonid Bloch return chkflag(MAC); 14089e117734SLeonid Bloch } 14099e117734SLeonid Bloch 1410e9845f09SVincenzo Maffione static const VMStateDescription vmstate_e1000_mit_state = { 1411e9845f09SVincenzo Maffione .name = "e1000/mit_state", 1412e9845f09SVincenzo Maffione .version_id = 1, 1413e9845f09SVincenzo Maffione .minimum_version_id = 1, 14145cd8cadaSJuan Quintela .needed = e1000_mit_state_needed, 1415e9845f09SVincenzo Maffione .fields = (VMStateField[]) { 1416e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[RDTR], E1000State), 1417e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[RADV], E1000State), 1418e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[TADV], E1000State), 1419e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[ITR], E1000State), 1420e9845f09SVincenzo Maffione VMSTATE_BOOL(mit_irq_level, E1000State), 1421e9845f09SVincenzo Maffione VMSTATE_END_OF_LIST() 1422e9845f09SVincenzo Maffione } 1423e9845f09SVincenzo Maffione }; 1424e9845f09SVincenzo Maffione 14259e117734SLeonid Bloch static const VMStateDescription vmstate_e1000_full_mac_state = { 14269e117734SLeonid Bloch .name = "e1000/full_mac_state", 14279e117734SLeonid Bloch .version_id = 1, 14289e117734SLeonid Bloch .minimum_version_id = 1, 14299e117734SLeonid Bloch .needed = e1000_full_mac_needed, 14309e117734SLeonid Bloch .fields = (VMStateField[]) { 14319e117734SLeonid Bloch VMSTATE_UINT32_ARRAY(mac_reg, E1000State, 0x8000), 14329e117734SLeonid Bloch VMSTATE_END_OF_LIST() 14339e117734SLeonid Bloch } 14349e117734SLeonid Bloch }; 14359e117734SLeonid Bloch 1436e482dc3eSJuan Quintela static const VMStateDescription vmstate_e1000 = { 1437e482dc3eSJuan Quintela .name = "e1000", 1438e482dc3eSJuan Quintela .version_id = 2, 1439e482dc3eSJuan Quintela .minimum_version_id = 1, 1440ddcb73b7SMichael S. Tsirkin .pre_save = e1000_pre_save, 1441e4b82364SAmos Kong .post_load = e1000_post_load, 1442e482dc3eSJuan Quintela .fields = (VMStateField[]) { 1443b08340d5SAndreas Färber VMSTATE_PCI_DEVICE(parent_obj, E1000State), 1444e482dc3eSJuan Quintela VMSTATE_UNUSED_TEST(is_version_1, 4), /* was instance id */ 1445e482dc3eSJuan Quintela VMSTATE_UNUSED(4), /* Was mmio_base. */ 1446e482dc3eSJuan Quintela VMSTATE_UINT32(rxbuf_size, E1000State), 1447e482dc3eSJuan Quintela VMSTATE_UINT32(rxbuf_min_shift, E1000State), 1448e482dc3eSJuan Quintela VMSTATE_UINT32(eecd_state.val_in, E1000State), 1449e482dc3eSJuan Quintela VMSTATE_UINT16(eecd_state.bitnum_in, E1000State), 1450e482dc3eSJuan Quintela VMSTATE_UINT16(eecd_state.bitnum_out, E1000State), 1451e482dc3eSJuan Quintela VMSTATE_UINT16(eecd_state.reading, E1000State), 1452e482dc3eSJuan Quintela VMSTATE_UINT32(eecd_state.old_eecd, E1000State), 1453*093454e2SDmitry Fleytman VMSTATE_UINT8(tx.props.ipcss, E1000State), 1454*093454e2SDmitry Fleytman VMSTATE_UINT8(tx.props.ipcso, E1000State), 1455*093454e2SDmitry Fleytman VMSTATE_UINT16(tx.props.ipcse, E1000State), 1456*093454e2SDmitry Fleytman VMSTATE_UINT8(tx.props.tucss, E1000State), 1457*093454e2SDmitry Fleytman VMSTATE_UINT8(tx.props.tucso, E1000State), 1458*093454e2SDmitry Fleytman VMSTATE_UINT16(tx.props.tucse, E1000State), 1459*093454e2SDmitry Fleytman VMSTATE_UINT32(tx.props.paylen, E1000State), 1460*093454e2SDmitry Fleytman VMSTATE_UINT8(tx.props.hdr_len, E1000State), 1461*093454e2SDmitry Fleytman VMSTATE_UINT16(tx.props.mss, E1000State), 1462e482dc3eSJuan Quintela VMSTATE_UINT16(tx.size, E1000State), 1463e482dc3eSJuan Quintela VMSTATE_UINT16(tx.tso_frames, E1000State), 1464*093454e2SDmitry Fleytman VMSTATE_UINT8(tx.props.sum_needed, E1000State), 1465*093454e2SDmitry Fleytman VMSTATE_INT8(tx.props.ip, E1000State), 1466*093454e2SDmitry Fleytman VMSTATE_INT8(tx.props.tcp, E1000State), 1467e482dc3eSJuan Quintela VMSTATE_BUFFER(tx.header, E1000State), 1468e482dc3eSJuan Quintela VMSTATE_BUFFER(tx.data, E1000State), 1469e482dc3eSJuan Quintela VMSTATE_UINT16_ARRAY(eeprom_data, E1000State, 64), 1470e482dc3eSJuan Quintela VMSTATE_UINT16_ARRAY(phy_reg, E1000State, 0x20), 1471e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[CTRL], E1000State), 1472e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[EECD], E1000State), 1473e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[EERD], E1000State), 1474e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[GPRC], E1000State), 1475e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[GPTC], E1000State), 1476e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[ICR], E1000State), 1477e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[ICS], E1000State), 1478e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[IMC], E1000State), 1479e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[IMS], E1000State), 1480e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[LEDCTL], E1000State), 1481e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[MANC], E1000State), 1482e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[MDIC], E1000State), 1483e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[MPC], E1000State), 1484e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[PBA], E1000State), 1485e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RCTL], E1000State), 1486e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDBAH], E1000State), 1487e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDBAL], E1000State), 1488e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDH], E1000State), 1489e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDLEN], E1000State), 1490e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDT], E1000State), 1491e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[STATUS], E1000State), 1492e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[SWSM], E1000State), 1493e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TCTL], E1000State), 1494e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDBAH], E1000State), 1495e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDBAL], E1000State), 1496e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDH], E1000State), 1497e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDLEN], E1000State), 1498e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDT], E1000State), 1499e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TORH], E1000State), 1500e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TORL], E1000State), 1501e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TOTH], E1000State), 1502e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TOTL], E1000State), 1503e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TPR], E1000State), 1504e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TPT], E1000State), 1505e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TXDCTL], E1000State), 1506e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[WUFC], E1000State), 1507e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[VET], E1000State), 1508e482dc3eSJuan Quintela VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, RA, 32), 1509e482dc3eSJuan Quintela VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, MTA, 128), 1510e482dc3eSJuan Quintela VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, VFTA, 128), 1511e482dc3eSJuan Quintela VMSTATE_END_OF_LIST() 1512e9845f09SVincenzo Maffione }, 15135cd8cadaSJuan Quintela .subsections = (const VMStateDescription*[]) { 15145cd8cadaSJuan Quintela &vmstate_e1000_mit_state, 15159e117734SLeonid Bloch &vmstate_e1000_full_mac_state, 15165cd8cadaSJuan Quintela NULL 15177c23b892Sbalrog } 1518e482dc3eSJuan Quintela }; 15197c23b892Sbalrog 15208597f2e1SGabriel L. Somlo /* 15218597f2e1SGabriel L. Somlo * EEPROM contents documented in Tables 5-2 and 5-3, pp. 98-102. 15228597f2e1SGabriel L. Somlo * Note: A valid DevId will be inserted during pci_e1000_init(). 15238597f2e1SGabriel L. Somlo */ 152488b4e9dbSblueswir1 static const uint16_t e1000_eeprom_template[64] = { 15257c23b892Sbalrog 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0x0000, 15268597f2e1SGabriel L. Somlo 0x3000, 0x1000, 0x6403, 0 /*DevId*/, 0x8086, 0 /*DevId*/, 0x8086, 0x3040, 15277c23b892Sbalrog 0x0008, 0x2000, 0x7e14, 0x0048, 0x1000, 0x00d8, 0x0000, 0x2700, 15287c23b892Sbalrog 0x6cc9, 0x3150, 0x0722, 0x040b, 0x0984, 0x0000, 0xc000, 0x0706, 15297c23b892Sbalrog 0x1008, 0x0000, 0x0f04, 0x7fff, 0x4d01, 0xffff, 0xffff, 0xffff, 15307c23b892Sbalrog 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 15317c23b892Sbalrog 0x0100, 0x4000, 0x121c, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 15327c23b892Sbalrog 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x0000, 15337c23b892Sbalrog }; 15347c23b892Sbalrog 15357c23b892Sbalrog /* PCI interface */ 15367c23b892Sbalrog 15377c23b892Sbalrog static void 1538ad00a9b9SAvi Kivity e1000_mmio_setup(E1000State *d) 15397c23b892Sbalrog { 1540f65ed4c1Saliguori int i; 1541f65ed4c1Saliguori const uint32_t excluded_regs[] = { 1542f65ed4c1Saliguori E1000_MDIC, E1000_ICR, E1000_ICS, E1000_IMS, 1543f65ed4c1Saliguori E1000_IMC, E1000_TCTL, E1000_TDT, PNPMMIO_SIZE 1544f65ed4c1Saliguori }; 1545f65ed4c1Saliguori 1546eedfac6fSPaolo Bonzini memory_region_init_io(&d->mmio, OBJECT(d), &e1000_mmio_ops, d, 1547eedfac6fSPaolo Bonzini "e1000-mmio", PNPMMIO_SIZE); 1548ad00a9b9SAvi Kivity memory_region_add_coalescing(&d->mmio, 0, excluded_regs[0]); 1549f65ed4c1Saliguori for (i = 0; excluded_regs[i] != PNPMMIO_SIZE; i++) 1550ad00a9b9SAvi Kivity memory_region_add_coalescing(&d->mmio, excluded_regs[i] + 4, 1551ad00a9b9SAvi Kivity excluded_regs[i+1] - excluded_regs[i] - 4); 1552eedfac6fSPaolo Bonzini memory_region_init_io(&d->io, OBJECT(d), &e1000_io_ops, d, "e1000-io", IOPORT_SIZE); 15537c23b892Sbalrog } 15547c23b892Sbalrog 1555b946a153Saliguori static void 15564b09be85Saliguori pci_e1000_uninit(PCIDevice *dev) 15574b09be85Saliguori { 1558567a3c9eSPeter Crosthwaite E1000State *d = E1000(dev); 15594b09be85Saliguori 1560bc72ad67SAlex Bligh timer_del(d->autoneg_timer); 1561bc72ad67SAlex Bligh timer_free(d->autoneg_timer); 1562e9845f09SVincenzo Maffione timer_del(d->mit_timer); 1563e9845f09SVincenzo Maffione timer_free(d->mit_timer); 1564948ecf21SJason Wang qemu_del_nic(d->nic); 15654b09be85Saliguori } 15664b09be85Saliguori 1567a03e2aecSMark McLoughlin static NetClientInfo net_e1000_info = { 15682be64a68SLaszlo Ersek .type = NET_CLIENT_OPTIONS_KIND_NIC, 1569a03e2aecSMark McLoughlin .size = sizeof(NICState), 1570a03e2aecSMark McLoughlin .can_receive = e1000_can_receive, 1571a03e2aecSMark McLoughlin .receive = e1000_receive, 157297410ddeSVincenzo Maffione .receive_iov = e1000_receive_iov, 1573a03e2aecSMark McLoughlin .link_status_changed = e1000_set_link_status, 1574a03e2aecSMark McLoughlin }; 1575a03e2aecSMark McLoughlin 157620302e71SMichael S. Tsirkin static void e1000_write_config(PCIDevice *pci_dev, uint32_t address, 157720302e71SMichael S. Tsirkin uint32_t val, int len) 157820302e71SMichael S. Tsirkin { 157920302e71SMichael S. Tsirkin E1000State *s = E1000(pci_dev); 158020302e71SMichael S. Tsirkin 158120302e71SMichael S. Tsirkin pci_default_write_config(pci_dev, address, val, len); 158220302e71SMichael S. Tsirkin 158320302e71SMichael S. Tsirkin if (range_covers_byte(address, len, PCI_COMMAND) && 158420302e71SMichael S. Tsirkin (pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { 158520302e71SMichael S. Tsirkin qemu_flush_queued_packets(qemu_get_queue(s->nic)); 158620302e71SMichael S. Tsirkin } 158720302e71SMichael S. Tsirkin } 158820302e71SMichael S. Tsirkin 15899af21dbeSMarkus Armbruster static void pci_e1000_realize(PCIDevice *pci_dev, Error **errp) 15907c23b892Sbalrog { 1591567a3c9eSPeter Crosthwaite DeviceState *dev = DEVICE(pci_dev); 1592567a3c9eSPeter Crosthwaite E1000State *d = E1000(pci_dev); 15937c23b892Sbalrog uint8_t *pci_conf; 1594fbdaa002SGerd Hoffmann uint8_t *macaddr; 1595aff427a1SChris Wright 159620302e71SMichael S. Tsirkin pci_dev->config_write = e1000_write_config; 159720302e71SMichael S. Tsirkin 1598b08340d5SAndreas Färber pci_conf = pci_dev->config; 15997c23b892Sbalrog 1600a9cbacb0SMichael S. Tsirkin /* TODO: RST# value should be 0, PCI spec 6.2.4 */ 1601a9cbacb0SMichael S. Tsirkin pci_conf[PCI_CACHE_LINE_SIZE] = 0x10; 16027c23b892Sbalrog 1603817e0b6fSMichael S. Tsirkin pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */ 16047c23b892Sbalrog 1605ad00a9b9SAvi Kivity e1000_mmio_setup(d); 16067c23b892Sbalrog 1607b08340d5SAndreas Färber pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio); 16087c23b892Sbalrog 1609b08340d5SAndreas Färber pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->io); 16107c23b892Sbalrog 1611fbdaa002SGerd Hoffmann qemu_macaddr_default_if_unset(&d->conf.macaddr); 1612fbdaa002SGerd Hoffmann macaddr = d->conf.macaddr.a; 1613*093454e2SDmitry Fleytman 1614*093454e2SDmitry Fleytman e1000x_core_prepare_eeprom(d->eeprom_data, 1615*093454e2SDmitry Fleytman e1000_eeprom_template, 1616*093454e2SDmitry Fleytman sizeof(e1000_eeprom_template), 1617*093454e2SDmitry Fleytman PCI_DEVICE_GET_CLASS(pci_dev)->device_id, 1618*093454e2SDmitry Fleytman macaddr); 16197c23b892Sbalrog 1620a03e2aecSMark McLoughlin d->nic = qemu_new_nic(&net_e1000_info, &d->conf, 1621567a3c9eSPeter Crosthwaite object_get_typename(OBJECT(d)), dev->id, d); 16227c23b892Sbalrog 1623b356f76dSJason Wang qemu_format_nic_info_str(qemu_get_queue(d->nic), macaddr); 16241ca4d09aSGleb Natapov 1625bc72ad67SAlex Bligh d->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, e1000_autoneg_timer, d); 1626e9845f09SVincenzo Maffione d->mit_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000_mit_timer, d); 16277c23b892Sbalrog } 16289d07d757SPaul Brook 1629fbdaa002SGerd Hoffmann static void qdev_e1000_reset(DeviceState *dev) 1630fbdaa002SGerd Hoffmann { 1631567a3c9eSPeter Crosthwaite E1000State *d = E1000(dev); 1632fbdaa002SGerd Hoffmann e1000_reset(d); 1633fbdaa002SGerd Hoffmann } 1634fbdaa002SGerd Hoffmann 163540021f08SAnthony Liguori static Property e1000_properties[] = { 1636fbdaa002SGerd Hoffmann DEFINE_NIC_PROPERTIES(E1000State, conf), 16372af234e6SMichael S. Tsirkin DEFINE_PROP_BIT("autonegotiation", E1000State, 16382af234e6SMichael S. Tsirkin compat_flags, E1000_FLAG_AUTONEG_BIT, true), 1639e9845f09SVincenzo Maffione DEFINE_PROP_BIT("mitigation", E1000State, 1640e9845f09SVincenzo Maffione compat_flags, E1000_FLAG_MIT_BIT, true), 1641ba63ec85SLeonid Bloch DEFINE_PROP_BIT("extra_mac_registers", E1000State, 1642ba63ec85SLeonid Bloch compat_flags, E1000_FLAG_MAC_BIT, true), 1643fbdaa002SGerd Hoffmann DEFINE_PROP_END_OF_LIST(), 164440021f08SAnthony Liguori }; 164540021f08SAnthony Liguori 16468597f2e1SGabriel L. Somlo typedef struct E1000Info { 16478597f2e1SGabriel L. Somlo const char *name; 16488597f2e1SGabriel L. Somlo uint16_t device_id; 16498597f2e1SGabriel L. Somlo uint8_t revision; 16508597f2e1SGabriel L. Somlo uint16_t phy_id2; 16518597f2e1SGabriel L. Somlo } E1000Info; 16528597f2e1SGabriel L. Somlo 165340021f08SAnthony Liguori static void e1000_class_init(ObjectClass *klass, void *data) 165440021f08SAnthony Liguori { 165539bffca2SAnthony Liguori DeviceClass *dc = DEVICE_CLASS(klass); 165640021f08SAnthony Liguori PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 16578597f2e1SGabriel L. Somlo E1000BaseClass *e = E1000_DEVICE_CLASS(klass); 16588597f2e1SGabriel L. Somlo const E1000Info *info = data; 165940021f08SAnthony Liguori 16609af21dbeSMarkus Armbruster k->realize = pci_e1000_realize; 166140021f08SAnthony Liguori k->exit = pci_e1000_uninit; 1662c45e5b5bSGerd Hoffmann k->romfile = "efi-e1000.rom"; 166340021f08SAnthony Liguori k->vendor_id = PCI_VENDOR_ID_INTEL; 16648597f2e1SGabriel L. Somlo k->device_id = info->device_id; 16658597f2e1SGabriel L. Somlo k->revision = info->revision; 16668597f2e1SGabriel L. Somlo e->phy_id2 = info->phy_id2; 166740021f08SAnthony Liguori k->class_id = PCI_CLASS_NETWORK_ETHERNET; 1668125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 166939bffca2SAnthony Liguori dc->desc = "Intel Gigabit Ethernet"; 167039bffca2SAnthony Liguori dc->reset = qdev_e1000_reset; 167139bffca2SAnthony Liguori dc->vmsd = &vmstate_e1000; 167239bffca2SAnthony Liguori dc->props = e1000_properties; 1673fbdaa002SGerd Hoffmann } 167440021f08SAnthony Liguori 16755df3bf62SGonglei static void e1000_instance_init(Object *obj) 16765df3bf62SGonglei { 16775df3bf62SGonglei E1000State *n = E1000(obj); 16785df3bf62SGonglei device_add_bootindex_property(obj, &n->conf.bootindex, 16795df3bf62SGonglei "bootindex", "/ethernet-phy@0", 16805df3bf62SGonglei DEVICE(n), NULL); 16815df3bf62SGonglei } 16825df3bf62SGonglei 16838597f2e1SGabriel L. Somlo static const TypeInfo e1000_base_info = { 16848597f2e1SGabriel L. Somlo .name = TYPE_E1000_BASE, 168539bffca2SAnthony Liguori .parent = TYPE_PCI_DEVICE, 168639bffca2SAnthony Liguori .instance_size = sizeof(E1000State), 16875df3bf62SGonglei .instance_init = e1000_instance_init, 16888597f2e1SGabriel L. Somlo .class_size = sizeof(E1000BaseClass), 16898597f2e1SGabriel L. Somlo .abstract = true, 16908597f2e1SGabriel L. Somlo }; 16918597f2e1SGabriel L. Somlo 16928597f2e1SGabriel L. Somlo static const E1000Info e1000_devices[] = { 16938597f2e1SGabriel L. Somlo { 169483044020SJason Wang .name = "e1000", 16958597f2e1SGabriel L. Somlo .device_id = E1000_DEV_ID_82540EM, 16968597f2e1SGabriel L. Somlo .revision = 0x03, 16978597f2e1SGabriel L. Somlo .phy_id2 = E1000_PHY_ID2_8254xx_DEFAULT, 16988597f2e1SGabriel L. Somlo }, 16998597f2e1SGabriel L. Somlo { 17008597f2e1SGabriel L. Somlo .name = "e1000-82544gc", 17018597f2e1SGabriel L. Somlo .device_id = E1000_DEV_ID_82544GC_COPPER, 17028597f2e1SGabriel L. Somlo .revision = 0x03, 17038597f2e1SGabriel L. Somlo .phy_id2 = E1000_PHY_ID2_82544x, 17048597f2e1SGabriel L. Somlo }, 17058597f2e1SGabriel L. Somlo { 17068597f2e1SGabriel L. Somlo .name = "e1000-82545em", 17078597f2e1SGabriel L. Somlo .device_id = E1000_DEV_ID_82545EM_COPPER, 17088597f2e1SGabriel L. Somlo .revision = 0x03, 17098597f2e1SGabriel L. Somlo .phy_id2 = E1000_PHY_ID2_8254xx_DEFAULT, 17108597f2e1SGabriel L. Somlo }, 17118597f2e1SGabriel L. Somlo }; 17128597f2e1SGabriel L. Somlo 171383f7d43aSAndreas Färber static void e1000_register_types(void) 17149d07d757SPaul Brook { 17158597f2e1SGabriel L. Somlo int i; 17168597f2e1SGabriel L. Somlo 17178597f2e1SGabriel L. Somlo type_register_static(&e1000_base_info); 17188597f2e1SGabriel L. Somlo for (i = 0; i < ARRAY_SIZE(e1000_devices); i++) { 17198597f2e1SGabriel L. Somlo const E1000Info *info = &e1000_devices[i]; 17208597f2e1SGabriel L. Somlo TypeInfo type_info = {}; 17218597f2e1SGabriel L. Somlo 17228597f2e1SGabriel L. Somlo type_info.name = info->name; 17238597f2e1SGabriel L. Somlo type_info.parent = TYPE_E1000_BASE; 17248597f2e1SGabriel L. Somlo type_info.class_data = (void *)info; 17258597f2e1SGabriel L. Somlo type_info.class_init = e1000_class_init; 17265df3bf62SGonglei type_info.instance_init = e1000_instance_init; 17278597f2e1SGabriel L. Somlo 17288597f2e1SGabriel L. Somlo type_register(&type_info); 17298597f2e1SGabriel L. Somlo } 17309d07d757SPaul Brook } 17319d07d757SPaul Brook 173283f7d43aSAndreas Färber type_init(e1000_register_types) 1733