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 39093454e2SDmitry 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; 101093454e2SDmitry 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 { 150093454e2SDmitry Fleytman e1000x_update_regs_on_link_up(s->mac_reg, s->phy_reg); 151093454e2SDmitry Fleytman 152093454e2SDmitry Fleytman /* E1000_STATUS_LU is tested by e1000_can_receive() */ 153093454e2SDmitry Fleytman qemu_flush_queued_packets(qemu_get_queue(s->nic)); 154093454e2SDmitry Fleytman } 155093454e2SDmitry Fleytman 156093454e2SDmitry Fleytman static void 157093454e2SDmitry Fleytman e1000_autoneg_done(E1000State *s) 158093454e2SDmitry Fleytman { 159093454e2SDmitry 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)) { 185093454e2SDmitry 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 s->mit_timer_on = 1; 315e9845f09SVincenzo Maffione timer_mod(s->mit_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 316e9845f09SVincenzo Maffione mit_delay * 256); 317e9845f09SVincenzo Maffione s->mit_ide = 0; 318e9845f09SVincenzo Maffione } 319e9845f09SVincenzo Maffione } 320e9845f09SVincenzo Maffione 321e9845f09SVincenzo Maffione s->mit_irq_level = (pending_ints != 0); 3229e64f8a3SMarcel Apfelbaum pci_set_irq(d, s->mit_irq_level); 323e9845f09SVincenzo Maffione } 324e9845f09SVincenzo Maffione 325e9845f09SVincenzo Maffione static void 326e9845f09SVincenzo Maffione e1000_mit_timer(void *opaque) 327e9845f09SVincenzo Maffione { 328e9845f09SVincenzo Maffione E1000State *s = opaque; 329e9845f09SVincenzo Maffione 330e9845f09SVincenzo Maffione s->mit_timer_on = 0; 331e9845f09SVincenzo Maffione /* Call set_interrupt_cause to update the irq level (if necessary). */ 332e9845f09SVincenzo Maffione set_interrupt_cause(s, 0, s->mac_reg[ICR]); 3337c23b892Sbalrog } 3347c23b892Sbalrog 3357c23b892Sbalrog static void 3367c23b892Sbalrog set_ics(E1000State *s, int index, uint32_t val) 3377c23b892Sbalrog { 3387c23b892Sbalrog DBGOUT(INTERRUPT, "set_ics %x, ICR %x, IMR %x\n", val, s->mac_reg[ICR], 3397c23b892Sbalrog s->mac_reg[IMS]); 3407c23b892Sbalrog set_interrupt_cause(s, 0, val | s->mac_reg[ICR]); 3417c23b892Sbalrog } 3427c23b892Sbalrog 343d52aec95SGabriel L. Somlo static void 344d52aec95SGabriel L. Somlo e1000_autoneg_timer(void *opaque) 345d52aec95SGabriel L. Somlo { 346d52aec95SGabriel L. Somlo E1000State *s = opaque; 347d52aec95SGabriel L. Somlo if (!qemu_get_queue(s->nic)->link_down) { 348093454e2SDmitry Fleytman e1000_autoneg_done(s); 349d52aec95SGabriel L. Somlo set_ics(s, 0, E1000_ICS_LSC); /* signal link status change to guest */ 350d52aec95SGabriel L. Somlo } 351d52aec95SGabriel L. Somlo } 352d52aec95SGabriel L. Somlo 353814cd3acSMichael S. Tsirkin static void e1000_reset(void *opaque) 354814cd3acSMichael S. Tsirkin { 355814cd3acSMichael S. Tsirkin E1000State *d = opaque; 3568597f2e1SGabriel L. Somlo E1000BaseClass *edc = E1000_DEVICE_GET_CLASS(d); 357372254c6SGabriel L. Somlo uint8_t *macaddr = d->conf.macaddr.a; 358814cd3acSMichael S. Tsirkin 359bc72ad67SAlex Bligh timer_del(d->autoneg_timer); 360e9845f09SVincenzo Maffione timer_del(d->mit_timer); 361e9845f09SVincenzo Maffione d->mit_timer_on = 0; 362e9845f09SVincenzo Maffione d->mit_irq_level = 0; 363e9845f09SVincenzo Maffione d->mit_ide = 0; 364814cd3acSMichael S. Tsirkin memset(d->phy_reg, 0, sizeof d->phy_reg); 365814cd3acSMichael S. Tsirkin memmove(d->phy_reg, phy_reg_init, sizeof phy_reg_init); 3668597f2e1SGabriel L. Somlo d->phy_reg[PHY_ID2] = edc->phy_id2; 367814cd3acSMichael S. Tsirkin memset(d->mac_reg, 0, sizeof d->mac_reg); 368814cd3acSMichael S. Tsirkin memmove(d->mac_reg, mac_reg_init, sizeof mac_reg_init); 369814cd3acSMichael S. Tsirkin d->rxbuf_min_shift = 1; 370814cd3acSMichael S. Tsirkin memset(&d->tx, 0, sizeof d->tx); 371814cd3acSMichael S. Tsirkin 372b356f76dSJason Wang if (qemu_get_queue(d->nic)->link_down) { 373093454e2SDmitry Fleytman e1000x_update_regs_on_link_down(d->mac_reg, d->phy_reg); 374814cd3acSMichael S. Tsirkin } 375372254c6SGabriel L. Somlo 376093454e2SDmitry Fleytman e1000x_reset_mac_addr(d->nic, d->mac_reg, macaddr); 377814cd3acSMichael S. Tsirkin } 378814cd3acSMichael S. Tsirkin 3797c23b892Sbalrog static void 380cab3c825SKevin Wolf set_ctrl(E1000State *s, int index, uint32_t val) 381cab3c825SKevin Wolf { 382cab3c825SKevin Wolf /* RST is self clearing */ 383cab3c825SKevin Wolf s->mac_reg[CTRL] = val & ~E1000_CTRL_RST; 384cab3c825SKevin Wolf } 385cab3c825SKevin Wolf 386cab3c825SKevin Wolf static void 3877c23b892Sbalrog set_rx_control(E1000State *s, int index, uint32_t val) 3887c23b892Sbalrog { 3897c23b892Sbalrog s->mac_reg[RCTL] = val; 390093454e2SDmitry Fleytman s->rxbuf_size = e1000x_rxbufsize(val); 3917c23b892Sbalrog s->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1; 3927c23b892Sbalrog DBGOUT(RX, "RCTL: %d, mac_reg[RCTL] = 0x%x\n", s->mac_reg[RDT], 3937c23b892Sbalrog s->mac_reg[RCTL]); 394b356f76dSJason Wang qemu_flush_queued_packets(qemu_get_queue(s->nic)); 3957c23b892Sbalrog } 3967c23b892Sbalrog 3977c23b892Sbalrog static void 3987c23b892Sbalrog set_mdic(E1000State *s, int index, uint32_t val) 3997c23b892Sbalrog { 4007c23b892Sbalrog uint32_t data = val & E1000_MDIC_DATA_MASK; 4017c23b892Sbalrog uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT); 4027c23b892Sbalrog 4037c23b892Sbalrog if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) // phy # 4047c23b892Sbalrog val = s->mac_reg[MDIC] | E1000_MDIC_ERROR; 4057c23b892Sbalrog else if (val & E1000_MDIC_OP_READ) { 4067c23b892Sbalrog DBGOUT(MDIC, "MDIC read reg 0x%x\n", addr); 4077c23b892Sbalrog if (!(phy_regcap[addr] & PHY_R)) { 4087c23b892Sbalrog DBGOUT(MDIC, "MDIC read reg %x unhandled\n", addr); 4097c23b892Sbalrog val |= E1000_MDIC_ERROR; 4107c23b892Sbalrog } else 4117c23b892Sbalrog val = (val ^ data) | s->phy_reg[addr]; 4127c23b892Sbalrog } else if (val & E1000_MDIC_OP_WRITE) { 4137c23b892Sbalrog DBGOUT(MDIC, "MDIC write reg 0x%x, value 0x%x\n", addr, data); 4147c23b892Sbalrog if (!(phy_regcap[addr] & PHY_W)) { 4157c23b892Sbalrog DBGOUT(MDIC, "MDIC write reg %x unhandled\n", addr); 4167c23b892Sbalrog val |= E1000_MDIC_ERROR; 417b9d03e35SJason Wang } else { 418b9d03e35SJason Wang if (addr < NPHYWRITEOPS && phyreg_writeops[addr]) { 419b9d03e35SJason Wang phyreg_writeops[addr](s, index, data); 4201195fed9SGabriel L. Somlo } else { 4217c23b892Sbalrog s->phy_reg[addr] = data; 4227c23b892Sbalrog } 423b9d03e35SJason Wang } 4241195fed9SGabriel L. Somlo } 4257c23b892Sbalrog s->mac_reg[MDIC] = val | E1000_MDIC_READY; 42617fbbb0bSJason Wang 42717fbbb0bSJason Wang if (val & E1000_MDIC_INT_EN) { 4287c23b892Sbalrog set_ics(s, 0, E1000_ICR_MDAC); 4297c23b892Sbalrog } 43017fbbb0bSJason Wang } 4317c23b892Sbalrog 4327c23b892Sbalrog static uint32_t 4337c23b892Sbalrog get_eecd(E1000State *s, int index) 4347c23b892Sbalrog { 4357c23b892Sbalrog uint32_t ret = E1000_EECD_PRES|E1000_EECD_GNT | s->eecd_state.old_eecd; 4367c23b892Sbalrog 4377c23b892Sbalrog DBGOUT(EEPROM, "reading eeprom bit %d (reading %d)\n", 4387c23b892Sbalrog s->eecd_state.bitnum_out, s->eecd_state.reading); 4397c23b892Sbalrog if (!s->eecd_state.reading || 4407c23b892Sbalrog ((s->eeprom_data[(s->eecd_state.bitnum_out >> 4) & 0x3f] >> 4417c23b892Sbalrog ((s->eecd_state.bitnum_out & 0xf) ^ 0xf))) & 1) 4427c23b892Sbalrog ret |= E1000_EECD_DO; 4437c23b892Sbalrog return ret; 4447c23b892Sbalrog } 4457c23b892Sbalrog 4467c23b892Sbalrog static void 4477c23b892Sbalrog set_eecd(E1000State *s, int index, uint32_t val) 4487c23b892Sbalrog { 4497c23b892Sbalrog uint32_t oldval = s->eecd_state.old_eecd; 4507c23b892Sbalrog 4517c23b892Sbalrog s->eecd_state.old_eecd = val & (E1000_EECD_SK | E1000_EECD_CS | 4527c23b892Sbalrog E1000_EECD_DI|E1000_EECD_FWE_MASK|E1000_EECD_REQ); 45320f3e863SLeonid Bloch if (!(E1000_EECD_CS & val)) { /* CS inactive; nothing to do */ 4549651ac55SIzumi Tsutsui return; 45520f3e863SLeonid Bloch } 45620f3e863SLeonid Bloch if (E1000_EECD_CS & (val ^ oldval)) { /* CS rise edge; reset state */ 4579651ac55SIzumi Tsutsui s->eecd_state.val_in = 0; 4589651ac55SIzumi Tsutsui s->eecd_state.bitnum_in = 0; 4599651ac55SIzumi Tsutsui s->eecd_state.bitnum_out = 0; 4609651ac55SIzumi Tsutsui s->eecd_state.reading = 0; 4619651ac55SIzumi Tsutsui } 46220f3e863SLeonid Bloch if (!(E1000_EECD_SK & (val ^ oldval))) { /* no clock edge */ 4637c23b892Sbalrog return; 46420f3e863SLeonid Bloch } 46520f3e863SLeonid Bloch if (!(E1000_EECD_SK & val)) { /* falling edge */ 4667c23b892Sbalrog s->eecd_state.bitnum_out++; 4677c23b892Sbalrog return; 4687c23b892Sbalrog } 4697c23b892Sbalrog s->eecd_state.val_in <<= 1; 4707c23b892Sbalrog if (val & E1000_EECD_DI) 4717c23b892Sbalrog s->eecd_state.val_in |= 1; 4727c23b892Sbalrog if (++s->eecd_state.bitnum_in == 9 && !s->eecd_state.reading) { 4737c23b892Sbalrog s->eecd_state.bitnum_out = ((s->eecd_state.val_in & 0x3f)<<4)-1; 4747c23b892Sbalrog s->eecd_state.reading = (((s->eecd_state.val_in >> 6) & 7) == 4757c23b892Sbalrog EEPROM_READ_OPCODE_MICROWIRE); 4767c23b892Sbalrog } 4777c23b892Sbalrog DBGOUT(EEPROM, "eeprom bitnum in %d out %d, reading %d\n", 4787c23b892Sbalrog s->eecd_state.bitnum_in, s->eecd_state.bitnum_out, 4797c23b892Sbalrog s->eecd_state.reading); 4807c23b892Sbalrog } 4817c23b892Sbalrog 4827c23b892Sbalrog static uint32_t 4837c23b892Sbalrog flash_eerd_read(E1000State *s, int x) 4847c23b892Sbalrog { 4857c23b892Sbalrog unsigned int index, r = s->mac_reg[EERD] & ~E1000_EEPROM_RW_REG_START; 4867c23b892Sbalrog 487b1332393SBill Paul if ((s->mac_reg[EERD] & E1000_EEPROM_RW_REG_START) == 0) 488b1332393SBill Paul return (s->mac_reg[EERD]); 489b1332393SBill Paul 4907c23b892Sbalrog if ((index = r >> E1000_EEPROM_RW_ADDR_SHIFT) > EEPROM_CHECKSUM_REG) 491b1332393SBill Paul return (E1000_EEPROM_RW_REG_DONE | r); 492b1332393SBill Paul 493b1332393SBill Paul return ((s->eeprom_data[index] << E1000_EEPROM_RW_REG_DATA) | 494b1332393SBill Paul E1000_EEPROM_RW_REG_DONE | r); 4957c23b892Sbalrog } 4967c23b892Sbalrog 4977c23b892Sbalrog static void 4987c23b892Sbalrog putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t css, uint32_t cse) 4997c23b892Sbalrog { 500c6a6a5e3Saliguori uint32_t sum; 501c6a6a5e3Saliguori 5027c23b892Sbalrog if (cse && cse < n) 5037c23b892Sbalrog n = cse + 1; 504c6a6a5e3Saliguori if (sloc < n-1) { 505c6a6a5e3Saliguori sum = net_checksum_add(n-css, data+css); 506d8ee2591SPeter Maydell stw_be_p(data + sloc, net_checksum_finish(sum)); 507c6a6a5e3Saliguori } 5087c23b892Sbalrog } 5097c23b892Sbalrog 5101f67f92cSLeonid Bloch static inline void 5113b274301SLeonid Bloch inc_tx_bcast_or_mcast_count(E1000State *s, const unsigned char *arr) 5123b274301SLeonid Bloch { 5133b274301SLeonid Bloch if (!memcmp(arr, bcast, sizeof bcast)) { 514093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, BPTC); 5153b274301SLeonid Bloch } else if (arr[0] & 1) { 516093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, MPTC); 5173b274301SLeonid Bloch } 5183b274301SLeonid Bloch } 5193b274301SLeonid Bloch 52045e93764SLeonid Bloch static void 52193e37d76SJason Wang e1000_send_packet(E1000State *s, const uint8_t *buf, int size) 52293e37d76SJason Wang { 5233b274301SLeonid Bloch static const int PTCregs[6] = { PTC64, PTC127, PTC255, PTC511, 5243b274301SLeonid Bloch PTC1023, PTC1522 }; 5253b274301SLeonid Bloch 526b356f76dSJason Wang NetClientState *nc = qemu_get_queue(s->nic); 52793e37d76SJason Wang if (s->phy_reg[PHY_CTRL] & MII_CR_LOOPBACK) { 528b356f76dSJason Wang nc->info->receive(nc, buf, size); 52993e37d76SJason Wang } else { 530b356f76dSJason Wang qemu_send_packet(nc, buf, size); 53193e37d76SJason Wang } 5323b274301SLeonid Bloch inc_tx_bcast_or_mcast_count(s, buf); 533093454e2SDmitry Fleytman e1000x_increase_size_stats(s->mac_reg, PTCregs, size); 53493e37d76SJason Wang } 53593e37d76SJason Wang 53693e37d76SJason Wang static void 5377c23b892Sbalrog xmit_seg(E1000State *s) 5387c23b892Sbalrog { 53914e60aaeSPeter Maydell uint16_t len; 54045e93764SLeonid Bloch unsigned int frames = s->tx.tso_frames, css, sofar; 5417c23b892Sbalrog struct e1000_tx *tp = &s->tx; 5427c23b892Sbalrog 543093454e2SDmitry Fleytman if (tp->props.tse && tp->props.cptse) { 544093454e2SDmitry Fleytman css = tp->props.ipcss; 5457c23b892Sbalrog DBGOUT(TXSUM, "frames %d size %d ipcss %d\n", 5467c23b892Sbalrog frames, tp->size, css); 547093454e2SDmitry Fleytman if (tp->props.ip) { /* IPv4 */ 548d8ee2591SPeter Maydell stw_be_p(tp->data+css+2, tp->size - css); 549d8ee2591SPeter Maydell stw_be_p(tp->data+css+4, 55014e60aaeSPeter Maydell lduw_be_p(tp->data + css + 4) + frames); 55120f3e863SLeonid Bloch } else { /* IPv6 */ 552d8ee2591SPeter Maydell stw_be_p(tp->data+css+4, tp->size - css); 55320f3e863SLeonid Bloch } 554093454e2SDmitry Fleytman css = tp->props.tucss; 5557c23b892Sbalrog len = tp->size - css; 556093454e2SDmitry Fleytman DBGOUT(TXSUM, "tcp %d tucss %d len %d\n", tp->props.tcp, css, len); 557093454e2SDmitry Fleytman if (tp->props.tcp) { 558093454e2SDmitry Fleytman sofar = frames * tp->props.mss; 5596bd194abSPeter Maydell stl_be_p(tp->data+css+4, ldl_be_p(tp->data+css+4)+sofar); /* seq */ 560093454e2SDmitry Fleytman if (tp->props.paylen - sofar > tp->props.mss) { 56120f3e863SLeonid Bloch tp->data[css + 13] &= ~9; /* PSH, FIN */ 5623b274301SLeonid Bloch } else if (frames) { 563093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, TSCTC); 5643b274301SLeonid Bloch } 56520f3e863SLeonid Bloch } else /* UDP */ 566d8ee2591SPeter Maydell stw_be_p(tp->data+css+4, len); 567093454e2SDmitry Fleytman if (tp->props.sum_needed & E1000_TXD_POPTS_TXSM) { 568e685b4ebSAlex Williamson unsigned int phsum; 5697c23b892Sbalrog // add pseudo-header length before checksum calculation 57014e60aaeSPeter Maydell void *sp = tp->data + tp->props.tucso; 57114e60aaeSPeter Maydell 57214e60aaeSPeter Maydell phsum = lduw_be_p(sp) + len; 573e685b4ebSAlex Williamson phsum = (phsum >> 16) + (phsum & 0xffff); 574d8ee2591SPeter Maydell stw_be_p(sp, phsum); 5757c23b892Sbalrog } 5767c23b892Sbalrog tp->tso_frames++; 5777c23b892Sbalrog } 5787c23b892Sbalrog 579093454e2SDmitry Fleytman if (tp->props.sum_needed & E1000_TXD_POPTS_TXSM) { 580093454e2SDmitry Fleytman putsum(tp->data, tp->size, tp->props.tucso, 581093454e2SDmitry Fleytman tp->props.tucss, tp->props.tucse); 582093454e2SDmitry Fleytman } 583093454e2SDmitry Fleytman if (tp->props.sum_needed & E1000_TXD_POPTS_IXSM) { 584093454e2SDmitry Fleytman putsum(tp->data, tp->size, tp->props.ipcso, 585093454e2SDmitry Fleytman tp->props.ipcss, tp->props.ipcse); 586093454e2SDmitry Fleytman } 5878f2e8d1fSaliguori if (tp->vlan_needed) { 588b10fec9bSStefan Weil memmove(tp->vlan, tp->data, 4); 589b10fec9bSStefan Weil memmove(tp->data, tp->data + 4, 8); 5908f2e8d1fSaliguori memcpy(tp->data + 8, tp->vlan_header, 4); 59193e37d76SJason Wang e1000_send_packet(s, tp->vlan, tp->size + 4); 59220f3e863SLeonid Bloch } else { 59393e37d76SJason Wang e1000_send_packet(s, tp->data, tp->size); 59420f3e863SLeonid Bloch } 59520f3e863SLeonid Bloch 596093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, TPT); 597093454e2SDmitry Fleytman e1000x_grow_8reg_if_not_full(s->mac_reg, TOTL, s->tx.size); 5981f67f92cSLeonid Bloch s->mac_reg[GPTC] = s->mac_reg[TPT]; 5993b274301SLeonid Bloch s->mac_reg[GOTCL] = s->mac_reg[TOTL]; 6003b274301SLeonid Bloch s->mac_reg[GOTCH] = s->mac_reg[TOTH]; 6017c23b892Sbalrog } 6027c23b892Sbalrog 6037c23b892Sbalrog static void 6047c23b892Sbalrog process_tx_desc(E1000State *s, struct e1000_tx_desc *dp) 6057c23b892Sbalrog { 606b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 6077c23b892Sbalrog uint32_t txd_lower = le32_to_cpu(dp->lower.data); 6087c23b892Sbalrog uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D); 609093454e2SDmitry Fleytman unsigned int split_size = txd_lower & 0xffff, bytes, sz; 610a0ae17a6SAndrew Jones unsigned int msh = 0xfffff; 6117c23b892Sbalrog uint64_t addr; 6127c23b892Sbalrog struct e1000_context_desc *xp = (struct e1000_context_desc *)dp; 6137c23b892Sbalrog struct e1000_tx *tp = &s->tx; 6147c23b892Sbalrog 615e9845f09SVincenzo Maffione s->mit_ide |= (txd_lower & E1000_TXD_CMD_IDE); 61620f3e863SLeonid Bloch if (dtype == E1000_TXD_CMD_DEXT) { /* context descriptor */ 617093454e2SDmitry Fleytman e1000x_read_tx_ctx_descr(xp, &tp->props); 6187c23b892Sbalrog tp->tso_frames = 0; 619093454e2SDmitry Fleytman if (tp->props.tucso == 0) { /* this is probably wrong */ 6207c23b892Sbalrog DBGOUT(TXSUM, "TCP/UDP: cso 0!\n"); 621093454e2SDmitry Fleytman tp->props.tucso = tp->props.tucss + (tp->props.tcp ? 16 : 6); 6227c23b892Sbalrog } 6237c23b892Sbalrog return; 6241b0009dbSbalrog } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) { 6251b0009dbSbalrog // data descriptor 626735e77ecSStefan Hajnoczi if (tp->size == 0) { 627093454e2SDmitry Fleytman tp->props.sum_needed = le32_to_cpu(dp->upper.data) >> 8; 628735e77ecSStefan Hajnoczi } 629093454e2SDmitry Fleytman tp->props.cptse = (txd_lower & E1000_TXD_CMD_TSE) ? 1 : 0; 63043ad7e3eSJes Sorensen } else { 6311b0009dbSbalrog // legacy descriptor 632093454e2SDmitry Fleytman tp->props.cptse = 0; 63343ad7e3eSJes Sorensen } 6347c23b892Sbalrog 635093454e2SDmitry Fleytman if (e1000x_vlan_enabled(s->mac_reg) && 636093454e2SDmitry Fleytman e1000x_is_vlan_txd(txd_lower) && 637093454e2SDmitry Fleytman (tp->props.cptse || txd_lower & E1000_TXD_CMD_EOP)) { 6388f2e8d1fSaliguori tp->vlan_needed = 1; 639d8ee2591SPeter Maydell stw_be_p(tp->vlan_header, 6404e60a250SShannon Zhao le16_to_cpu(s->mac_reg[VET])); 641d8ee2591SPeter Maydell stw_be_p(tp->vlan_header + 2, 6428f2e8d1fSaliguori le16_to_cpu(dp->upper.fields.special)); 6438f2e8d1fSaliguori } 6448f2e8d1fSaliguori 6457c23b892Sbalrog addr = le64_to_cpu(dp->buffer_addr); 646093454e2SDmitry Fleytman if (tp->props.tse && tp->props.cptse) { 647093454e2SDmitry Fleytman msh = tp->props.hdr_len + tp->props.mss; 6487c23b892Sbalrog do { 6497c23b892Sbalrog bytes = split_size; 6507c23b892Sbalrog if (tp->size + bytes > msh) 6517c23b892Sbalrog bytes = msh - tp->size; 65265f82df0SAnthony Liguori 65365f82df0SAnthony Liguori bytes = MIN(sizeof(tp->data) - tp->size, bytes); 654b08340d5SAndreas Färber pci_dma_read(d, addr, tp->data + tp->size, bytes); 655a0ae17a6SAndrew Jones sz = tp->size + bytes; 656093454e2SDmitry Fleytman if (sz >= tp->props.hdr_len && tp->size < tp->props.hdr_len) { 657093454e2SDmitry Fleytman memmove(tp->header, tp->data, tp->props.hdr_len); 658a0ae17a6SAndrew Jones } 6597c23b892Sbalrog tp->size = sz; 6607c23b892Sbalrog addr += bytes; 6617c23b892Sbalrog if (sz == msh) { 6627c23b892Sbalrog xmit_seg(s); 663093454e2SDmitry Fleytman memmove(tp->data, tp->header, tp->props.hdr_len); 664093454e2SDmitry Fleytman tp->size = tp->props.hdr_len; 6657c23b892Sbalrog } 666b947ac2bSP J P split_size -= bytes; 667b947ac2bSP J P } while (bytes && split_size); 668093454e2SDmitry Fleytman } else if (!tp->props.tse && tp->props.cptse) { 6691b0009dbSbalrog // context descriptor TSE is not set, while data descriptor TSE is set 670362f5fb5SStefan Weil DBGOUT(TXERR, "TCP segmentation error\n"); 6711b0009dbSbalrog } else { 67265f82df0SAnthony Liguori split_size = MIN(sizeof(tp->data) - tp->size, split_size); 673b08340d5SAndreas Färber pci_dma_read(d, addr, tp->data + tp->size, split_size); 6741b0009dbSbalrog tp->size += split_size; 6751b0009dbSbalrog } 6767c23b892Sbalrog 6777c23b892Sbalrog if (!(txd_lower & E1000_TXD_CMD_EOP)) 6787c23b892Sbalrog return; 679093454e2SDmitry Fleytman if (!(tp->props.tse && tp->props.cptse && tp->size < tp->props.hdr_len)) { 6807c23b892Sbalrog xmit_seg(s); 681a0ae17a6SAndrew Jones } 6827c23b892Sbalrog tp->tso_frames = 0; 683093454e2SDmitry Fleytman tp->props.sum_needed = 0; 6848f2e8d1fSaliguori tp->vlan_needed = 0; 6857c23b892Sbalrog tp->size = 0; 686093454e2SDmitry Fleytman tp->props.cptse = 0; 6877c23b892Sbalrog } 6887c23b892Sbalrog 6897c23b892Sbalrog static uint32_t 69062ecbd35SEduard - Gabriel Munteanu txdesc_writeback(E1000State *s, dma_addr_t base, struct e1000_tx_desc *dp) 6917c23b892Sbalrog { 692b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 6937c23b892Sbalrog uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data); 6947c23b892Sbalrog 6957c23b892Sbalrog if (!(txd_lower & (E1000_TXD_CMD_RS|E1000_TXD_CMD_RPS))) 6967c23b892Sbalrog return 0; 6977c23b892Sbalrog txd_upper = (le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD) & 6987c23b892Sbalrog ~(E1000_TXD_STAT_EC | E1000_TXD_STAT_LC | E1000_TXD_STAT_TU); 6997c23b892Sbalrog dp->upper.data = cpu_to_le32(txd_upper); 700b08340d5SAndreas Färber pci_dma_write(d, base + ((char *)&dp->upper - (char *)dp), 70100c3a05bSDavid Gibson &dp->upper, sizeof(dp->upper)); 7027c23b892Sbalrog return E1000_ICR_TXDW; 7037c23b892Sbalrog } 7047c23b892Sbalrog 705d17161f6SKevin Wolf static uint64_t tx_desc_base(E1000State *s) 706d17161f6SKevin Wolf { 707d17161f6SKevin Wolf uint64_t bah = s->mac_reg[TDBAH]; 708d17161f6SKevin Wolf uint64_t bal = s->mac_reg[TDBAL] & ~0xf; 709d17161f6SKevin Wolf 710d17161f6SKevin Wolf return (bah << 32) + bal; 711d17161f6SKevin Wolf } 712d17161f6SKevin Wolf 7137c23b892Sbalrog static void 7147c23b892Sbalrog start_xmit(E1000State *s) 7157c23b892Sbalrog { 716b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 71762ecbd35SEduard - Gabriel Munteanu dma_addr_t base; 7187c23b892Sbalrog struct e1000_tx_desc desc; 7197c23b892Sbalrog uint32_t tdh_start = s->mac_reg[TDH], cause = E1000_ICS_TXQE; 7207c23b892Sbalrog 7217c23b892Sbalrog if (!(s->mac_reg[TCTL] & E1000_TCTL_EN)) { 7227c23b892Sbalrog DBGOUT(TX, "tx disabled\n"); 7237c23b892Sbalrog return; 7247c23b892Sbalrog } 7257c23b892Sbalrog 7267c23b892Sbalrog while (s->mac_reg[TDH] != s->mac_reg[TDT]) { 727d17161f6SKevin Wolf base = tx_desc_base(s) + 7287c23b892Sbalrog sizeof(struct e1000_tx_desc) * s->mac_reg[TDH]; 729b08340d5SAndreas Färber pci_dma_read(d, base, &desc, sizeof(desc)); 7307c23b892Sbalrog 7317c23b892Sbalrog DBGOUT(TX, "index %d: %p : %x %x\n", s->mac_reg[TDH], 7326106075bSths (void *)(intptr_t)desc.buffer_addr, desc.lower.data, 7337c23b892Sbalrog desc.upper.data); 7347c23b892Sbalrog 7357c23b892Sbalrog process_tx_desc(s, &desc); 73662ecbd35SEduard - Gabriel Munteanu cause |= txdesc_writeback(s, base, &desc); 7377c23b892Sbalrog 7387c23b892Sbalrog if (++s->mac_reg[TDH] * sizeof(desc) >= s->mac_reg[TDLEN]) 7397c23b892Sbalrog s->mac_reg[TDH] = 0; 7407c23b892Sbalrog /* 7417c23b892Sbalrog * the following could happen only if guest sw assigns 7427c23b892Sbalrog * bogus values to TDT/TDLEN. 7437c23b892Sbalrog * there's nothing too intelligent we could do about this. 7447c23b892Sbalrog */ 745dd793a74SLaszlo Ersek if (s->mac_reg[TDH] == tdh_start || 746dd793a74SLaszlo Ersek tdh_start >= s->mac_reg[TDLEN] / sizeof(desc)) { 7477c23b892Sbalrog DBGOUT(TXERR, "TDH wraparound @%x, TDT %x, TDLEN %x\n", 7487c23b892Sbalrog tdh_start, s->mac_reg[TDT], s->mac_reg[TDLEN]); 7497c23b892Sbalrog break; 7507c23b892Sbalrog } 7517c23b892Sbalrog } 7527c23b892Sbalrog set_ics(s, 0, cause); 7537c23b892Sbalrog } 7547c23b892Sbalrog 7557c23b892Sbalrog static int 7567c23b892Sbalrog receive_filter(E1000State *s, const uint8_t *buf, int size) 7577c23b892Sbalrog { 758093454e2SDmitry Fleytman uint32_t rctl = s->mac_reg[RCTL]; 7594aeea330SLeonid Bloch int isbcast = !memcmp(buf, bcast, sizeof bcast), ismcast = (buf[0] & 1); 7607c23b892Sbalrog 761093454e2SDmitry Fleytman if (e1000x_is_vlan_packet(buf, le16_to_cpu(s->mac_reg[VET])) && 762093454e2SDmitry Fleytman e1000x_vlan_rx_filter_enabled(s->mac_reg)) { 76314e60aaeSPeter Maydell uint16_t vid = lduw_be_p(buf + 14); 76414e60aaeSPeter Maydell uint32_t vfta = ldl_le_p((uint32_t*)(s->mac_reg + VFTA) + 7658f2e8d1fSaliguori ((vid >> 5) & 0x7f)); 7668f2e8d1fSaliguori if ((vfta & (1 << (vid & 0x1f))) == 0) 7678f2e8d1fSaliguori return 0; 7688f2e8d1fSaliguori } 7698f2e8d1fSaliguori 7704aeea330SLeonid Bloch if (!isbcast && !ismcast && (rctl & E1000_RCTL_UPE)) { /* promiscuous ucast */ 7717c23b892Sbalrog return 1; 7724aeea330SLeonid Bloch } 7737c23b892Sbalrog 7744aeea330SLeonid Bloch if (ismcast && (rctl & E1000_RCTL_MPE)) { /* promiscuous mcast */ 775093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, MPRC); 7767c23b892Sbalrog return 1; 7774aeea330SLeonid Bloch } 7787c23b892Sbalrog 7794aeea330SLeonid Bloch if (isbcast && (rctl & E1000_RCTL_BAM)) { /* broadcast enabled */ 780093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, BPRC); 7817c23b892Sbalrog return 1; 7824aeea330SLeonid Bloch } 7837c23b892Sbalrog 784093454e2SDmitry Fleytman return e1000x_rx_group_filter(s->mac_reg, buf); 7857c23b892Sbalrog } 7867c23b892Sbalrog 78799ed7e30Saliguori static void 7884e68f7a0SStefan Hajnoczi e1000_set_link_status(NetClientState *nc) 78999ed7e30Saliguori { 790cc1f0f45SJason Wang E1000State *s = qemu_get_nic_opaque(nc); 79199ed7e30Saliguori uint32_t old_status = s->mac_reg[STATUS]; 79299ed7e30Saliguori 793d4044c2aSBjørn Mork if (nc->link_down) { 794093454e2SDmitry Fleytman e1000x_update_regs_on_link_down(s->mac_reg, s->phy_reg); 795d4044c2aSBjørn Mork } else { 796d7a41552SGabriel L. Somlo if (have_autoneg(s) && 7976a2acedbSGabriel L. Somlo !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) { 798093454e2SDmitry Fleytman e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer); 7996a2acedbSGabriel L. Somlo } else { 80071aadd3cSJason Wang e1000_link_up(s); 801d4044c2aSBjørn Mork } 8026a2acedbSGabriel L. Somlo } 80399ed7e30Saliguori 80499ed7e30Saliguori if (s->mac_reg[STATUS] != old_status) 80599ed7e30Saliguori set_ics(s, 0, E1000_ICR_LSC); 80699ed7e30Saliguori } 80799ed7e30Saliguori 808322fd48aSMichael S. Tsirkin static bool e1000_has_rxbufs(E1000State *s, size_t total_size) 809322fd48aSMichael S. Tsirkin { 810322fd48aSMichael S. Tsirkin int bufs; 811322fd48aSMichael S. Tsirkin /* Fast-path short packets */ 812322fd48aSMichael S. Tsirkin if (total_size <= s->rxbuf_size) { 813e5b8b0d4SDmitry Fleytman return s->mac_reg[RDH] != s->mac_reg[RDT]; 814322fd48aSMichael S. Tsirkin } 815322fd48aSMichael S. Tsirkin if (s->mac_reg[RDH] < s->mac_reg[RDT]) { 816322fd48aSMichael S. Tsirkin bufs = s->mac_reg[RDT] - s->mac_reg[RDH]; 817e5b8b0d4SDmitry Fleytman } else if (s->mac_reg[RDH] > s->mac_reg[RDT]) { 818322fd48aSMichael S. Tsirkin bufs = s->mac_reg[RDLEN] / sizeof(struct e1000_rx_desc) + 819322fd48aSMichael S. Tsirkin s->mac_reg[RDT] - s->mac_reg[RDH]; 820322fd48aSMichael S. Tsirkin } else { 821322fd48aSMichael S. Tsirkin return false; 822322fd48aSMichael S. Tsirkin } 823322fd48aSMichael S. Tsirkin return total_size <= bufs * s->rxbuf_size; 824322fd48aSMichael S. Tsirkin } 825322fd48aSMichael S. Tsirkin 8266cdfab28SMichael S. Tsirkin static int 8274e68f7a0SStefan Hajnoczi e1000_can_receive(NetClientState *nc) 8286cdfab28SMichael S. Tsirkin { 829cc1f0f45SJason Wang E1000State *s = qemu_get_nic_opaque(nc); 8306cdfab28SMichael S. Tsirkin 831093454e2SDmitry Fleytman return e1000x_rx_ready(&s->parent_obj, s->mac_reg) && 83220302e71SMichael S. Tsirkin e1000_has_rxbufs(s, 1); 8336cdfab28SMichael S. Tsirkin } 8346cdfab28SMichael S. Tsirkin 835d17161f6SKevin Wolf static uint64_t rx_desc_base(E1000State *s) 836d17161f6SKevin Wolf { 837d17161f6SKevin Wolf uint64_t bah = s->mac_reg[RDBAH]; 838d17161f6SKevin Wolf uint64_t bal = s->mac_reg[RDBAL] & ~0xf; 839d17161f6SKevin Wolf 840d17161f6SKevin Wolf return (bah << 32) + bal; 841d17161f6SKevin Wolf } 842d17161f6SKevin Wolf 8434f1c942bSMark McLoughlin static ssize_t 84497410ddeSVincenzo Maffione e1000_receive_iov(NetClientState *nc, const struct iovec *iov, int iovcnt) 8457c23b892Sbalrog { 846cc1f0f45SJason Wang E1000State *s = qemu_get_nic_opaque(nc); 847b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 8487c23b892Sbalrog struct e1000_rx_desc desc; 84962ecbd35SEduard - Gabriel Munteanu dma_addr_t base; 8507c23b892Sbalrog unsigned int n, rdt; 8517c23b892Sbalrog uint32_t rdh_start; 8528f2e8d1fSaliguori uint16_t vlan_special = 0; 85397410ddeSVincenzo Maffione uint8_t vlan_status = 0; 85478aeb23eSStefan Hajnoczi uint8_t min_buf[MIN_BUF_SIZE]; 85597410ddeSVincenzo Maffione struct iovec min_iov; 85697410ddeSVincenzo Maffione uint8_t *filter_buf = iov->iov_base; 85797410ddeSVincenzo Maffione size_t size = iov_size(iov, iovcnt); 85897410ddeSVincenzo Maffione size_t iov_ofs = 0; 859b19487e2SMichael S. Tsirkin size_t desc_offset; 860b19487e2SMichael S. Tsirkin size_t desc_size; 861b19487e2SMichael S. Tsirkin size_t total_size; 8627c23b892Sbalrog 863093454e2SDmitry Fleytman if (!e1000x_hw_rx_enabled(s->mac_reg)) { 864ddcb73b7SMichael S. Tsirkin return -1; 865ddcb73b7SMichael S. Tsirkin } 8667c23b892Sbalrog 86778aeb23eSStefan Hajnoczi /* Pad to minimum Ethernet frame length */ 86878aeb23eSStefan Hajnoczi if (size < sizeof(min_buf)) { 86997410ddeSVincenzo Maffione iov_to_buf(iov, iovcnt, 0, min_buf, size); 87078aeb23eSStefan Hajnoczi memset(&min_buf[size], 0, sizeof(min_buf) - size); 871093454e2SDmitry Fleytman e1000x_inc_reg_if_not_full(s->mac_reg, RUC); 87297410ddeSVincenzo Maffione min_iov.iov_base = filter_buf = min_buf; 87397410ddeSVincenzo Maffione min_iov.iov_len = size = sizeof(min_buf); 87497410ddeSVincenzo Maffione iovcnt = 1; 87597410ddeSVincenzo Maffione iov = &min_iov; 87697410ddeSVincenzo Maffione } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) { 87797410ddeSVincenzo Maffione /* This is very unlikely, but may happen. */ 87897410ddeSVincenzo Maffione iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN); 87997410ddeSVincenzo Maffione filter_buf = min_buf; 88078aeb23eSStefan Hajnoczi } 88178aeb23eSStefan Hajnoczi 882b0d9ffcdSMichael Contreras /* Discard oversized packets if !LPE and !SBP. */ 883093454e2SDmitry Fleytman if (e1000x_is_oversized(s->mac_reg, size)) { 884b0d9ffcdSMichael Contreras return size; 885b0d9ffcdSMichael Contreras } 886b0d9ffcdSMichael Contreras 88797410ddeSVincenzo Maffione if (!receive_filter(s, filter_buf, size)) { 8884f1c942bSMark McLoughlin return size; 88997410ddeSVincenzo Maffione } 8907c23b892Sbalrog 891093454e2SDmitry Fleytman if (e1000x_vlan_enabled(s->mac_reg) && 892093454e2SDmitry Fleytman e1000x_is_vlan_packet(filter_buf, le16_to_cpu(s->mac_reg[VET]))) { 89314e60aaeSPeter Maydell vlan_special = cpu_to_le16(lduw_be_p(filter_buf + 14)); 89497410ddeSVincenzo Maffione iov_ofs = 4; 89597410ddeSVincenzo Maffione if (filter_buf == iov->iov_base) { 89697410ddeSVincenzo Maffione memmove(filter_buf + 4, filter_buf, 12); 89797410ddeSVincenzo Maffione } else { 89897410ddeSVincenzo Maffione iov_from_buf(iov, iovcnt, 4, filter_buf, 12); 89997410ddeSVincenzo Maffione while (iov->iov_len <= iov_ofs) { 90097410ddeSVincenzo Maffione iov_ofs -= iov->iov_len; 90197410ddeSVincenzo Maffione iov++; 90297410ddeSVincenzo Maffione } 90397410ddeSVincenzo Maffione } 9048f2e8d1fSaliguori vlan_status = E1000_RXD_STAT_VP; 9058f2e8d1fSaliguori size -= 4; 9068f2e8d1fSaliguori } 9078f2e8d1fSaliguori 9087c23b892Sbalrog rdh_start = s->mac_reg[RDH]; 909b19487e2SMichael S. Tsirkin desc_offset = 0; 910093454e2SDmitry Fleytman total_size = size + e1000x_fcs_len(s->mac_reg); 911322fd48aSMichael S. Tsirkin if (!e1000_has_rxbufs(s, total_size)) { 912322fd48aSMichael S. Tsirkin set_ics(s, 0, E1000_ICS_RXO); 913322fd48aSMichael S. Tsirkin return -1; 914322fd48aSMichael S. Tsirkin } 9157c23b892Sbalrog do { 916b19487e2SMichael S. Tsirkin desc_size = total_size - desc_offset; 917b19487e2SMichael S. Tsirkin if (desc_size > s->rxbuf_size) { 918b19487e2SMichael S. Tsirkin desc_size = s->rxbuf_size; 919b19487e2SMichael S. Tsirkin } 920d17161f6SKevin Wolf base = rx_desc_base(s) + sizeof(desc) * s->mac_reg[RDH]; 921b08340d5SAndreas Färber pci_dma_read(d, base, &desc, sizeof(desc)); 9228f2e8d1fSaliguori desc.special = vlan_special; 9238f2e8d1fSaliguori desc.status |= (vlan_status | E1000_RXD_STAT_DD); 9247c23b892Sbalrog if (desc.buffer_addr) { 925b19487e2SMichael S. Tsirkin if (desc_offset < size) { 92697410ddeSVincenzo Maffione size_t iov_copy; 92797410ddeSVincenzo Maffione hwaddr ba = le64_to_cpu(desc.buffer_addr); 928b19487e2SMichael S. Tsirkin size_t copy_size = size - desc_offset; 929b19487e2SMichael S. Tsirkin if (copy_size > s->rxbuf_size) { 930b19487e2SMichael S. Tsirkin copy_size = s->rxbuf_size; 931b19487e2SMichael S. Tsirkin } 93297410ddeSVincenzo Maffione do { 93397410ddeSVincenzo Maffione iov_copy = MIN(copy_size, iov->iov_len - iov_ofs); 93497410ddeSVincenzo Maffione pci_dma_write(d, ba, iov->iov_base + iov_ofs, iov_copy); 93597410ddeSVincenzo Maffione copy_size -= iov_copy; 93697410ddeSVincenzo Maffione ba += iov_copy; 93797410ddeSVincenzo Maffione iov_ofs += iov_copy; 93897410ddeSVincenzo Maffione if (iov_ofs == iov->iov_len) { 93997410ddeSVincenzo Maffione iov++; 94097410ddeSVincenzo Maffione iov_ofs = 0; 94197410ddeSVincenzo Maffione } 94297410ddeSVincenzo Maffione } while (copy_size); 943b19487e2SMichael S. Tsirkin } 944b19487e2SMichael S. Tsirkin desc_offset += desc_size; 945b19487e2SMichael S. Tsirkin desc.length = cpu_to_le16(desc_size); 946ee912ccfSMichael S. Tsirkin if (desc_offset >= total_size) { 9477c23b892Sbalrog desc.status |= E1000_RXD_STAT_EOP | E1000_RXD_STAT_IXSM; 948b19487e2SMichael S. Tsirkin } else { 949ee912ccfSMichael S. Tsirkin /* Guest zeroing out status is not a hardware requirement. 950ee912ccfSMichael S. Tsirkin Clear EOP in case guest didn't do it. */ 951ee912ccfSMichael S. Tsirkin desc.status &= ~E1000_RXD_STAT_EOP; 952b19487e2SMichael S. Tsirkin } 95343ad7e3eSJes Sorensen } else { // as per intel docs; skip descriptors with null buf addr 9547c23b892Sbalrog DBGOUT(RX, "Null RX descriptor!!\n"); 95543ad7e3eSJes Sorensen } 956b08340d5SAndreas Färber pci_dma_write(d, base, &desc, sizeof(desc)); 9577c23b892Sbalrog 9587c23b892Sbalrog if (++s->mac_reg[RDH] * sizeof(desc) >= s->mac_reg[RDLEN]) 9597c23b892Sbalrog s->mac_reg[RDH] = 0; 9607c23b892Sbalrog /* see comment in start_xmit; same here */ 961dd793a74SLaszlo Ersek if (s->mac_reg[RDH] == rdh_start || 962dd793a74SLaszlo Ersek rdh_start >= s->mac_reg[RDLEN] / sizeof(desc)) { 9637c23b892Sbalrog DBGOUT(RXERR, "RDH wraparound @%x, RDT %x, RDLEN %x\n", 9647c23b892Sbalrog rdh_start, s->mac_reg[RDT], s->mac_reg[RDLEN]); 9657c23b892Sbalrog set_ics(s, 0, E1000_ICS_RXO); 9664f1c942bSMark McLoughlin return -1; 9677c23b892Sbalrog } 968b19487e2SMichael S. Tsirkin } while (desc_offset < total_size); 9697c23b892Sbalrog 970093454e2SDmitry Fleytman e1000x_update_rx_total_stats(s->mac_reg, size, total_size); 9717c23b892Sbalrog 9727c23b892Sbalrog n = E1000_ICS_RXT0; 9737c23b892Sbalrog if ((rdt = s->mac_reg[RDT]) < s->mac_reg[RDH]) 9747c23b892Sbalrog rdt += s->mac_reg[RDLEN] / sizeof(desc); 975bf16cc8fSaliguori if (((rdt - s->mac_reg[RDH]) * sizeof(desc)) <= s->mac_reg[RDLEN] >> 976bf16cc8fSaliguori s->rxbuf_min_shift) 9777c23b892Sbalrog n |= E1000_ICS_RXDMT0; 9787c23b892Sbalrog 9797c23b892Sbalrog set_ics(s, 0, n); 9804f1c942bSMark McLoughlin 9814f1c942bSMark McLoughlin return size; 9827c23b892Sbalrog } 9837c23b892Sbalrog 98497410ddeSVincenzo Maffione static ssize_t 98597410ddeSVincenzo Maffione e1000_receive(NetClientState *nc, const uint8_t *buf, size_t size) 98697410ddeSVincenzo Maffione { 98797410ddeSVincenzo Maffione const struct iovec iov = { 98897410ddeSVincenzo Maffione .iov_base = (uint8_t *)buf, 98997410ddeSVincenzo Maffione .iov_len = size 99097410ddeSVincenzo Maffione }; 99197410ddeSVincenzo Maffione 99297410ddeSVincenzo Maffione return e1000_receive_iov(nc, &iov, 1); 99397410ddeSVincenzo Maffione } 99497410ddeSVincenzo Maffione 9957c23b892Sbalrog static uint32_t 9967c23b892Sbalrog mac_readreg(E1000State *s, int index) 9977c23b892Sbalrog { 9987c23b892Sbalrog return s->mac_reg[index]; 9997c23b892Sbalrog } 10007c23b892Sbalrog 10017c23b892Sbalrog static uint32_t 100272ea771cSLeonid Bloch mac_low4_read(E1000State *s, int index) 100372ea771cSLeonid Bloch { 100472ea771cSLeonid Bloch return s->mac_reg[index] & 0xf; 100572ea771cSLeonid Bloch } 100672ea771cSLeonid Bloch 100772ea771cSLeonid Bloch static uint32_t 100872ea771cSLeonid Bloch mac_low11_read(E1000State *s, int index) 100972ea771cSLeonid Bloch { 101072ea771cSLeonid Bloch return s->mac_reg[index] & 0x7ff; 101172ea771cSLeonid Bloch } 101272ea771cSLeonid Bloch 101372ea771cSLeonid Bloch static uint32_t 101472ea771cSLeonid Bloch mac_low13_read(E1000State *s, int index) 101572ea771cSLeonid Bloch { 101672ea771cSLeonid Bloch return s->mac_reg[index] & 0x1fff; 101772ea771cSLeonid Bloch } 101872ea771cSLeonid Bloch 101972ea771cSLeonid Bloch static uint32_t 102072ea771cSLeonid Bloch mac_low16_read(E1000State *s, int index) 102172ea771cSLeonid Bloch { 102272ea771cSLeonid Bloch return s->mac_reg[index] & 0xffff; 102372ea771cSLeonid Bloch } 102472ea771cSLeonid Bloch 102572ea771cSLeonid Bloch static uint32_t 10267c23b892Sbalrog mac_icr_read(E1000State *s, int index) 10277c23b892Sbalrog { 10287c23b892Sbalrog uint32_t ret = s->mac_reg[ICR]; 10297c23b892Sbalrog 10307c23b892Sbalrog DBGOUT(INTERRUPT, "ICR read: %x\n", ret); 10317c23b892Sbalrog set_interrupt_cause(s, 0, 0); 10327c23b892Sbalrog return ret; 10337c23b892Sbalrog } 10347c23b892Sbalrog 10357c23b892Sbalrog static uint32_t 10367c23b892Sbalrog mac_read_clr4(E1000State *s, int index) 10377c23b892Sbalrog { 10387c23b892Sbalrog uint32_t ret = s->mac_reg[index]; 10397c23b892Sbalrog 10407c23b892Sbalrog s->mac_reg[index] = 0; 10417c23b892Sbalrog return ret; 10427c23b892Sbalrog } 10437c23b892Sbalrog 10447c23b892Sbalrog static uint32_t 10457c23b892Sbalrog mac_read_clr8(E1000State *s, int index) 10467c23b892Sbalrog { 10477c23b892Sbalrog uint32_t ret = s->mac_reg[index]; 10487c23b892Sbalrog 10497c23b892Sbalrog s->mac_reg[index] = 0; 10507c23b892Sbalrog s->mac_reg[index-1] = 0; 10517c23b892Sbalrog return ret; 10527c23b892Sbalrog } 10537c23b892Sbalrog 10547c23b892Sbalrog static void 10557c23b892Sbalrog mac_writereg(E1000State *s, int index, uint32_t val) 10567c23b892Sbalrog { 10577c36507cSAmos Kong uint32_t macaddr[2]; 10587c36507cSAmos Kong 10597c23b892Sbalrog s->mac_reg[index] = val; 10607c36507cSAmos Kong 106190d131fbSMichael S. Tsirkin if (index == RA + 1) { 10627c36507cSAmos Kong macaddr[0] = cpu_to_le32(s->mac_reg[RA]); 10637c36507cSAmos Kong macaddr[1] = cpu_to_le32(s->mac_reg[RA + 1]); 10647c36507cSAmos Kong qemu_format_nic_info_str(qemu_get_queue(s->nic), (uint8_t *)macaddr); 10657c36507cSAmos Kong } 10667c23b892Sbalrog } 10677c23b892Sbalrog 10687c23b892Sbalrog static void 10697c23b892Sbalrog set_rdt(E1000State *s, int index, uint32_t val) 10707c23b892Sbalrog { 10717c23b892Sbalrog s->mac_reg[index] = val & 0xffff; 1072e8b4c680SPaolo Bonzini if (e1000_has_rxbufs(s, 1)) { 1073b356f76dSJason Wang qemu_flush_queued_packets(qemu_get_queue(s->nic)); 1074e8b4c680SPaolo Bonzini } 10757c23b892Sbalrog } 10767c23b892Sbalrog 10777c23b892Sbalrog static void 10787c23b892Sbalrog set_16bit(E1000State *s, int index, uint32_t val) 10797c23b892Sbalrog { 10807c23b892Sbalrog s->mac_reg[index] = val & 0xffff; 10817c23b892Sbalrog } 10827c23b892Sbalrog 10837c23b892Sbalrog static void 10847c23b892Sbalrog set_dlen(E1000State *s, int index, uint32_t val) 10857c23b892Sbalrog { 10867c23b892Sbalrog s->mac_reg[index] = val & 0xfff80; 10877c23b892Sbalrog } 10887c23b892Sbalrog 10897c23b892Sbalrog static void 10907c23b892Sbalrog set_tctl(E1000State *s, int index, uint32_t val) 10917c23b892Sbalrog { 10927c23b892Sbalrog s->mac_reg[index] = val; 10937c23b892Sbalrog s->mac_reg[TDT] &= 0xffff; 10947c23b892Sbalrog start_xmit(s); 10957c23b892Sbalrog } 10967c23b892Sbalrog 10977c23b892Sbalrog static void 10987c23b892Sbalrog set_icr(E1000State *s, int index, uint32_t val) 10997c23b892Sbalrog { 11007c23b892Sbalrog DBGOUT(INTERRUPT, "set_icr %x\n", val); 11017c23b892Sbalrog set_interrupt_cause(s, 0, s->mac_reg[ICR] & ~val); 11027c23b892Sbalrog } 11037c23b892Sbalrog 11047c23b892Sbalrog static void 11057c23b892Sbalrog set_imc(E1000State *s, int index, uint32_t val) 11067c23b892Sbalrog { 11077c23b892Sbalrog s->mac_reg[IMS] &= ~val; 11087c23b892Sbalrog set_ics(s, 0, 0); 11097c23b892Sbalrog } 11107c23b892Sbalrog 11117c23b892Sbalrog static void 11127c23b892Sbalrog set_ims(E1000State *s, int index, uint32_t val) 11137c23b892Sbalrog { 11147c23b892Sbalrog s->mac_reg[IMS] |= val; 11157c23b892Sbalrog set_ics(s, 0, 0); 11167c23b892Sbalrog } 11177c23b892Sbalrog 11187c23b892Sbalrog #define getreg(x) [x] = mac_readreg 11197c23b892Sbalrog static uint32_t (*macreg_readops[])(E1000State *, int) = { 11207c23b892Sbalrog getreg(PBA), getreg(RCTL), getreg(TDH), getreg(TXDCTL), 11217c23b892Sbalrog getreg(WUFC), getreg(TDT), getreg(CTRL), getreg(LEDCTL), 11227c23b892Sbalrog getreg(MANC), getreg(MDIC), getreg(SWSM), getreg(STATUS), 11237c23b892Sbalrog getreg(TORL), getreg(TOTL), getreg(IMS), getreg(TCTL), 1124b1332393SBill Paul getreg(RDH), getreg(RDT), getreg(VET), getreg(ICS), 1125a00b2335SKay Ackermann getreg(TDBAL), getreg(TDBAH), getreg(RDBAH), getreg(RDBAL), 1126e9845f09SVincenzo Maffione getreg(TDLEN), getreg(RDLEN), getreg(RDTR), getreg(RADV), 112772ea771cSLeonid Bloch getreg(TADV), getreg(ITR), getreg(FCRUC), getreg(IPAV), 112872ea771cSLeonid Bloch getreg(WUC), getreg(WUS), getreg(SCC), getreg(ECOL), 112972ea771cSLeonid Bloch getreg(MCC), getreg(LATECOL), getreg(COLC), getreg(DC), 113072ea771cSLeonid Bloch getreg(TNCRS), getreg(SEC), getreg(CEXTERR), getreg(RLEC), 113172ea771cSLeonid Bloch getreg(XONRXC), getreg(XONTXC), getreg(XOFFRXC), getreg(XOFFTXC), 113272ea771cSLeonid Bloch getreg(RFC), getreg(RJC), getreg(RNBC), getreg(TSCTFC), 11333b274301SLeonid Bloch getreg(MGTPRC), getreg(MGTPDC), getreg(MGTPTC), getreg(GORCL), 11343b274301SLeonid Bloch getreg(GOTCL), 11357c23b892Sbalrog 113620f3e863SLeonid Bloch [TOTH] = mac_read_clr8, [TORH] = mac_read_clr8, 11373b274301SLeonid Bloch [GOTCH] = mac_read_clr8, [GORCH] = mac_read_clr8, 11383b274301SLeonid Bloch [PRC64] = mac_read_clr4, [PRC127] = mac_read_clr4, 11393b274301SLeonid Bloch [PRC255] = mac_read_clr4, [PRC511] = mac_read_clr4, 11403b274301SLeonid Bloch [PRC1023] = mac_read_clr4, [PRC1522] = mac_read_clr4, 11413b274301SLeonid Bloch [PTC64] = mac_read_clr4, [PTC127] = mac_read_clr4, 11423b274301SLeonid Bloch [PTC255] = mac_read_clr4, [PTC511] = mac_read_clr4, 11433b274301SLeonid Bloch [PTC1023] = mac_read_clr4, [PTC1522] = mac_read_clr4, 114420f3e863SLeonid Bloch [GPRC] = mac_read_clr4, [GPTC] = mac_read_clr4, 114520f3e863SLeonid Bloch [TPT] = mac_read_clr4, [TPR] = mac_read_clr4, 11463b274301SLeonid Bloch [RUC] = mac_read_clr4, [ROC] = mac_read_clr4, 11473b274301SLeonid Bloch [BPRC] = mac_read_clr4, [MPRC] = mac_read_clr4, 11483b274301SLeonid Bloch [TSCTC] = mac_read_clr4, [BPTC] = mac_read_clr4, 11493b274301SLeonid Bloch [MPTC] = mac_read_clr4, 115020f3e863SLeonid Bloch [ICR] = mac_icr_read, [EECD] = get_eecd, 115120f3e863SLeonid Bloch [EERD] = flash_eerd_read, 115272ea771cSLeonid Bloch [RDFH] = mac_low13_read, [RDFT] = mac_low13_read, 115372ea771cSLeonid Bloch [RDFHS] = mac_low13_read, [RDFTS] = mac_low13_read, 115472ea771cSLeonid Bloch [RDFPC] = mac_low13_read, 115572ea771cSLeonid Bloch [TDFH] = mac_low11_read, [TDFT] = mac_low11_read, 115672ea771cSLeonid Bloch [TDFHS] = mac_low13_read, [TDFTS] = mac_low13_read, 115772ea771cSLeonid Bloch [TDFPC] = mac_low13_read, 115872ea771cSLeonid Bloch [AIT] = mac_low16_read, 115920f3e863SLeonid Bloch 11607c23b892Sbalrog [CRCERRS ... MPC] = &mac_readreg, 116172ea771cSLeonid Bloch [IP6AT ... IP6AT+3] = &mac_readreg, [IP4AT ... IP4AT+6] = &mac_readreg, 116272ea771cSLeonid Bloch [FFLT ... FFLT+6] = &mac_low11_read, 11637c23b892Sbalrog [RA ... RA+31] = &mac_readreg, 116472ea771cSLeonid Bloch [WUPM ... WUPM+31] = &mac_readreg, 11657c23b892Sbalrog [MTA ... MTA+127] = &mac_readreg, 11668f2e8d1fSaliguori [VFTA ... VFTA+127] = &mac_readreg, 116772ea771cSLeonid Bloch [FFMT ... FFMT+254] = &mac_low4_read, 116872ea771cSLeonid Bloch [FFVT ... FFVT+254] = &mac_readreg, 116972ea771cSLeonid Bloch [PBM ... PBM+16383] = &mac_readreg, 11707c23b892Sbalrog }; 1171b1503cdaSmalc enum { NREADOPS = ARRAY_SIZE(macreg_readops) }; 11727c23b892Sbalrog 11737c23b892Sbalrog #define putreg(x) [x] = mac_writereg 11747c23b892Sbalrog static void (*macreg_writeops[])(E1000State *, int, uint32_t) = { 11757c23b892Sbalrog putreg(PBA), putreg(EERD), putreg(SWSM), putreg(WUFC), 11767c23b892Sbalrog putreg(TDBAL), putreg(TDBAH), putreg(TXDCTL), putreg(RDBAH), 117772ea771cSLeonid Bloch putreg(RDBAL), putreg(LEDCTL), putreg(VET), putreg(FCRUC), 117872ea771cSLeonid Bloch putreg(TDFH), putreg(TDFT), putreg(TDFHS), putreg(TDFTS), 117972ea771cSLeonid Bloch putreg(TDFPC), putreg(RDFH), putreg(RDFT), putreg(RDFHS), 118072ea771cSLeonid Bloch putreg(RDFTS), putreg(RDFPC), putreg(IPAV), putreg(WUC), 118172ea771cSLeonid Bloch putreg(WUS), putreg(AIT), 118220f3e863SLeonid Bloch 11837c23b892Sbalrog [TDLEN] = set_dlen, [RDLEN] = set_dlen, [TCTL] = set_tctl, 11847c23b892Sbalrog [TDT] = set_tctl, [MDIC] = set_mdic, [ICS] = set_ics, 11857c23b892Sbalrog [TDH] = set_16bit, [RDH] = set_16bit, [RDT] = set_rdt, 11867c23b892Sbalrog [IMC] = set_imc, [IMS] = set_ims, [ICR] = set_icr, 1187cab3c825SKevin Wolf [EECD] = set_eecd, [RCTL] = set_rx_control, [CTRL] = set_ctrl, 1188e9845f09SVincenzo Maffione [RDTR] = set_16bit, [RADV] = set_16bit, [TADV] = set_16bit, 1189e9845f09SVincenzo Maffione [ITR] = set_16bit, 119020f3e863SLeonid Bloch 119172ea771cSLeonid Bloch [IP6AT ... IP6AT+3] = &mac_writereg, [IP4AT ... IP4AT+6] = &mac_writereg, 119272ea771cSLeonid Bloch [FFLT ... FFLT+6] = &mac_writereg, 11937c23b892Sbalrog [RA ... RA+31] = &mac_writereg, 119472ea771cSLeonid Bloch [WUPM ... WUPM+31] = &mac_writereg, 11957c23b892Sbalrog [MTA ... MTA+127] = &mac_writereg, 11968f2e8d1fSaliguori [VFTA ... VFTA+127] = &mac_writereg, 119772ea771cSLeonid Bloch [FFMT ... FFMT+254] = &mac_writereg, [FFVT ... FFVT+254] = &mac_writereg, 119872ea771cSLeonid Bloch [PBM ... PBM+16383] = &mac_writereg, 11997c23b892Sbalrog }; 1200b9d03e35SJason Wang 1201b1503cdaSmalc enum { NWRITEOPS = ARRAY_SIZE(macreg_writeops) }; 12027c23b892Sbalrog 1203bc0f0674SLeonid Bloch enum { MAC_ACCESS_PARTIAL = 1, MAC_ACCESS_FLAG_NEEDED = 2 }; 1204bc0f0674SLeonid Bloch 1205bc0f0674SLeonid Bloch #define markflag(x) ((E1000_FLAG_##x << 2) | MAC_ACCESS_FLAG_NEEDED) 1206bc0f0674SLeonid Bloch /* In the array below the meaning of the bits is: [f|f|f|f|f|f|n|p] 1207bc0f0674SLeonid Bloch * f - flag bits (up to 6 possible flags) 1208bc0f0674SLeonid Bloch * n - flag needed 1209bc0f0674SLeonid Bloch * p - partially implenented */ 1210bc0f0674SLeonid Bloch static const uint8_t mac_reg_access[0x8000] = { 1211bc0f0674SLeonid Bloch [RDTR] = markflag(MIT), [TADV] = markflag(MIT), 1212bc0f0674SLeonid Bloch [RADV] = markflag(MIT), [ITR] = markflag(MIT), 121372ea771cSLeonid Bloch 121472ea771cSLeonid Bloch [IPAV] = markflag(MAC), [WUC] = markflag(MAC), 121572ea771cSLeonid Bloch [IP6AT] = markflag(MAC), [IP4AT] = markflag(MAC), 121672ea771cSLeonid Bloch [FFVT] = markflag(MAC), [WUPM] = markflag(MAC), 121772ea771cSLeonid Bloch [ECOL] = markflag(MAC), [MCC] = markflag(MAC), 121872ea771cSLeonid Bloch [DC] = markflag(MAC), [TNCRS] = markflag(MAC), 121972ea771cSLeonid Bloch [RLEC] = markflag(MAC), [XONRXC] = markflag(MAC), 122072ea771cSLeonid Bloch [XOFFTXC] = markflag(MAC), [RFC] = markflag(MAC), 122172ea771cSLeonid Bloch [TSCTFC] = markflag(MAC), [MGTPRC] = markflag(MAC), 122272ea771cSLeonid Bloch [WUS] = markflag(MAC), [AIT] = markflag(MAC), 122372ea771cSLeonid Bloch [FFLT] = markflag(MAC), [FFMT] = markflag(MAC), 122472ea771cSLeonid Bloch [SCC] = markflag(MAC), [FCRUC] = markflag(MAC), 122572ea771cSLeonid Bloch [LATECOL] = markflag(MAC), [COLC] = markflag(MAC), 122672ea771cSLeonid Bloch [SEC] = markflag(MAC), [CEXTERR] = markflag(MAC), 122772ea771cSLeonid Bloch [XONTXC] = markflag(MAC), [XOFFRXC] = markflag(MAC), 122872ea771cSLeonid Bloch [RJC] = markflag(MAC), [RNBC] = markflag(MAC), 122972ea771cSLeonid Bloch [MGTPDC] = markflag(MAC), [MGTPTC] = markflag(MAC), 12303b274301SLeonid Bloch [RUC] = markflag(MAC), [ROC] = markflag(MAC), 12313b274301SLeonid Bloch [GORCL] = markflag(MAC), [GORCH] = markflag(MAC), 12323b274301SLeonid Bloch [GOTCL] = markflag(MAC), [GOTCH] = markflag(MAC), 12333b274301SLeonid Bloch [BPRC] = markflag(MAC), [MPRC] = markflag(MAC), 12343b274301SLeonid Bloch [TSCTC] = markflag(MAC), [PRC64] = markflag(MAC), 12353b274301SLeonid Bloch [PRC127] = markflag(MAC), [PRC255] = markflag(MAC), 12363b274301SLeonid Bloch [PRC511] = markflag(MAC), [PRC1023] = markflag(MAC), 12373b274301SLeonid Bloch [PRC1522] = markflag(MAC), [PTC64] = markflag(MAC), 12383b274301SLeonid Bloch [PTC127] = markflag(MAC), [PTC255] = markflag(MAC), 12393b274301SLeonid Bloch [PTC511] = markflag(MAC), [PTC1023] = markflag(MAC), 12403b274301SLeonid Bloch [PTC1522] = markflag(MAC), [MPTC] = markflag(MAC), 12413b274301SLeonid Bloch [BPTC] = markflag(MAC), 124272ea771cSLeonid Bloch 124372ea771cSLeonid Bloch [TDFH] = markflag(MAC) | MAC_ACCESS_PARTIAL, 124472ea771cSLeonid Bloch [TDFT] = markflag(MAC) | MAC_ACCESS_PARTIAL, 124572ea771cSLeonid Bloch [TDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL, 124672ea771cSLeonid Bloch [TDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL, 124772ea771cSLeonid Bloch [TDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL, 124872ea771cSLeonid Bloch [RDFH] = markflag(MAC) | MAC_ACCESS_PARTIAL, 124972ea771cSLeonid Bloch [RDFT] = markflag(MAC) | MAC_ACCESS_PARTIAL, 125072ea771cSLeonid Bloch [RDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL, 125172ea771cSLeonid Bloch [RDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL, 125272ea771cSLeonid Bloch [RDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL, 125372ea771cSLeonid Bloch [PBM] = markflag(MAC) | MAC_ACCESS_PARTIAL, 1254bc0f0674SLeonid Bloch }; 1255bc0f0674SLeonid Bloch 12567c23b892Sbalrog static void 1257a8170e5eSAvi Kivity e1000_mmio_write(void *opaque, hwaddr addr, uint64_t val, 1258ad00a9b9SAvi Kivity unsigned size) 12597c23b892Sbalrog { 12607c23b892Sbalrog E1000State *s = opaque; 12618da3ff18Spbrook unsigned int index = (addr & 0x1ffff) >> 2; 12627c23b892Sbalrog 126343ad7e3eSJes Sorensen if (index < NWRITEOPS && macreg_writeops[index]) { 1264bc0f0674SLeonid Bloch if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED) 1265bc0f0674SLeonid Bloch || (s->compat_flags & (mac_reg_access[index] >> 2))) { 1266bc0f0674SLeonid Bloch if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) { 1267bc0f0674SLeonid Bloch DBGOUT(GENERAL, "Writing to register at offset: 0x%08x. " 1268bc0f0674SLeonid Bloch "It is not fully implemented.\n", index<<2); 1269bc0f0674SLeonid Bloch } 12706b59fc74Saurel32 macreg_writeops[index](s, index, val); 1271bc0f0674SLeonid Bloch } else { /* "flag needed" bit is set, but the flag is not active */ 1272bc0f0674SLeonid Bloch DBGOUT(MMIO, "MMIO write attempt to disabled reg. addr=0x%08x\n", 1273bc0f0674SLeonid Bloch index<<2); 1274bc0f0674SLeonid Bloch } 127543ad7e3eSJes Sorensen } else if (index < NREADOPS && macreg_readops[index]) { 1276bc0f0674SLeonid Bloch DBGOUT(MMIO, "e1000_mmio_writel RO %x: 0x%04"PRIx64"\n", 1277bc0f0674SLeonid Bloch index<<2, val); 127843ad7e3eSJes Sorensen } else { 1279ad00a9b9SAvi Kivity DBGOUT(UNKNOWN, "MMIO unknown write addr=0x%08x,val=0x%08"PRIx64"\n", 12807c23b892Sbalrog index<<2, val); 12817c23b892Sbalrog } 128243ad7e3eSJes Sorensen } 12837c23b892Sbalrog 1284ad00a9b9SAvi Kivity static uint64_t 1285a8170e5eSAvi Kivity e1000_mmio_read(void *opaque, hwaddr addr, unsigned size) 12867c23b892Sbalrog { 12877c23b892Sbalrog E1000State *s = opaque; 12888da3ff18Spbrook unsigned int index = (addr & 0x1ffff) >> 2; 12897c23b892Sbalrog 1290bc0f0674SLeonid Bloch if (index < NREADOPS && macreg_readops[index]) { 1291bc0f0674SLeonid Bloch if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED) 1292bc0f0674SLeonid Bloch || (s->compat_flags & (mac_reg_access[index] >> 2))) { 1293bc0f0674SLeonid Bloch if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) { 1294bc0f0674SLeonid Bloch DBGOUT(GENERAL, "Reading register at offset: 0x%08x. " 1295bc0f0674SLeonid Bloch "It is not fully implemented.\n", index<<2); 12966b59fc74Saurel32 } 1297bc0f0674SLeonid Bloch return macreg_readops[index](s, index); 1298bc0f0674SLeonid Bloch } else { /* "flag needed" bit is set, but the flag is not active */ 1299bc0f0674SLeonid Bloch DBGOUT(MMIO, "MMIO read attempt of disabled reg. addr=0x%08x\n", 1300bc0f0674SLeonid Bloch index<<2); 1301bc0f0674SLeonid Bloch } 1302bc0f0674SLeonid Bloch } else { 13037c23b892Sbalrog DBGOUT(UNKNOWN, "MMIO unknown read addr=0x%08x\n", index<<2); 1304bc0f0674SLeonid Bloch } 13057c23b892Sbalrog return 0; 13067c23b892Sbalrog } 13077c23b892Sbalrog 1308ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_mmio_ops = { 1309ad00a9b9SAvi Kivity .read = e1000_mmio_read, 1310ad00a9b9SAvi Kivity .write = e1000_mmio_write, 1311ad00a9b9SAvi Kivity .endianness = DEVICE_LITTLE_ENDIAN, 1312ad00a9b9SAvi Kivity .impl = { 1313ad00a9b9SAvi Kivity .min_access_size = 4, 1314ad00a9b9SAvi Kivity .max_access_size = 4, 1315ad00a9b9SAvi Kivity }, 1316ad00a9b9SAvi Kivity }; 1317ad00a9b9SAvi Kivity 1318a8170e5eSAvi Kivity static uint64_t e1000_io_read(void *opaque, hwaddr addr, 1319ad00a9b9SAvi Kivity unsigned size) 13207c23b892Sbalrog { 1321ad00a9b9SAvi Kivity E1000State *s = opaque; 1322ad00a9b9SAvi Kivity 1323ad00a9b9SAvi Kivity (void)s; 1324ad00a9b9SAvi Kivity return 0; 13257c23b892Sbalrog } 13267c23b892Sbalrog 1327a8170e5eSAvi Kivity static void e1000_io_write(void *opaque, hwaddr addr, 1328ad00a9b9SAvi Kivity uint64_t val, unsigned size) 13297c23b892Sbalrog { 1330ad00a9b9SAvi Kivity E1000State *s = opaque; 1331ad00a9b9SAvi Kivity 1332ad00a9b9SAvi Kivity (void)s; 13337c23b892Sbalrog } 13347c23b892Sbalrog 1335ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_io_ops = { 1336ad00a9b9SAvi Kivity .read = e1000_io_read, 1337ad00a9b9SAvi Kivity .write = e1000_io_write, 1338ad00a9b9SAvi Kivity .endianness = DEVICE_LITTLE_ENDIAN, 1339ad00a9b9SAvi Kivity }; 1340ad00a9b9SAvi Kivity 1341e482dc3eSJuan Quintela static bool is_version_1(void *opaque, int version_id) 13427c23b892Sbalrog { 1343e482dc3eSJuan Quintela return version_id == 1; 13447c23b892Sbalrog } 13457c23b892Sbalrog 1346ddcb73b7SMichael S. Tsirkin static void e1000_pre_save(void *opaque) 1347ddcb73b7SMichael S. Tsirkin { 1348ddcb73b7SMichael S. Tsirkin E1000State *s = opaque; 1349ddcb73b7SMichael S. Tsirkin NetClientState *nc = qemu_get_queue(s->nic); 13502af234e6SMichael S. Tsirkin 1351e9845f09SVincenzo Maffione /* If the mitigation timer is active, emulate a timeout now. */ 1352e9845f09SVincenzo Maffione if (s->mit_timer_on) { 1353e9845f09SVincenzo Maffione e1000_mit_timer(s); 1354e9845f09SVincenzo Maffione } 1355e9845f09SVincenzo Maffione 1356ddcb73b7SMichael S. Tsirkin /* 13576a2acedbSGabriel L. Somlo * If link is down and auto-negotiation is supported and ongoing, 13586a2acedbSGabriel L. Somlo * complete auto-negotiation immediately. This allows us to look 13596a2acedbSGabriel L. Somlo * at MII_SR_AUTONEG_COMPLETE to infer link status on load. 1360ddcb73b7SMichael S. Tsirkin */ 1361d7a41552SGabriel L. Somlo if (nc->link_down && have_autoneg(s)) { 1362ddcb73b7SMichael S. Tsirkin s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE; 1363ddcb73b7SMichael S. Tsirkin } 1364ddcb73b7SMichael S. Tsirkin } 1365ddcb73b7SMichael S. Tsirkin 1366e4b82364SAmos Kong static int e1000_post_load(void *opaque, int version_id) 1367e4b82364SAmos Kong { 1368e4b82364SAmos Kong E1000State *s = opaque; 1369b356f76dSJason Wang NetClientState *nc = qemu_get_queue(s->nic); 1370e4b82364SAmos Kong 1371bc0f0674SLeonid Bloch if (!chkflag(MIT)) { 1372e9845f09SVincenzo Maffione s->mac_reg[ITR] = s->mac_reg[RDTR] = s->mac_reg[RADV] = 1373e9845f09SVincenzo Maffione s->mac_reg[TADV] = 0; 1374e9845f09SVincenzo Maffione s->mit_irq_level = false; 1375e9845f09SVincenzo Maffione } 1376e9845f09SVincenzo Maffione s->mit_ide = 0; 1377e9845f09SVincenzo Maffione s->mit_timer_on = false; 1378e9845f09SVincenzo Maffione 1379e4b82364SAmos Kong /* nc.link_down can't be migrated, so infer link_down according 1380ddcb73b7SMichael S. Tsirkin * to link status bit in mac_reg[STATUS]. 1381ddcb73b7SMichael S. Tsirkin * Alternatively, restart link negotiation if it was in progress. */ 1382b356f76dSJason Wang nc->link_down = (s->mac_reg[STATUS] & E1000_STATUS_LU) == 0; 13832af234e6SMichael S. Tsirkin 1384d7a41552SGabriel L. Somlo if (have_autoneg(s) && 1385ddcb73b7SMichael S. Tsirkin !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) { 1386ddcb73b7SMichael S. Tsirkin nc->link_down = false; 1387d7a41552SGabriel L. Somlo timer_mod(s->autoneg_timer, 1388d7a41552SGabriel L. Somlo qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500); 1389ddcb73b7SMichael S. Tsirkin } 1390e4b82364SAmos Kong 1391e4b82364SAmos Kong return 0; 1392e4b82364SAmos Kong } 1393e4b82364SAmos Kong 1394e9845f09SVincenzo Maffione static bool e1000_mit_state_needed(void *opaque) 1395e9845f09SVincenzo Maffione { 1396e9845f09SVincenzo Maffione E1000State *s = opaque; 1397e9845f09SVincenzo Maffione 1398bc0f0674SLeonid Bloch return chkflag(MIT); 1399e9845f09SVincenzo Maffione } 1400e9845f09SVincenzo Maffione 14019e117734SLeonid Bloch static bool e1000_full_mac_needed(void *opaque) 14029e117734SLeonid Bloch { 14039e117734SLeonid Bloch E1000State *s = opaque; 14049e117734SLeonid Bloch 1405bc0f0674SLeonid Bloch return chkflag(MAC); 14069e117734SLeonid Bloch } 14079e117734SLeonid Bloch 1408e9845f09SVincenzo Maffione static const VMStateDescription vmstate_e1000_mit_state = { 1409e9845f09SVincenzo Maffione .name = "e1000/mit_state", 1410e9845f09SVincenzo Maffione .version_id = 1, 1411e9845f09SVincenzo Maffione .minimum_version_id = 1, 14125cd8cadaSJuan Quintela .needed = e1000_mit_state_needed, 1413e9845f09SVincenzo Maffione .fields = (VMStateField[]) { 1414e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[RDTR], E1000State), 1415e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[RADV], E1000State), 1416e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[TADV], E1000State), 1417e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[ITR], E1000State), 1418e9845f09SVincenzo Maffione VMSTATE_BOOL(mit_irq_level, E1000State), 1419e9845f09SVincenzo Maffione VMSTATE_END_OF_LIST() 1420e9845f09SVincenzo Maffione } 1421e9845f09SVincenzo Maffione }; 1422e9845f09SVincenzo Maffione 14239e117734SLeonid Bloch static const VMStateDescription vmstate_e1000_full_mac_state = { 14249e117734SLeonid Bloch .name = "e1000/full_mac_state", 14259e117734SLeonid Bloch .version_id = 1, 14269e117734SLeonid Bloch .minimum_version_id = 1, 14279e117734SLeonid Bloch .needed = e1000_full_mac_needed, 14289e117734SLeonid Bloch .fields = (VMStateField[]) { 14299e117734SLeonid Bloch VMSTATE_UINT32_ARRAY(mac_reg, E1000State, 0x8000), 14309e117734SLeonid Bloch VMSTATE_END_OF_LIST() 14319e117734SLeonid Bloch } 14329e117734SLeonid Bloch }; 14339e117734SLeonid Bloch 1434e482dc3eSJuan Quintela static const VMStateDescription vmstate_e1000 = { 1435e482dc3eSJuan Quintela .name = "e1000", 1436e482dc3eSJuan Quintela .version_id = 2, 1437e482dc3eSJuan Quintela .minimum_version_id = 1, 1438ddcb73b7SMichael S. Tsirkin .pre_save = e1000_pre_save, 1439e4b82364SAmos Kong .post_load = e1000_post_load, 1440e482dc3eSJuan Quintela .fields = (VMStateField[]) { 1441b08340d5SAndreas Färber VMSTATE_PCI_DEVICE(parent_obj, E1000State), 1442e482dc3eSJuan Quintela VMSTATE_UNUSED_TEST(is_version_1, 4), /* was instance id */ 1443e482dc3eSJuan Quintela VMSTATE_UNUSED(4), /* Was mmio_base. */ 1444e482dc3eSJuan Quintela VMSTATE_UINT32(rxbuf_size, E1000State), 1445e482dc3eSJuan Quintela VMSTATE_UINT32(rxbuf_min_shift, E1000State), 1446e482dc3eSJuan Quintela VMSTATE_UINT32(eecd_state.val_in, E1000State), 1447e482dc3eSJuan Quintela VMSTATE_UINT16(eecd_state.bitnum_in, E1000State), 1448e482dc3eSJuan Quintela VMSTATE_UINT16(eecd_state.bitnum_out, E1000State), 1449e482dc3eSJuan Quintela VMSTATE_UINT16(eecd_state.reading, E1000State), 1450e482dc3eSJuan Quintela VMSTATE_UINT32(eecd_state.old_eecd, E1000State), 1451093454e2SDmitry Fleytman VMSTATE_UINT8(tx.props.ipcss, E1000State), 1452093454e2SDmitry Fleytman VMSTATE_UINT8(tx.props.ipcso, E1000State), 1453093454e2SDmitry Fleytman VMSTATE_UINT16(tx.props.ipcse, E1000State), 1454093454e2SDmitry Fleytman VMSTATE_UINT8(tx.props.tucss, E1000State), 1455093454e2SDmitry Fleytman VMSTATE_UINT8(tx.props.tucso, E1000State), 1456093454e2SDmitry Fleytman VMSTATE_UINT16(tx.props.tucse, E1000State), 1457093454e2SDmitry Fleytman VMSTATE_UINT32(tx.props.paylen, E1000State), 1458093454e2SDmitry Fleytman VMSTATE_UINT8(tx.props.hdr_len, E1000State), 1459093454e2SDmitry Fleytman VMSTATE_UINT16(tx.props.mss, E1000State), 1460e482dc3eSJuan Quintela VMSTATE_UINT16(tx.size, E1000State), 1461e482dc3eSJuan Quintela VMSTATE_UINT16(tx.tso_frames, E1000State), 1462093454e2SDmitry Fleytman VMSTATE_UINT8(tx.props.sum_needed, E1000State), 1463093454e2SDmitry Fleytman VMSTATE_INT8(tx.props.ip, E1000State), 1464093454e2SDmitry Fleytman VMSTATE_INT8(tx.props.tcp, E1000State), 1465e482dc3eSJuan Quintela VMSTATE_BUFFER(tx.header, E1000State), 1466e482dc3eSJuan Quintela VMSTATE_BUFFER(tx.data, E1000State), 1467e482dc3eSJuan Quintela VMSTATE_UINT16_ARRAY(eeprom_data, E1000State, 64), 1468e482dc3eSJuan Quintela VMSTATE_UINT16_ARRAY(phy_reg, E1000State, 0x20), 1469e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[CTRL], E1000State), 1470e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[EECD], E1000State), 1471e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[EERD], E1000State), 1472e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[GPRC], E1000State), 1473e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[GPTC], E1000State), 1474e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[ICR], E1000State), 1475e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[ICS], E1000State), 1476e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[IMC], E1000State), 1477e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[IMS], E1000State), 1478e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[LEDCTL], E1000State), 1479e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[MANC], E1000State), 1480e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[MDIC], E1000State), 1481e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[MPC], E1000State), 1482e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[PBA], E1000State), 1483e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RCTL], E1000State), 1484e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDBAH], E1000State), 1485e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDBAL], E1000State), 1486e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDH], E1000State), 1487e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDLEN], E1000State), 1488e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDT], E1000State), 1489e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[STATUS], E1000State), 1490e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[SWSM], E1000State), 1491e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TCTL], E1000State), 1492e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDBAH], E1000State), 1493e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDBAL], E1000State), 1494e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDH], E1000State), 1495e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDLEN], E1000State), 1496e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDT], E1000State), 1497e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TORH], E1000State), 1498e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TORL], E1000State), 1499e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TOTH], E1000State), 1500e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TOTL], E1000State), 1501e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TPR], E1000State), 1502e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TPT], E1000State), 1503e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TXDCTL], E1000State), 1504e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[WUFC], E1000State), 1505e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[VET], E1000State), 1506e482dc3eSJuan Quintela VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, RA, 32), 1507e482dc3eSJuan Quintela VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, MTA, 128), 1508e482dc3eSJuan Quintela VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, VFTA, 128), 1509e482dc3eSJuan Quintela VMSTATE_END_OF_LIST() 1510e9845f09SVincenzo Maffione }, 15115cd8cadaSJuan Quintela .subsections = (const VMStateDescription*[]) { 15125cd8cadaSJuan Quintela &vmstate_e1000_mit_state, 15139e117734SLeonid Bloch &vmstate_e1000_full_mac_state, 15145cd8cadaSJuan Quintela NULL 15157c23b892Sbalrog } 1516e482dc3eSJuan Quintela }; 15177c23b892Sbalrog 15188597f2e1SGabriel L. Somlo /* 15198597f2e1SGabriel L. Somlo * EEPROM contents documented in Tables 5-2 and 5-3, pp. 98-102. 15208597f2e1SGabriel L. Somlo * Note: A valid DevId will be inserted during pci_e1000_init(). 15218597f2e1SGabriel L. Somlo */ 152288b4e9dbSblueswir1 static const uint16_t e1000_eeprom_template[64] = { 15237c23b892Sbalrog 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0x0000, 15248597f2e1SGabriel L. Somlo 0x3000, 0x1000, 0x6403, 0 /*DevId*/, 0x8086, 0 /*DevId*/, 0x8086, 0x3040, 15257c23b892Sbalrog 0x0008, 0x2000, 0x7e14, 0x0048, 0x1000, 0x00d8, 0x0000, 0x2700, 15267c23b892Sbalrog 0x6cc9, 0x3150, 0x0722, 0x040b, 0x0984, 0x0000, 0xc000, 0x0706, 15277c23b892Sbalrog 0x1008, 0x0000, 0x0f04, 0x7fff, 0x4d01, 0xffff, 0xffff, 0xffff, 15287c23b892Sbalrog 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 15297c23b892Sbalrog 0x0100, 0x4000, 0x121c, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 15307c23b892Sbalrog 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x0000, 15317c23b892Sbalrog }; 15327c23b892Sbalrog 15337c23b892Sbalrog /* PCI interface */ 15347c23b892Sbalrog 15357c23b892Sbalrog static void 1536ad00a9b9SAvi Kivity e1000_mmio_setup(E1000State *d) 15377c23b892Sbalrog { 1538f65ed4c1Saliguori int i; 1539f65ed4c1Saliguori const uint32_t excluded_regs[] = { 1540f65ed4c1Saliguori E1000_MDIC, E1000_ICR, E1000_ICS, E1000_IMS, 1541f65ed4c1Saliguori E1000_IMC, E1000_TCTL, E1000_TDT, PNPMMIO_SIZE 1542f65ed4c1Saliguori }; 1543f65ed4c1Saliguori 1544eedfac6fSPaolo Bonzini memory_region_init_io(&d->mmio, OBJECT(d), &e1000_mmio_ops, d, 1545eedfac6fSPaolo Bonzini "e1000-mmio", PNPMMIO_SIZE); 1546ad00a9b9SAvi Kivity memory_region_add_coalescing(&d->mmio, 0, excluded_regs[0]); 1547f65ed4c1Saliguori for (i = 0; excluded_regs[i] != PNPMMIO_SIZE; i++) 1548ad00a9b9SAvi Kivity memory_region_add_coalescing(&d->mmio, excluded_regs[i] + 4, 1549ad00a9b9SAvi Kivity excluded_regs[i+1] - excluded_regs[i] - 4); 1550eedfac6fSPaolo Bonzini memory_region_init_io(&d->io, OBJECT(d), &e1000_io_ops, d, "e1000-io", IOPORT_SIZE); 15517c23b892Sbalrog } 15527c23b892Sbalrog 1553b946a153Saliguori static void 15544b09be85Saliguori pci_e1000_uninit(PCIDevice *dev) 15554b09be85Saliguori { 1556567a3c9eSPeter Crosthwaite E1000State *d = E1000(dev); 15574b09be85Saliguori 1558bc72ad67SAlex Bligh timer_del(d->autoneg_timer); 1559bc72ad67SAlex Bligh timer_free(d->autoneg_timer); 1560e9845f09SVincenzo Maffione timer_del(d->mit_timer); 1561e9845f09SVincenzo Maffione timer_free(d->mit_timer); 1562948ecf21SJason Wang qemu_del_nic(d->nic); 15634b09be85Saliguori } 15644b09be85Saliguori 1565a03e2aecSMark McLoughlin static NetClientInfo net_e1000_info = { 1566*f394b2e2SEric Blake .type = NET_CLIENT_DRIVER_NIC, 1567a03e2aecSMark McLoughlin .size = sizeof(NICState), 1568a03e2aecSMark McLoughlin .can_receive = e1000_can_receive, 1569a03e2aecSMark McLoughlin .receive = e1000_receive, 157097410ddeSVincenzo Maffione .receive_iov = e1000_receive_iov, 1571a03e2aecSMark McLoughlin .link_status_changed = e1000_set_link_status, 1572a03e2aecSMark McLoughlin }; 1573a03e2aecSMark McLoughlin 157420302e71SMichael S. Tsirkin static void e1000_write_config(PCIDevice *pci_dev, uint32_t address, 157520302e71SMichael S. Tsirkin uint32_t val, int len) 157620302e71SMichael S. Tsirkin { 157720302e71SMichael S. Tsirkin E1000State *s = E1000(pci_dev); 157820302e71SMichael S. Tsirkin 157920302e71SMichael S. Tsirkin pci_default_write_config(pci_dev, address, val, len); 158020302e71SMichael S. Tsirkin 158120302e71SMichael S. Tsirkin if (range_covers_byte(address, len, PCI_COMMAND) && 158220302e71SMichael S. Tsirkin (pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { 158320302e71SMichael S. Tsirkin qemu_flush_queued_packets(qemu_get_queue(s->nic)); 158420302e71SMichael S. Tsirkin } 158520302e71SMichael S. Tsirkin } 158620302e71SMichael S. Tsirkin 15879af21dbeSMarkus Armbruster static void pci_e1000_realize(PCIDevice *pci_dev, Error **errp) 15887c23b892Sbalrog { 1589567a3c9eSPeter Crosthwaite DeviceState *dev = DEVICE(pci_dev); 1590567a3c9eSPeter Crosthwaite E1000State *d = E1000(pci_dev); 15917c23b892Sbalrog uint8_t *pci_conf; 1592fbdaa002SGerd Hoffmann uint8_t *macaddr; 1593aff427a1SChris Wright 159420302e71SMichael S. Tsirkin pci_dev->config_write = e1000_write_config; 159520302e71SMichael S. Tsirkin 1596b08340d5SAndreas Färber pci_conf = pci_dev->config; 15977c23b892Sbalrog 1598a9cbacb0SMichael S. Tsirkin /* TODO: RST# value should be 0, PCI spec 6.2.4 */ 1599a9cbacb0SMichael S. Tsirkin pci_conf[PCI_CACHE_LINE_SIZE] = 0x10; 16007c23b892Sbalrog 1601817e0b6fSMichael S. Tsirkin pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */ 16027c23b892Sbalrog 1603ad00a9b9SAvi Kivity e1000_mmio_setup(d); 16047c23b892Sbalrog 1605b08340d5SAndreas Färber pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio); 16067c23b892Sbalrog 1607b08340d5SAndreas Färber pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->io); 16087c23b892Sbalrog 1609fbdaa002SGerd Hoffmann qemu_macaddr_default_if_unset(&d->conf.macaddr); 1610fbdaa002SGerd Hoffmann macaddr = d->conf.macaddr.a; 1611093454e2SDmitry Fleytman 1612093454e2SDmitry Fleytman e1000x_core_prepare_eeprom(d->eeprom_data, 1613093454e2SDmitry Fleytman e1000_eeprom_template, 1614093454e2SDmitry Fleytman sizeof(e1000_eeprom_template), 1615093454e2SDmitry Fleytman PCI_DEVICE_GET_CLASS(pci_dev)->device_id, 1616093454e2SDmitry Fleytman macaddr); 16177c23b892Sbalrog 1618a03e2aecSMark McLoughlin d->nic = qemu_new_nic(&net_e1000_info, &d->conf, 1619567a3c9eSPeter Crosthwaite object_get_typename(OBJECT(d)), dev->id, d); 16207c23b892Sbalrog 1621b356f76dSJason Wang qemu_format_nic_info_str(qemu_get_queue(d->nic), macaddr); 16221ca4d09aSGleb Natapov 1623bc72ad67SAlex Bligh d->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, e1000_autoneg_timer, d); 1624e9845f09SVincenzo Maffione d->mit_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000_mit_timer, d); 16257c23b892Sbalrog } 16269d07d757SPaul Brook 1627fbdaa002SGerd Hoffmann static void qdev_e1000_reset(DeviceState *dev) 1628fbdaa002SGerd Hoffmann { 1629567a3c9eSPeter Crosthwaite E1000State *d = E1000(dev); 1630fbdaa002SGerd Hoffmann e1000_reset(d); 1631fbdaa002SGerd Hoffmann } 1632fbdaa002SGerd Hoffmann 163340021f08SAnthony Liguori static Property e1000_properties[] = { 1634fbdaa002SGerd Hoffmann DEFINE_NIC_PROPERTIES(E1000State, conf), 16352af234e6SMichael S. Tsirkin DEFINE_PROP_BIT("autonegotiation", E1000State, 16362af234e6SMichael S. Tsirkin compat_flags, E1000_FLAG_AUTONEG_BIT, true), 1637e9845f09SVincenzo Maffione DEFINE_PROP_BIT("mitigation", E1000State, 1638e9845f09SVincenzo Maffione compat_flags, E1000_FLAG_MIT_BIT, true), 1639ba63ec85SLeonid Bloch DEFINE_PROP_BIT("extra_mac_registers", E1000State, 1640ba63ec85SLeonid Bloch compat_flags, E1000_FLAG_MAC_BIT, true), 1641fbdaa002SGerd Hoffmann DEFINE_PROP_END_OF_LIST(), 164240021f08SAnthony Liguori }; 164340021f08SAnthony Liguori 16448597f2e1SGabriel L. Somlo typedef struct E1000Info { 16458597f2e1SGabriel L. Somlo const char *name; 16468597f2e1SGabriel L. Somlo uint16_t device_id; 16478597f2e1SGabriel L. Somlo uint8_t revision; 16488597f2e1SGabriel L. Somlo uint16_t phy_id2; 16498597f2e1SGabriel L. Somlo } E1000Info; 16508597f2e1SGabriel L. Somlo 165140021f08SAnthony Liguori static void e1000_class_init(ObjectClass *klass, void *data) 165240021f08SAnthony Liguori { 165339bffca2SAnthony Liguori DeviceClass *dc = DEVICE_CLASS(klass); 165440021f08SAnthony Liguori PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 16558597f2e1SGabriel L. Somlo E1000BaseClass *e = E1000_DEVICE_CLASS(klass); 16568597f2e1SGabriel L. Somlo const E1000Info *info = data; 165740021f08SAnthony Liguori 16589af21dbeSMarkus Armbruster k->realize = pci_e1000_realize; 165940021f08SAnthony Liguori k->exit = pci_e1000_uninit; 1660c45e5b5bSGerd Hoffmann k->romfile = "efi-e1000.rom"; 166140021f08SAnthony Liguori k->vendor_id = PCI_VENDOR_ID_INTEL; 16628597f2e1SGabriel L. Somlo k->device_id = info->device_id; 16638597f2e1SGabriel L. Somlo k->revision = info->revision; 16648597f2e1SGabriel L. Somlo e->phy_id2 = info->phy_id2; 166540021f08SAnthony Liguori k->class_id = PCI_CLASS_NETWORK_ETHERNET; 1666125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 166739bffca2SAnthony Liguori dc->desc = "Intel Gigabit Ethernet"; 166839bffca2SAnthony Liguori dc->reset = qdev_e1000_reset; 166939bffca2SAnthony Liguori dc->vmsd = &vmstate_e1000; 167039bffca2SAnthony Liguori dc->props = e1000_properties; 1671fbdaa002SGerd Hoffmann } 167240021f08SAnthony Liguori 16735df3bf62SGonglei static void e1000_instance_init(Object *obj) 16745df3bf62SGonglei { 16755df3bf62SGonglei E1000State *n = E1000(obj); 16765df3bf62SGonglei device_add_bootindex_property(obj, &n->conf.bootindex, 16775df3bf62SGonglei "bootindex", "/ethernet-phy@0", 16785df3bf62SGonglei DEVICE(n), NULL); 16795df3bf62SGonglei } 16805df3bf62SGonglei 16818597f2e1SGabriel L. Somlo static const TypeInfo e1000_base_info = { 16828597f2e1SGabriel L. Somlo .name = TYPE_E1000_BASE, 168339bffca2SAnthony Liguori .parent = TYPE_PCI_DEVICE, 168439bffca2SAnthony Liguori .instance_size = sizeof(E1000State), 16855df3bf62SGonglei .instance_init = e1000_instance_init, 16868597f2e1SGabriel L. Somlo .class_size = sizeof(E1000BaseClass), 16878597f2e1SGabriel L. Somlo .abstract = true, 16888597f2e1SGabriel L. Somlo }; 16898597f2e1SGabriel L. Somlo 16908597f2e1SGabriel L. Somlo static const E1000Info e1000_devices[] = { 16918597f2e1SGabriel L. Somlo { 169283044020SJason Wang .name = "e1000", 16938597f2e1SGabriel L. Somlo .device_id = E1000_DEV_ID_82540EM, 16948597f2e1SGabriel L. Somlo .revision = 0x03, 16958597f2e1SGabriel L. Somlo .phy_id2 = E1000_PHY_ID2_8254xx_DEFAULT, 16968597f2e1SGabriel L. Somlo }, 16978597f2e1SGabriel L. Somlo { 16988597f2e1SGabriel L. Somlo .name = "e1000-82544gc", 16998597f2e1SGabriel L. Somlo .device_id = E1000_DEV_ID_82544GC_COPPER, 17008597f2e1SGabriel L. Somlo .revision = 0x03, 17018597f2e1SGabriel L. Somlo .phy_id2 = E1000_PHY_ID2_82544x, 17028597f2e1SGabriel L. Somlo }, 17038597f2e1SGabriel L. Somlo { 17048597f2e1SGabriel L. Somlo .name = "e1000-82545em", 17058597f2e1SGabriel L. Somlo .device_id = E1000_DEV_ID_82545EM_COPPER, 17068597f2e1SGabriel L. Somlo .revision = 0x03, 17078597f2e1SGabriel L. Somlo .phy_id2 = E1000_PHY_ID2_8254xx_DEFAULT, 17088597f2e1SGabriel L. Somlo }, 17098597f2e1SGabriel L. Somlo }; 17108597f2e1SGabriel L. Somlo 171183f7d43aSAndreas Färber static void e1000_register_types(void) 17129d07d757SPaul Brook { 17138597f2e1SGabriel L. Somlo int i; 17148597f2e1SGabriel L. Somlo 17158597f2e1SGabriel L. Somlo type_register_static(&e1000_base_info); 17168597f2e1SGabriel L. Somlo for (i = 0; i < ARRAY_SIZE(e1000_devices); i++) { 17178597f2e1SGabriel L. Somlo const E1000Info *info = &e1000_devices[i]; 17188597f2e1SGabriel L. Somlo TypeInfo type_info = {}; 17198597f2e1SGabriel L. Somlo 17208597f2e1SGabriel L. Somlo type_info.name = info->name; 17218597f2e1SGabriel L. Somlo type_info.parent = TYPE_E1000_BASE; 17228597f2e1SGabriel L. Somlo type_info.class_data = (void *)info; 17238597f2e1SGabriel L. Somlo type_info.class_init = e1000_class_init; 17245df3bf62SGonglei type_info.instance_init = e1000_instance_init; 17258597f2e1SGabriel L. Somlo 17268597f2e1SGabriel L. Somlo type_register(&type_info); 17278597f2e1SGabriel L. Somlo } 17289d07d757SPaul Brook } 17299d07d757SPaul Brook 173083f7d43aSAndreas Färber type_init(e1000_register_types) 1731