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 2883c9f4caSPaolo Bonzini #include "hw/hw.h" 2983c9f4caSPaolo Bonzini #include "hw/pci/pci.h" 301422e32dSPaolo Bonzini #include "net/net.h" 317200ac3cSMark McLoughlin #include "net/checksum.h" 3283c9f4caSPaolo Bonzini #include "hw/loader.h" 339c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 349c17d615SPaolo Bonzini #include "sysemu/dma.h" 3597410ddeSVincenzo Maffione #include "qemu/iov.h" 367c23b892Sbalrog 3747b43a1fSPaolo Bonzini #include "e1000_regs.h" 387c23b892Sbalrog 3927124888SJes Sorensen #define E1000_DEBUG 407c23b892Sbalrog 4127124888SJes Sorensen #ifdef E1000_DEBUG 427c23b892Sbalrog enum { 437c23b892Sbalrog DEBUG_GENERAL, DEBUG_IO, DEBUG_MMIO, DEBUG_INTERRUPT, 447c23b892Sbalrog DEBUG_RX, DEBUG_TX, DEBUG_MDIC, DEBUG_EEPROM, 457c23b892Sbalrog DEBUG_UNKNOWN, DEBUG_TXSUM, DEBUG_TXERR, DEBUG_RXERR, 46f9c1cdf4SJason Wang DEBUG_RXFILTER, DEBUG_PHY, DEBUG_NOTYET, 477c23b892Sbalrog }; 487c23b892Sbalrog #define DBGBIT(x) (1<<DEBUG_##x) 497c23b892Sbalrog static int debugflags = DBGBIT(TXERR) | DBGBIT(GENERAL); 507c23b892Sbalrog 516c7f4b47SBlue Swirl #define DBGOUT(what, fmt, ...) do { \ 527c23b892Sbalrog if (debugflags & DBGBIT(what)) \ 536c7f4b47SBlue Swirl fprintf(stderr, "e1000: " fmt, ## __VA_ARGS__); \ 547c23b892Sbalrog } while (0) 557c23b892Sbalrog #else 566c7f4b47SBlue Swirl #define DBGOUT(what, fmt, ...) do {} while (0) 577c23b892Sbalrog #endif 587c23b892Sbalrog 597c23b892Sbalrog #define IOPORT_SIZE 0x40 60e94bbefeSaurel32 #define PNPMMIO_SIZE 0x20000 6178aeb23eSStefan Hajnoczi #define MIN_BUF_SIZE 60 /* Min. octets in an ethernet frame sans FCS */ 627c23b892Sbalrog 63b0d9ffcdSMichael Contreras /* this is the size past which hardware will drop packets when setting LPE=0 */ 64b0d9ffcdSMichael Contreras #define MAXIMUM_ETHERNET_VLAN_SIZE 1522 652c0331f4SMichael Contreras /* this is the size past which hardware will drop packets when setting LPE=1 */ 662c0331f4SMichael Contreras #define MAXIMUM_ETHERNET_LPE_SIZE 16384 67b0d9ffcdSMichael Contreras 6897410ddeSVincenzo Maffione #define MAXIMUM_ETHERNET_HDR_LEN (14+4) 6997410ddeSVincenzo Maffione 707c23b892Sbalrog /* 717c23b892Sbalrog * HW models: 72*8597f2e1SGabriel L. Somlo * E1000_DEV_ID_82540EM works with Windows, Linux, and OS X <= 10.8 737c23b892Sbalrog * E1000_DEV_ID_82573L OK with windoze and Linux 2.6.22, 747c23b892Sbalrog * appears to perform better than 82540EM, but breaks with Linux 2.6.18 757c23b892Sbalrog * E1000_DEV_ID_82544GC_COPPER appears to work; not well tested 76*8597f2e1SGabriel L. Somlo * E1000_DEV_ID_82545EM_COPPER works with Linux and OS X >= 10.6 777c23b892Sbalrog * Others never tested 787c23b892Sbalrog */ 797c23b892Sbalrog 807c23b892Sbalrog typedef struct E1000State_st { 81b08340d5SAndreas Färber /*< private >*/ 82b08340d5SAndreas Färber PCIDevice parent_obj; 83b08340d5SAndreas Färber /*< public >*/ 84b08340d5SAndreas Färber 85a03e2aecSMark McLoughlin NICState *nic; 86fbdaa002SGerd Hoffmann NICConf conf; 87ad00a9b9SAvi Kivity MemoryRegion mmio; 88ad00a9b9SAvi Kivity MemoryRegion io; 897c23b892Sbalrog 907c23b892Sbalrog uint32_t mac_reg[0x8000]; 917c23b892Sbalrog uint16_t phy_reg[0x20]; 927c23b892Sbalrog uint16_t eeprom_data[64]; 937c23b892Sbalrog 947c23b892Sbalrog uint32_t rxbuf_size; 957c23b892Sbalrog uint32_t rxbuf_min_shift; 967c23b892Sbalrog struct e1000_tx { 977c23b892Sbalrog unsigned char header[256]; 988f2e8d1fSaliguori unsigned char vlan_header[4]; 99b10fec9bSStefan Weil /* Fields vlan and data must not be reordered or separated. */ 1008f2e8d1fSaliguori unsigned char vlan[4]; 1017c23b892Sbalrog unsigned char data[0x10000]; 1027c23b892Sbalrog uint16_t size; 1037c23b892Sbalrog unsigned char sum_needed; 1048f2e8d1fSaliguori unsigned char vlan_needed; 1057c23b892Sbalrog uint8_t ipcss; 1067c23b892Sbalrog uint8_t ipcso; 1077c23b892Sbalrog uint16_t ipcse; 1087c23b892Sbalrog uint8_t tucss; 1097c23b892Sbalrog uint8_t tucso; 1107c23b892Sbalrog uint16_t tucse; 1117c23b892Sbalrog uint8_t hdr_len; 1127c23b892Sbalrog uint16_t mss; 1137c23b892Sbalrog uint32_t paylen; 1147c23b892Sbalrog uint16_t tso_frames; 1157c23b892Sbalrog char tse; 116b6c4f71fSblueswir1 int8_t ip; 117b6c4f71fSblueswir1 int8_t tcp; 1181b0009dbSbalrog char cptse; // current packet tse bit 1197c23b892Sbalrog } tx; 1207c23b892Sbalrog 1217c23b892Sbalrog struct { 1227c23b892Sbalrog uint32_t val_in; // shifted in from guest driver 1237c23b892Sbalrog uint16_t bitnum_in; 1247c23b892Sbalrog uint16_t bitnum_out; 1257c23b892Sbalrog uint16_t reading; 1267c23b892Sbalrog uint32_t old_eecd; 1277c23b892Sbalrog } eecd_state; 128b9d03e35SJason Wang 129b9d03e35SJason Wang QEMUTimer *autoneg_timer; 1302af234e6SMichael S. Tsirkin 131e9845f09SVincenzo Maffione QEMUTimer *mit_timer; /* Mitigation timer. */ 132e9845f09SVincenzo Maffione bool mit_timer_on; /* Mitigation timer is running. */ 133e9845f09SVincenzo Maffione bool mit_irq_level; /* Tracks interrupt pin level. */ 134e9845f09SVincenzo Maffione uint32_t mit_ide; /* Tracks E1000_TXD_CMD_IDE bit. */ 135e9845f09SVincenzo Maffione 1362af234e6SMichael S. Tsirkin /* Compatibility flags for migration to/from qemu 1.3.0 and older */ 1372af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG_BIT 0 138e9845f09SVincenzo Maffione #define E1000_FLAG_MIT_BIT 1 1392af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG (1 << E1000_FLAG_AUTONEG_BIT) 140e9845f09SVincenzo Maffione #define E1000_FLAG_MIT (1 << E1000_FLAG_MIT_BIT) 1412af234e6SMichael S. Tsirkin uint32_t compat_flags; 1427c23b892Sbalrog } E1000State; 1437c23b892Sbalrog 144*8597f2e1SGabriel L. Somlo typedef struct E1000BaseClass { 145*8597f2e1SGabriel L. Somlo PCIDeviceClass parent_class; 146*8597f2e1SGabriel L. Somlo uint16_t phy_id2; 147*8597f2e1SGabriel L. Somlo bool is_8257xx; 148*8597f2e1SGabriel L. Somlo } E1000BaseClass; 149*8597f2e1SGabriel L. Somlo 150*8597f2e1SGabriel L. Somlo #define TYPE_E1000_BASE "e1000-base" 151567a3c9eSPeter Crosthwaite 152567a3c9eSPeter Crosthwaite #define E1000(obj) \ 153*8597f2e1SGabriel L. Somlo OBJECT_CHECK(E1000State, (obj), TYPE_E1000_BASE) 154*8597f2e1SGabriel L. Somlo 155*8597f2e1SGabriel L. Somlo #define E1000_DEVICE_CLASS(klass) \ 156*8597f2e1SGabriel L. Somlo OBJECT_CLASS_CHECK(E1000BaseClass, (klass), TYPE_E1000_BASE) 157*8597f2e1SGabriel L. Somlo #define E1000_DEVICE_GET_CLASS(obj) \ 158*8597f2e1SGabriel L. Somlo OBJECT_GET_CLASS(E1000BaseClass, (obj), TYPE_E1000_BASE) 159567a3c9eSPeter Crosthwaite 1607c23b892Sbalrog #define defreg(x) x = (E1000_##x>>2) 1617c23b892Sbalrog enum { 1627c23b892Sbalrog defreg(CTRL), defreg(EECD), defreg(EERD), defreg(GPRC), 1637c23b892Sbalrog defreg(GPTC), defreg(ICR), defreg(ICS), defreg(IMC), 1647c23b892Sbalrog defreg(IMS), defreg(LEDCTL), defreg(MANC), defreg(MDIC), 1657c23b892Sbalrog defreg(MPC), defreg(PBA), defreg(RCTL), defreg(RDBAH), 1667c23b892Sbalrog defreg(RDBAL), defreg(RDH), defreg(RDLEN), defreg(RDT), 1677c23b892Sbalrog defreg(STATUS), defreg(SWSM), defreg(TCTL), defreg(TDBAH), 1687c23b892Sbalrog defreg(TDBAL), defreg(TDH), defreg(TDLEN), defreg(TDT), 1697c23b892Sbalrog defreg(TORH), defreg(TORL), defreg(TOTH), defreg(TOTL), 1707c23b892Sbalrog defreg(TPR), defreg(TPT), defreg(TXDCTL), defreg(WUFC), 1718f2e8d1fSaliguori defreg(RA), defreg(MTA), defreg(CRCERRS),defreg(VFTA), 172e9845f09SVincenzo Maffione defreg(VET), defreg(RDTR), defreg(RADV), defreg(TADV), 173e9845f09SVincenzo Maffione defreg(ITR), 1747c23b892Sbalrog }; 1757c23b892Sbalrog 17671aadd3cSJason Wang static void 17771aadd3cSJason Wang e1000_link_down(E1000State *s) 17871aadd3cSJason Wang { 17971aadd3cSJason Wang s->mac_reg[STATUS] &= ~E1000_STATUS_LU; 18071aadd3cSJason Wang s->phy_reg[PHY_STATUS] &= ~MII_SR_LINK_STATUS; 18171aadd3cSJason Wang } 18271aadd3cSJason Wang 18371aadd3cSJason Wang static void 18471aadd3cSJason Wang e1000_link_up(E1000State *s) 18571aadd3cSJason Wang { 18671aadd3cSJason Wang s->mac_reg[STATUS] |= E1000_STATUS_LU; 18771aadd3cSJason Wang s->phy_reg[PHY_STATUS] |= MII_SR_LINK_STATUS; 18871aadd3cSJason Wang } 18971aadd3cSJason Wang 190b9d03e35SJason Wang static void 191b9d03e35SJason Wang set_phy_ctrl(E1000State *s, int index, uint16_t val) 192b9d03e35SJason Wang { 1932af234e6SMichael S. Tsirkin /* 1942af234e6SMichael S. Tsirkin * QEMU 1.3 does not support link auto-negotiation emulation, so if we 1952af234e6SMichael S. Tsirkin * migrate during auto negotiation, after migration the link will be 1962af234e6SMichael S. Tsirkin * down. 1972af234e6SMichael S. Tsirkin */ 1982af234e6SMichael S. Tsirkin if (!(s->compat_flags & E1000_FLAG_AUTONEG)) { 1992af234e6SMichael S. Tsirkin return; 2002af234e6SMichael S. Tsirkin } 201b9d03e35SJason Wang if ((val & MII_CR_AUTO_NEG_EN) && (val & MII_CR_RESTART_AUTO_NEG)) { 202b9d03e35SJason Wang e1000_link_down(s); 203b9d03e35SJason Wang s->phy_reg[PHY_STATUS] &= ~MII_SR_AUTONEG_COMPLETE; 204b9d03e35SJason Wang DBGOUT(PHY, "Start link auto negotiation\n"); 205bc72ad67SAlex Bligh timer_mod(s->autoneg_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500); 206b9d03e35SJason Wang } 207b9d03e35SJason Wang } 208b9d03e35SJason Wang 209b9d03e35SJason Wang static void 210b9d03e35SJason Wang e1000_autoneg_timer(void *opaque) 211b9d03e35SJason Wang { 212b9d03e35SJason Wang E1000State *s = opaque; 213ddcb73b7SMichael S. Tsirkin if (!qemu_get_queue(s->nic)->link_down) { 214b9d03e35SJason Wang e1000_link_up(s); 215ddcb73b7SMichael S. Tsirkin } 216b9d03e35SJason Wang s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE; 217b9d03e35SJason Wang DBGOUT(PHY, "Auto negotiation is completed\n"); 218b9d03e35SJason Wang } 219b9d03e35SJason Wang 220b9d03e35SJason Wang static void (*phyreg_writeops[])(E1000State *, int, uint16_t) = { 221b9d03e35SJason Wang [PHY_CTRL] = set_phy_ctrl, 222b9d03e35SJason Wang }; 223b9d03e35SJason Wang 224b9d03e35SJason Wang enum { NPHYWRITEOPS = ARRAY_SIZE(phyreg_writeops) }; 225b9d03e35SJason Wang 2267c23b892Sbalrog enum { PHY_R = 1, PHY_W = 2, PHY_RW = PHY_R | PHY_W }; 22788b4e9dbSblueswir1 static const char phy_regcap[0x20] = { 2287c23b892Sbalrog [PHY_STATUS] = PHY_R, [M88E1000_EXT_PHY_SPEC_CTRL] = PHY_RW, 2297c23b892Sbalrog [PHY_ID1] = PHY_R, [M88E1000_PHY_SPEC_CTRL] = PHY_RW, 2307c23b892Sbalrog [PHY_CTRL] = PHY_RW, [PHY_1000T_CTRL] = PHY_RW, 2317c23b892Sbalrog [PHY_LP_ABILITY] = PHY_R, [PHY_1000T_STATUS] = PHY_R, 2327c23b892Sbalrog [PHY_AUTONEG_ADV] = PHY_RW, [M88E1000_RX_ERR_CNTR] = PHY_R, 233700f6e2cSaurel32 [PHY_ID2] = PHY_R, [M88E1000_PHY_SPEC_STATUS] = PHY_R 2347c23b892Sbalrog }; 2357c23b892Sbalrog 236*8597f2e1SGabriel L. Somlo /* PHY_ID2 documented in 8254x_GBe_SDM.pdf, pp. 250 */ 237814cd3acSMichael S. Tsirkin static const uint16_t phy_reg_init[] = { 238b9d03e35SJason Wang [PHY_CTRL] = 0x1140, 239b9d03e35SJason Wang [PHY_STATUS] = 0x794d, /* link initially up with not completed autoneg */ 240*8597f2e1SGabriel L. Somlo [PHY_ID1] = 0x141, /* [PHY_ID2] configured per DevId, from e1000_reset() */ 241814cd3acSMichael S. Tsirkin [PHY_1000T_CTRL] = 0x0e00, [M88E1000_PHY_SPEC_CTRL] = 0x360, 242814cd3acSMichael S. Tsirkin [M88E1000_EXT_PHY_SPEC_CTRL] = 0x0d60, [PHY_AUTONEG_ADV] = 0xde1, 243814cd3acSMichael S. Tsirkin [PHY_LP_ABILITY] = 0x1e0, [PHY_1000T_STATUS] = 0x3c00, 244814cd3acSMichael S. Tsirkin [M88E1000_PHY_SPEC_STATUS] = 0xac00, 245814cd3acSMichael S. Tsirkin }; 246814cd3acSMichael S. Tsirkin 247814cd3acSMichael S. Tsirkin static const uint32_t mac_reg_init[] = { 248814cd3acSMichael S. Tsirkin [PBA] = 0x00100030, 249814cd3acSMichael S. Tsirkin [LEDCTL] = 0x602, 250814cd3acSMichael S. Tsirkin [CTRL] = E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN0 | 251814cd3acSMichael S. Tsirkin E1000_CTRL_SPD_1000 | E1000_CTRL_SLU, 252814cd3acSMichael S. Tsirkin [STATUS] = 0x80000000 | E1000_STATUS_GIO_MASTER_ENABLE | 253814cd3acSMichael S. Tsirkin E1000_STATUS_ASDV | E1000_STATUS_MTXCKOK | 254814cd3acSMichael S. Tsirkin E1000_STATUS_SPEED_1000 | E1000_STATUS_FD | 255814cd3acSMichael S. Tsirkin E1000_STATUS_LU, 256814cd3acSMichael S. Tsirkin [MANC] = E1000_MANC_EN_MNG2HOST | E1000_MANC_RCV_TCO_EN | 257814cd3acSMichael S. Tsirkin E1000_MANC_ARP_EN | E1000_MANC_0298_EN | 258814cd3acSMichael S. Tsirkin E1000_MANC_RMCP_EN, 259814cd3acSMichael S. Tsirkin }; 260814cd3acSMichael S. Tsirkin 261e9845f09SVincenzo Maffione /* Helper function, *curr == 0 means the value is not set */ 262e9845f09SVincenzo Maffione static inline void 263e9845f09SVincenzo Maffione mit_update_delay(uint32_t *curr, uint32_t value) 264e9845f09SVincenzo Maffione { 265e9845f09SVincenzo Maffione if (value && (*curr == 0 || value < *curr)) { 266e9845f09SVincenzo Maffione *curr = value; 267e9845f09SVincenzo Maffione } 268e9845f09SVincenzo Maffione } 269e9845f09SVincenzo Maffione 2707c23b892Sbalrog static void 2717c23b892Sbalrog set_interrupt_cause(E1000State *s, int index, uint32_t val) 2727c23b892Sbalrog { 273b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 274*8597f2e1SGabriel L. Somlo E1000BaseClass *edc = E1000_DEVICE_GET_CLASS(d); 275e9845f09SVincenzo Maffione uint32_t pending_ints; 276e9845f09SVincenzo Maffione uint32_t mit_delay; 277b08340d5SAndreas Färber 278*8597f2e1SGabriel L. Somlo if (val && edc->is_8257xx) { 279*8597f2e1SGabriel L. Somlo /* hack only for 8257xx models */ 2807c23b892Sbalrog val |= E1000_ICR_INT_ASSERTED; 281f1219091SJason Wang } 282*8597f2e1SGabriel L. Somlo 2837c23b892Sbalrog s->mac_reg[ICR] = val; 284a52a8841SMichael S. Tsirkin 285a52a8841SMichael S. Tsirkin /* 286a52a8841SMichael S. Tsirkin * Make sure ICR and ICS registers have the same value. 287a52a8841SMichael S. Tsirkin * The spec says that the ICS register is write-only. However in practice, 288a52a8841SMichael S. Tsirkin * on real hardware ICS is readable, and for reads it has the same value as 289a52a8841SMichael S. Tsirkin * ICR (except that ICS does not have the clear on read behaviour of ICR). 290a52a8841SMichael S. Tsirkin * 291a52a8841SMichael S. Tsirkin * The VxWorks PRO/1000 driver uses this behaviour. 292a52a8841SMichael S. Tsirkin */ 293b1332393SBill Paul s->mac_reg[ICS] = val; 294a52a8841SMichael S. Tsirkin 295e9845f09SVincenzo Maffione pending_ints = (s->mac_reg[IMS] & s->mac_reg[ICR]); 296e9845f09SVincenzo Maffione if (!s->mit_irq_level && pending_ints) { 297e9845f09SVincenzo Maffione /* 298e9845f09SVincenzo Maffione * Here we detect a potential raising edge. We postpone raising the 299e9845f09SVincenzo Maffione * interrupt line if we are inside the mitigation delay window 300e9845f09SVincenzo Maffione * (s->mit_timer_on == 1). 301e9845f09SVincenzo Maffione * We provide a partial implementation of interrupt mitigation, 302e9845f09SVincenzo Maffione * emulating only RADV, TADV and ITR (lower 16 bits, 1024ns units for 303e9845f09SVincenzo Maffione * RADV and TADV, 256ns units for ITR). RDTR is only used to enable 304e9845f09SVincenzo Maffione * RADV; relative timers based on TIDV and RDTR are not implemented. 305e9845f09SVincenzo Maffione */ 306e9845f09SVincenzo Maffione if (s->mit_timer_on) { 307e9845f09SVincenzo Maffione return; 308e9845f09SVincenzo Maffione } 309e9845f09SVincenzo Maffione if (s->compat_flags & E1000_FLAG_MIT) { 310e9845f09SVincenzo Maffione /* Compute the next mitigation delay according to pending 311e9845f09SVincenzo Maffione * interrupts and the current values of RADV (provided 312e9845f09SVincenzo Maffione * RDTR!=0), TADV and ITR. 313e9845f09SVincenzo Maffione * Then rearm the timer. 314e9845f09SVincenzo Maffione */ 315e9845f09SVincenzo Maffione mit_delay = 0; 316e9845f09SVincenzo Maffione if (s->mit_ide && 317e9845f09SVincenzo Maffione (pending_ints & (E1000_ICR_TXQE | E1000_ICR_TXDW))) { 318e9845f09SVincenzo Maffione mit_update_delay(&mit_delay, s->mac_reg[TADV] * 4); 319e9845f09SVincenzo Maffione } 320e9845f09SVincenzo Maffione if (s->mac_reg[RDTR] && (pending_ints & E1000_ICS_RXT0)) { 321e9845f09SVincenzo Maffione mit_update_delay(&mit_delay, s->mac_reg[RADV] * 4); 322e9845f09SVincenzo Maffione } 323e9845f09SVincenzo Maffione mit_update_delay(&mit_delay, s->mac_reg[ITR]); 324e9845f09SVincenzo Maffione 325e9845f09SVincenzo Maffione if (mit_delay) { 326e9845f09SVincenzo Maffione s->mit_timer_on = 1; 327e9845f09SVincenzo Maffione timer_mod(s->mit_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 328e9845f09SVincenzo Maffione mit_delay * 256); 329e9845f09SVincenzo Maffione } 330e9845f09SVincenzo Maffione s->mit_ide = 0; 331e9845f09SVincenzo Maffione } 332e9845f09SVincenzo Maffione } 333e9845f09SVincenzo Maffione 334e9845f09SVincenzo Maffione s->mit_irq_level = (pending_ints != 0); 3359e64f8a3SMarcel Apfelbaum pci_set_irq(d, s->mit_irq_level); 336e9845f09SVincenzo Maffione } 337e9845f09SVincenzo Maffione 338e9845f09SVincenzo Maffione static void 339e9845f09SVincenzo Maffione e1000_mit_timer(void *opaque) 340e9845f09SVincenzo Maffione { 341e9845f09SVincenzo Maffione E1000State *s = opaque; 342e9845f09SVincenzo Maffione 343e9845f09SVincenzo Maffione s->mit_timer_on = 0; 344e9845f09SVincenzo Maffione /* Call set_interrupt_cause to update the irq level (if necessary). */ 345e9845f09SVincenzo Maffione set_interrupt_cause(s, 0, s->mac_reg[ICR]); 3467c23b892Sbalrog } 3477c23b892Sbalrog 3487c23b892Sbalrog static void 3497c23b892Sbalrog set_ics(E1000State *s, int index, uint32_t val) 3507c23b892Sbalrog { 3517c23b892Sbalrog DBGOUT(INTERRUPT, "set_ics %x, ICR %x, IMR %x\n", val, s->mac_reg[ICR], 3527c23b892Sbalrog s->mac_reg[IMS]); 3537c23b892Sbalrog set_interrupt_cause(s, 0, val | s->mac_reg[ICR]); 3547c23b892Sbalrog } 3557c23b892Sbalrog 3567c23b892Sbalrog static int 3577c23b892Sbalrog rxbufsize(uint32_t v) 3587c23b892Sbalrog { 3597c23b892Sbalrog v &= E1000_RCTL_BSEX | E1000_RCTL_SZ_16384 | E1000_RCTL_SZ_8192 | 3607c23b892Sbalrog E1000_RCTL_SZ_4096 | E1000_RCTL_SZ_2048 | E1000_RCTL_SZ_1024 | 3617c23b892Sbalrog E1000_RCTL_SZ_512 | E1000_RCTL_SZ_256; 3627c23b892Sbalrog switch (v) { 3637c23b892Sbalrog case E1000_RCTL_BSEX | E1000_RCTL_SZ_16384: 3647c23b892Sbalrog return 16384; 3657c23b892Sbalrog case E1000_RCTL_BSEX | E1000_RCTL_SZ_8192: 3667c23b892Sbalrog return 8192; 3677c23b892Sbalrog case E1000_RCTL_BSEX | E1000_RCTL_SZ_4096: 3687c23b892Sbalrog return 4096; 3697c23b892Sbalrog case E1000_RCTL_SZ_1024: 3707c23b892Sbalrog return 1024; 3717c23b892Sbalrog case E1000_RCTL_SZ_512: 3727c23b892Sbalrog return 512; 3737c23b892Sbalrog case E1000_RCTL_SZ_256: 3747c23b892Sbalrog return 256; 3757c23b892Sbalrog } 3767c23b892Sbalrog return 2048; 3777c23b892Sbalrog } 3787c23b892Sbalrog 379814cd3acSMichael S. Tsirkin static void e1000_reset(void *opaque) 380814cd3acSMichael S. Tsirkin { 381814cd3acSMichael S. Tsirkin E1000State *d = opaque; 382*8597f2e1SGabriel L. Somlo E1000BaseClass *edc = E1000_DEVICE_GET_CLASS(d); 383372254c6SGabriel L. Somlo uint8_t *macaddr = d->conf.macaddr.a; 384372254c6SGabriel L. Somlo int i; 385814cd3acSMichael S. Tsirkin 386bc72ad67SAlex Bligh timer_del(d->autoneg_timer); 387e9845f09SVincenzo Maffione timer_del(d->mit_timer); 388e9845f09SVincenzo Maffione d->mit_timer_on = 0; 389e9845f09SVincenzo Maffione d->mit_irq_level = 0; 390e9845f09SVincenzo Maffione d->mit_ide = 0; 391814cd3acSMichael S. Tsirkin memset(d->phy_reg, 0, sizeof d->phy_reg); 392814cd3acSMichael S. Tsirkin memmove(d->phy_reg, phy_reg_init, sizeof phy_reg_init); 393*8597f2e1SGabriel L. Somlo d->phy_reg[PHY_ID2] = edc->phy_id2; 394814cd3acSMichael S. Tsirkin memset(d->mac_reg, 0, sizeof d->mac_reg); 395814cd3acSMichael S. Tsirkin memmove(d->mac_reg, mac_reg_init, sizeof mac_reg_init); 396814cd3acSMichael S. Tsirkin d->rxbuf_min_shift = 1; 397814cd3acSMichael S. Tsirkin memset(&d->tx, 0, sizeof d->tx); 398814cd3acSMichael S. Tsirkin 399b356f76dSJason Wang if (qemu_get_queue(d->nic)->link_down) { 40071aadd3cSJason Wang e1000_link_down(d); 401814cd3acSMichael S. Tsirkin } 402372254c6SGabriel L. Somlo 403372254c6SGabriel L. Somlo /* Some guests expect pre-initialized RAH/RAL (AddrValid flag + MACaddr) */ 404372254c6SGabriel L. Somlo d->mac_reg[RA] = 0; 405372254c6SGabriel L. Somlo d->mac_reg[RA + 1] = E1000_RAH_AV; 406372254c6SGabriel L. Somlo for (i = 0; i < 4; i++) { 407372254c6SGabriel L. Somlo d->mac_reg[RA] |= macaddr[i] << (8 * i); 408372254c6SGabriel L. Somlo d->mac_reg[RA + 1] |= (i < 2) ? macaddr[i + 4] << (8 * i) : 0; 409372254c6SGabriel L. Somlo } 410655d3b63SAmos Kong qemu_format_nic_info_str(qemu_get_queue(d->nic), macaddr); 411814cd3acSMichael S. Tsirkin } 412814cd3acSMichael S. Tsirkin 4137c23b892Sbalrog static void 414cab3c825SKevin Wolf set_ctrl(E1000State *s, int index, uint32_t val) 415cab3c825SKevin Wolf { 416cab3c825SKevin Wolf /* RST is self clearing */ 417cab3c825SKevin Wolf s->mac_reg[CTRL] = val & ~E1000_CTRL_RST; 418cab3c825SKevin Wolf } 419cab3c825SKevin Wolf 420cab3c825SKevin Wolf static void 4217c23b892Sbalrog set_rx_control(E1000State *s, int index, uint32_t val) 4227c23b892Sbalrog { 4237c23b892Sbalrog s->mac_reg[RCTL] = val; 4247c23b892Sbalrog s->rxbuf_size = rxbufsize(val); 4257c23b892Sbalrog s->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1; 4267c23b892Sbalrog DBGOUT(RX, "RCTL: %d, mac_reg[RCTL] = 0x%x\n", s->mac_reg[RDT], 4277c23b892Sbalrog s->mac_reg[RCTL]); 428b356f76dSJason Wang qemu_flush_queued_packets(qemu_get_queue(s->nic)); 4297c23b892Sbalrog } 4307c23b892Sbalrog 4317c23b892Sbalrog static void 4327c23b892Sbalrog set_mdic(E1000State *s, int index, uint32_t val) 4337c23b892Sbalrog { 4347c23b892Sbalrog uint32_t data = val & E1000_MDIC_DATA_MASK; 4357c23b892Sbalrog uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT); 4367c23b892Sbalrog 4377c23b892Sbalrog if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) // phy # 4387c23b892Sbalrog val = s->mac_reg[MDIC] | E1000_MDIC_ERROR; 4397c23b892Sbalrog else if (val & E1000_MDIC_OP_READ) { 4407c23b892Sbalrog DBGOUT(MDIC, "MDIC read reg 0x%x\n", addr); 4417c23b892Sbalrog if (!(phy_regcap[addr] & PHY_R)) { 4427c23b892Sbalrog DBGOUT(MDIC, "MDIC read reg %x unhandled\n", addr); 4437c23b892Sbalrog val |= E1000_MDIC_ERROR; 4447c23b892Sbalrog } else 4457c23b892Sbalrog val = (val ^ data) | s->phy_reg[addr]; 4467c23b892Sbalrog } else if (val & E1000_MDIC_OP_WRITE) { 4477c23b892Sbalrog DBGOUT(MDIC, "MDIC write reg 0x%x, value 0x%x\n", addr, data); 4487c23b892Sbalrog if (!(phy_regcap[addr] & PHY_W)) { 4497c23b892Sbalrog DBGOUT(MDIC, "MDIC write reg %x unhandled\n", addr); 4507c23b892Sbalrog val |= E1000_MDIC_ERROR; 451b9d03e35SJason Wang } else { 452b9d03e35SJason Wang if (addr < NPHYWRITEOPS && phyreg_writeops[addr]) { 453b9d03e35SJason Wang phyreg_writeops[addr](s, index, data); 454b9d03e35SJason Wang } 4557c23b892Sbalrog s->phy_reg[addr] = data; 4567c23b892Sbalrog } 457b9d03e35SJason Wang } 4587c23b892Sbalrog s->mac_reg[MDIC] = val | E1000_MDIC_READY; 45917fbbb0bSJason Wang 46017fbbb0bSJason Wang if (val & E1000_MDIC_INT_EN) { 4617c23b892Sbalrog set_ics(s, 0, E1000_ICR_MDAC); 4627c23b892Sbalrog } 46317fbbb0bSJason Wang } 4647c23b892Sbalrog 4657c23b892Sbalrog static uint32_t 4667c23b892Sbalrog get_eecd(E1000State *s, int index) 4677c23b892Sbalrog { 4687c23b892Sbalrog uint32_t ret = E1000_EECD_PRES|E1000_EECD_GNT | s->eecd_state.old_eecd; 4697c23b892Sbalrog 4707c23b892Sbalrog DBGOUT(EEPROM, "reading eeprom bit %d (reading %d)\n", 4717c23b892Sbalrog s->eecd_state.bitnum_out, s->eecd_state.reading); 4727c23b892Sbalrog if (!s->eecd_state.reading || 4737c23b892Sbalrog ((s->eeprom_data[(s->eecd_state.bitnum_out >> 4) & 0x3f] >> 4747c23b892Sbalrog ((s->eecd_state.bitnum_out & 0xf) ^ 0xf))) & 1) 4757c23b892Sbalrog ret |= E1000_EECD_DO; 4767c23b892Sbalrog return ret; 4777c23b892Sbalrog } 4787c23b892Sbalrog 4797c23b892Sbalrog static void 4807c23b892Sbalrog set_eecd(E1000State *s, int index, uint32_t val) 4817c23b892Sbalrog { 4827c23b892Sbalrog uint32_t oldval = s->eecd_state.old_eecd; 4837c23b892Sbalrog 4847c23b892Sbalrog s->eecd_state.old_eecd = val & (E1000_EECD_SK | E1000_EECD_CS | 4857c23b892Sbalrog E1000_EECD_DI|E1000_EECD_FWE_MASK|E1000_EECD_REQ); 4869651ac55SIzumi Tsutsui if (!(E1000_EECD_CS & val)) // CS inactive; nothing to do 4879651ac55SIzumi Tsutsui return; 4889651ac55SIzumi Tsutsui if (E1000_EECD_CS & (val ^ oldval)) { // CS rise edge; reset state 4899651ac55SIzumi Tsutsui s->eecd_state.val_in = 0; 4909651ac55SIzumi Tsutsui s->eecd_state.bitnum_in = 0; 4919651ac55SIzumi Tsutsui s->eecd_state.bitnum_out = 0; 4929651ac55SIzumi Tsutsui s->eecd_state.reading = 0; 4939651ac55SIzumi Tsutsui } 4947c23b892Sbalrog if (!(E1000_EECD_SK & (val ^ oldval))) // no clock edge 4957c23b892Sbalrog return; 4967c23b892Sbalrog if (!(E1000_EECD_SK & val)) { // falling edge 4977c23b892Sbalrog s->eecd_state.bitnum_out++; 4987c23b892Sbalrog return; 4997c23b892Sbalrog } 5007c23b892Sbalrog s->eecd_state.val_in <<= 1; 5017c23b892Sbalrog if (val & E1000_EECD_DI) 5027c23b892Sbalrog s->eecd_state.val_in |= 1; 5037c23b892Sbalrog if (++s->eecd_state.bitnum_in == 9 && !s->eecd_state.reading) { 5047c23b892Sbalrog s->eecd_state.bitnum_out = ((s->eecd_state.val_in & 0x3f)<<4)-1; 5057c23b892Sbalrog s->eecd_state.reading = (((s->eecd_state.val_in >> 6) & 7) == 5067c23b892Sbalrog EEPROM_READ_OPCODE_MICROWIRE); 5077c23b892Sbalrog } 5087c23b892Sbalrog DBGOUT(EEPROM, "eeprom bitnum in %d out %d, reading %d\n", 5097c23b892Sbalrog s->eecd_state.bitnum_in, s->eecd_state.bitnum_out, 5107c23b892Sbalrog s->eecd_state.reading); 5117c23b892Sbalrog } 5127c23b892Sbalrog 5137c23b892Sbalrog static uint32_t 5147c23b892Sbalrog flash_eerd_read(E1000State *s, int x) 5157c23b892Sbalrog { 5167c23b892Sbalrog unsigned int index, r = s->mac_reg[EERD] & ~E1000_EEPROM_RW_REG_START; 5177c23b892Sbalrog 518b1332393SBill Paul if ((s->mac_reg[EERD] & E1000_EEPROM_RW_REG_START) == 0) 519b1332393SBill Paul return (s->mac_reg[EERD]); 520b1332393SBill Paul 5217c23b892Sbalrog if ((index = r >> E1000_EEPROM_RW_ADDR_SHIFT) > EEPROM_CHECKSUM_REG) 522b1332393SBill Paul return (E1000_EEPROM_RW_REG_DONE | r); 523b1332393SBill Paul 524b1332393SBill Paul return ((s->eeprom_data[index] << E1000_EEPROM_RW_REG_DATA) | 525b1332393SBill Paul E1000_EEPROM_RW_REG_DONE | r); 5267c23b892Sbalrog } 5277c23b892Sbalrog 5287c23b892Sbalrog static void 5297c23b892Sbalrog putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t css, uint32_t cse) 5307c23b892Sbalrog { 531c6a6a5e3Saliguori uint32_t sum; 532c6a6a5e3Saliguori 5337c23b892Sbalrog if (cse && cse < n) 5347c23b892Sbalrog n = cse + 1; 535c6a6a5e3Saliguori if (sloc < n-1) { 536c6a6a5e3Saliguori sum = net_checksum_add(n-css, data+css); 537d8ee2591SPeter Maydell stw_be_p(data + sloc, net_checksum_finish(sum)); 538c6a6a5e3Saliguori } 5397c23b892Sbalrog } 5407c23b892Sbalrog 5418f2e8d1fSaliguori static inline int 5428f2e8d1fSaliguori vlan_enabled(E1000State *s) 5438f2e8d1fSaliguori { 5448f2e8d1fSaliguori return ((s->mac_reg[CTRL] & E1000_CTRL_VME) != 0); 5458f2e8d1fSaliguori } 5468f2e8d1fSaliguori 5478f2e8d1fSaliguori static inline int 5488f2e8d1fSaliguori vlan_rx_filter_enabled(E1000State *s) 5498f2e8d1fSaliguori { 5508f2e8d1fSaliguori return ((s->mac_reg[RCTL] & E1000_RCTL_VFE) != 0); 5518f2e8d1fSaliguori } 5528f2e8d1fSaliguori 5538f2e8d1fSaliguori static inline int 5548f2e8d1fSaliguori is_vlan_packet(E1000State *s, const uint8_t *buf) 5558f2e8d1fSaliguori { 5568f2e8d1fSaliguori return (be16_to_cpup((uint16_t *)(buf + 12)) == 5578f2e8d1fSaliguori le16_to_cpup((uint16_t *)(s->mac_reg + VET))); 5588f2e8d1fSaliguori } 5598f2e8d1fSaliguori 5608f2e8d1fSaliguori static inline int 5618f2e8d1fSaliguori is_vlan_txd(uint32_t txd_lower) 5628f2e8d1fSaliguori { 5638f2e8d1fSaliguori return ((txd_lower & E1000_TXD_CMD_VLE) != 0); 5648f2e8d1fSaliguori } 5658f2e8d1fSaliguori 56655e8d1ceSMichael S. Tsirkin /* FCS aka Ethernet CRC-32. We don't get it from backends and can't 56755e8d1ceSMichael S. Tsirkin * fill it in, just pad descriptor length by 4 bytes unless guest 568a05e8a6eSMichael S. Tsirkin * told us to strip it off the packet. */ 56955e8d1ceSMichael S. Tsirkin static inline int 57055e8d1ceSMichael S. Tsirkin fcs_len(E1000State *s) 57155e8d1ceSMichael S. Tsirkin { 57255e8d1ceSMichael S. Tsirkin return (s->mac_reg[RCTL] & E1000_RCTL_SECRC) ? 0 : 4; 57355e8d1ceSMichael S. Tsirkin } 57455e8d1ceSMichael S. Tsirkin 5757c23b892Sbalrog static void 57693e37d76SJason Wang e1000_send_packet(E1000State *s, const uint8_t *buf, int size) 57793e37d76SJason Wang { 578b356f76dSJason Wang NetClientState *nc = qemu_get_queue(s->nic); 57993e37d76SJason Wang if (s->phy_reg[PHY_CTRL] & MII_CR_LOOPBACK) { 580b356f76dSJason Wang nc->info->receive(nc, buf, size); 58193e37d76SJason Wang } else { 582b356f76dSJason Wang qemu_send_packet(nc, buf, size); 58393e37d76SJason Wang } 58493e37d76SJason Wang } 58593e37d76SJason Wang 58693e37d76SJason Wang static void 5877c23b892Sbalrog xmit_seg(E1000State *s) 5887c23b892Sbalrog { 5897c23b892Sbalrog uint16_t len, *sp; 5907c23b892Sbalrog unsigned int frames = s->tx.tso_frames, css, sofar, n; 5917c23b892Sbalrog struct e1000_tx *tp = &s->tx; 5927c23b892Sbalrog 5931b0009dbSbalrog if (tp->tse && tp->cptse) { 5947c23b892Sbalrog css = tp->ipcss; 5957c23b892Sbalrog DBGOUT(TXSUM, "frames %d size %d ipcss %d\n", 5967c23b892Sbalrog frames, tp->size, css); 5977c23b892Sbalrog if (tp->ip) { // IPv4 598d8ee2591SPeter Maydell stw_be_p(tp->data+css+2, tp->size - css); 599d8ee2591SPeter Maydell stw_be_p(tp->data+css+4, 6007c23b892Sbalrog be16_to_cpup((uint16_t *)(tp->data+css+4))+frames); 6017c23b892Sbalrog } else // IPv6 602d8ee2591SPeter Maydell stw_be_p(tp->data+css+4, tp->size - css); 6037c23b892Sbalrog css = tp->tucss; 6047c23b892Sbalrog len = tp->size - css; 6057c23b892Sbalrog DBGOUT(TXSUM, "tcp %d tucss %d len %d\n", tp->tcp, css, len); 6067c23b892Sbalrog if (tp->tcp) { 6077c23b892Sbalrog sofar = frames * tp->mss; 6086bd194abSPeter Maydell stl_be_p(tp->data+css+4, ldl_be_p(tp->data+css+4)+sofar); /* seq */ 6097c23b892Sbalrog if (tp->paylen - sofar > tp->mss) 6107c23b892Sbalrog tp->data[css + 13] &= ~9; // PSH, FIN 6117c23b892Sbalrog } else // UDP 612d8ee2591SPeter Maydell stw_be_p(tp->data+css+4, len); 6137c23b892Sbalrog if (tp->sum_needed & E1000_TXD_POPTS_TXSM) { 614e685b4ebSAlex Williamson unsigned int phsum; 6157c23b892Sbalrog // add pseudo-header length before checksum calculation 6167c23b892Sbalrog sp = (uint16_t *)(tp->data + tp->tucso); 617e685b4ebSAlex Williamson phsum = be16_to_cpup(sp) + len; 618e685b4ebSAlex Williamson phsum = (phsum >> 16) + (phsum & 0xffff); 619d8ee2591SPeter Maydell stw_be_p(sp, phsum); 6207c23b892Sbalrog } 6217c23b892Sbalrog tp->tso_frames++; 6227c23b892Sbalrog } 6237c23b892Sbalrog 6247c23b892Sbalrog if (tp->sum_needed & E1000_TXD_POPTS_TXSM) 6257c23b892Sbalrog putsum(tp->data, tp->size, tp->tucso, tp->tucss, tp->tucse); 6267c23b892Sbalrog if (tp->sum_needed & E1000_TXD_POPTS_IXSM) 6277c23b892Sbalrog putsum(tp->data, tp->size, tp->ipcso, tp->ipcss, tp->ipcse); 6288f2e8d1fSaliguori if (tp->vlan_needed) { 629b10fec9bSStefan Weil memmove(tp->vlan, tp->data, 4); 630b10fec9bSStefan Weil memmove(tp->data, tp->data + 4, 8); 6318f2e8d1fSaliguori memcpy(tp->data + 8, tp->vlan_header, 4); 63293e37d76SJason Wang e1000_send_packet(s, tp->vlan, tp->size + 4); 6338f2e8d1fSaliguori } else 63493e37d76SJason Wang e1000_send_packet(s, tp->data, tp->size); 6357c23b892Sbalrog s->mac_reg[TPT]++; 6367c23b892Sbalrog s->mac_reg[GPTC]++; 6377c23b892Sbalrog n = s->mac_reg[TOTL]; 6387c23b892Sbalrog if ((s->mac_reg[TOTL] += s->tx.size) < n) 6397c23b892Sbalrog s->mac_reg[TOTH]++; 6407c23b892Sbalrog } 6417c23b892Sbalrog 6427c23b892Sbalrog static void 6437c23b892Sbalrog process_tx_desc(E1000State *s, struct e1000_tx_desc *dp) 6447c23b892Sbalrog { 645b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 6467c23b892Sbalrog uint32_t txd_lower = le32_to_cpu(dp->lower.data); 6477c23b892Sbalrog uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D); 6487c23b892Sbalrog unsigned int split_size = txd_lower & 0xffff, bytes, sz, op; 649a0ae17a6SAndrew Jones unsigned int msh = 0xfffff; 6507c23b892Sbalrog uint64_t addr; 6517c23b892Sbalrog struct e1000_context_desc *xp = (struct e1000_context_desc *)dp; 6527c23b892Sbalrog struct e1000_tx *tp = &s->tx; 6537c23b892Sbalrog 654e9845f09SVincenzo Maffione s->mit_ide |= (txd_lower & E1000_TXD_CMD_IDE); 6557c23b892Sbalrog if (dtype == E1000_TXD_CMD_DEXT) { // context descriptor 6567c23b892Sbalrog op = le32_to_cpu(xp->cmd_and_length); 6577c23b892Sbalrog tp->ipcss = xp->lower_setup.ip_fields.ipcss; 6587c23b892Sbalrog tp->ipcso = xp->lower_setup.ip_fields.ipcso; 6597c23b892Sbalrog tp->ipcse = le16_to_cpu(xp->lower_setup.ip_fields.ipcse); 6607c23b892Sbalrog tp->tucss = xp->upper_setup.tcp_fields.tucss; 6617c23b892Sbalrog tp->tucso = xp->upper_setup.tcp_fields.tucso; 6627c23b892Sbalrog tp->tucse = le16_to_cpu(xp->upper_setup.tcp_fields.tucse); 6637c23b892Sbalrog tp->paylen = op & 0xfffff; 6647c23b892Sbalrog tp->hdr_len = xp->tcp_seg_setup.fields.hdr_len; 6657c23b892Sbalrog tp->mss = le16_to_cpu(xp->tcp_seg_setup.fields.mss); 6667c23b892Sbalrog tp->ip = (op & E1000_TXD_CMD_IP) ? 1 : 0; 6677c23b892Sbalrog tp->tcp = (op & E1000_TXD_CMD_TCP) ? 1 : 0; 6687c23b892Sbalrog tp->tse = (op & E1000_TXD_CMD_TSE) ? 1 : 0; 6697c23b892Sbalrog tp->tso_frames = 0; 6707c23b892Sbalrog if (tp->tucso == 0) { // this is probably wrong 6717c23b892Sbalrog DBGOUT(TXSUM, "TCP/UDP: cso 0!\n"); 6727c23b892Sbalrog tp->tucso = tp->tucss + (tp->tcp ? 16 : 6); 6737c23b892Sbalrog } 6747c23b892Sbalrog return; 6751b0009dbSbalrog } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) { 6761b0009dbSbalrog // data descriptor 677735e77ecSStefan Hajnoczi if (tp->size == 0) { 6787c23b892Sbalrog tp->sum_needed = le32_to_cpu(dp->upper.data) >> 8; 679735e77ecSStefan Hajnoczi } 6801b0009dbSbalrog tp->cptse = ( txd_lower & E1000_TXD_CMD_TSE ) ? 1 : 0; 68143ad7e3eSJes Sorensen } else { 6821b0009dbSbalrog // legacy descriptor 6831b0009dbSbalrog tp->cptse = 0; 68443ad7e3eSJes Sorensen } 6857c23b892Sbalrog 6868f2e8d1fSaliguori if (vlan_enabled(s) && is_vlan_txd(txd_lower) && 6878f2e8d1fSaliguori (tp->cptse || txd_lower & E1000_TXD_CMD_EOP)) { 6888f2e8d1fSaliguori tp->vlan_needed = 1; 689d8ee2591SPeter Maydell stw_be_p(tp->vlan_header, 6908f2e8d1fSaliguori le16_to_cpup((uint16_t *)(s->mac_reg + VET))); 691d8ee2591SPeter Maydell stw_be_p(tp->vlan_header + 2, 6928f2e8d1fSaliguori le16_to_cpu(dp->upper.fields.special)); 6938f2e8d1fSaliguori } 6948f2e8d1fSaliguori 6957c23b892Sbalrog addr = le64_to_cpu(dp->buffer_addr); 6961b0009dbSbalrog if (tp->tse && tp->cptse) { 697a0ae17a6SAndrew Jones msh = tp->hdr_len + tp->mss; 6987c23b892Sbalrog do { 6997c23b892Sbalrog bytes = split_size; 7007c23b892Sbalrog if (tp->size + bytes > msh) 7017c23b892Sbalrog bytes = msh - tp->size; 70265f82df0SAnthony Liguori 70365f82df0SAnthony Liguori bytes = MIN(sizeof(tp->data) - tp->size, bytes); 704b08340d5SAndreas Färber pci_dma_read(d, addr, tp->data + tp->size, bytes); 705a0ae17a6SAndrew Jones sz = tp->size + bytes; 706a0ae17a6SAndrew Jones if (sz >= tp->hdr_len && tp->size < tp->hdr_len) { 707a0ae17a6SAndrew Jones memmove(tp->header, tp->data, tp->hdr_len); 708a0ae17a6SAndrew Jones } 7097c23b892Sbalrog tp->size = sz; 7107c23b892Sbalrog addr += bytes; 7117c23b892Sbalrog if (sz == msh) { 7127c23b892Sbalrog xmit_seg(s); 713a0ae17a6SAndrew Jones memmove(tp->data, tp->header, tp->hdr_len); 714a0ae17a6SAndrew Jones tp->size = tp->hdr_len; 7157c23b892Sbalrog } 7167c23b892Sbalrog } while (split_size -= bytes); 7171b0009dbSbalrog } else if (!tp->tse && tp->cptse) { 7181b0009dbSbalrog // context descriptor TSE is not set, while data descriptor TSE is set 719362f5fb5SStefan Weil DBGOUT(TXERR, "TCP segmentation error\n"); 7201b0009dbSbalrog } else { 72165f82df0SAnthony Liguori split_size = MIN(sizeof(tp->data) - tp->size, split_size); 722b08340d5SAndreas Färber pci_dma_read(d, addr, tp->data + tp->size, split_size); 7231b0009dbSbalrog tp->size += split_size; 7241b0009dbSbalrog } 7257c23b892Sbalrog 7267c23b892Sbalrog if (!(txd_lower & E1000_TXD_CMD_EOP)) 7277c23b892Sbalrog return; 728a0ae17a6SAndrew Jones if (!(tp->tse && tp->cptse && tp->size < tp->hdr_len)) { 7297c23b892Sbalrog xmit_seg(s); 730a0ae17a6SAndrew Jones } 7317c23b892Sbalrog tp->tso_frames = 0; 7327c23b892Sbalrog tp->sum_needed = 0; 7338f2e8d1fSaliguori tp->vlan_needed = 0; 7347c23b892Sbalrog tp->size = 0; 7351b0009dbSbalrog tp->cptse = 0; 7367c23b892Sbalrog } 7377c23b892Sbalrog 7387c23b892Sbalrog static uint32_t 73962ecbd35SEduard - Gabriel Munteanu txdesc_writeback(E1000State *s, dma_addr_t base, struct e1000_tx_desc *dp) 7407c23b892Sbalrog { 741b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 7427c23b892Sbalrog uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data); 7437c23b892Sbalrog 7447c23b892Sbalrog if (!(txd_lower & (E1000_TXD_CMD_RS|E1000_TXD_CMD_RPS))) 7457c23b892Sbalrog return 0; 7467c23b892Sbalrog txd_upper = (le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD) & 7477c23b892Sbalrog ~(E1000_TXD_STAT_EC | E1000_TXD_STAT_LC | E1000_TXD_STAT_TU); 7487c23b892Sbalrog dp->upper.data = cpu_to_le32(txd_upper); 749b08340d5SAndreas Färber pci_dma_write(d, base + ((char *)&dp->upper - (char *)dp), 75000c3a05bSDavid Gibson &dp->upper, sizeof(dp->upper)); 7517c23b892Sbalrog return E1000_ICR_TXDW; 7527c23b892Sbalrog } 7537c23b892Sbalrog 754d17161f6SKevin Wolf static uint64_t tx_desc_base(E1000State *s) 755d17161f6SKevin Wolf { 756d17161f6SKevin Wolf uint64_t bah = s->mac_reg[TDBAH]; 757d17161f6SKevin Wolf uint64_t bal = s->mac_reg[TDBAL] & ~0xf; 758d17161f6SKevin Wolf 759d17161f6SKevin Wolf return (bah << 32) + bal; 760d17161f6SKevin Wolf } 761d17161f6SKevin Wolf 7627c23b892Sbalrog static void 7637c23b892Sbalrog start_xmit(E1000State *s) 7647c23b892Sbalrog { 765b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 76662ecbd35SEduard - Gabriel Munteanu dma_addr_t base; 7677c23b892Sbalrog struct e1000_tx_desc desc; 7687c23b892Sbalrog uint32_t tdh_start = s->mac_reg[TDH], cause = E1000_ICS_TXQE; 7697c23b892Sbalrog 7707c23b892Sbalrog if (!(s->mac_reg[TCTL] & E1000_TCTL_EN)) { 7717c23b892Sbalrog DBGOUT(TX, "tx disabled\n"); 7727c23b892Sbalrog return; 7737c23b892Sbalrog } 7747c23b892Sbalrog 7757c23b892Sbalrog while (s->mac_reg[TDH] != s->mac_reg[TDT]) { 776d17161f6SKevin Wolf base = tx_desc_base(s) + 7777c23b892Sbalrog sizeof(struct e1000_tx_desc) * s->mac_reg[TDH]; 778b08340d5SAndreas Färber pci_dma_read(d, base, &desc, sizeof(desc)); 7797c23b892Sbalrog 7807c23b892Sbalrog DBGOUT(TX, "index %d: %p : %x %x\n", s->mac_reg[TDH], 7816106075bSths (void *)(intptr_t)desc.buffer_addr, desc.lower.data, 7827c23b892Sbalrog desc.upper.data); 7837c23b892Sbalrog 7847c23b892Sbalrog process_tx_desc(s, &desc); 78562ecbd35SEduard - Gabriel Munteanu cause |= txdesc_writeback(s, base, &desc); 7867c23b892Sbalrog 7877c23b892Sbalrog if (++s->mac_reg[TDH] * sizeof(desc) >= s->mac_reg[TDLEN]) 7887c23b892Sbalrog s->mac_reg[TDH] = 0; 7897c23b892Sbalrog /* 7907c23b892Sbalrog * the following could happen only if guest sw assigns 7917c23b892Sbalrog * bogus values to TDT/TDLEN. 7927c23b892Sbalrog * there's nothing too intelligent we could do about this. 7937c23b892Sbalrog */ 7947c23b892Sbalrog if (s->mac_reg[TDH] == tdh_start) { 7957c23b892Sbalrog DBGOUT(TXERR, "TDH wraparound @%x, TDT %x, TDLEN %x\n", 7967c23b892Sbalrog tdh_start, s->mac_reg[TDT], s->mac_reg[TDLEN]); 7977c23b892Sbalrog break; 7987c23b892Sbalrog } 7997c23b892Sbalrog } 8007c23b892Sbalrog set_ics(s, 0, cause); 8017c23b892Sbalrog } 8027c23b892Sbalrog 8037c23b892Sbalrog static int 8047c23b892Sbalrog receive_filter(E1000State *s, const uint8_t *buf, int size) 8057c23b892Sbalrog { 806af2960f9SBlue Swirl static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 807af2960f9SBlue Swirl static const int mta_shift[] = {4, 3, 2, 0}; 8087c23b892Sbalrog uint32_t f, rctl = s->mac_reg[RCTL], ra[2], *rp; 8097c23b892Sbalrog 8108f2e8d1fSaliguori if (is_vlan_packet(s, buf) && vlan_rx_filter_enabled(s)) { 8118f2e8d1fSaliguori uint16_t vid = be16_to_cpup((uint16_t *)(buf + 14)); 8128f2e8d1fSaliguori uint32_t vfta = le32_to_cpup((uint32_t *)(s->mac_reg + VFTA) + 8138f2e8d1fSaliguori ((vid >> 5) & 0x7f)); 8148f2e8d1fSaliguori if ((vfta & (1 << (vid & 0x1f))) == 0) 8158f2e8d1fSaliguori return 0; 8168f2e8d1fSaliguori } 8178f2e8d1fSaliguori 8187c23b892Sbalrog if (rctl & E1000_RCTL_UPE) // promiscuous 8197c23b892Sbalrog return 1; 8207c23b892Sbalrog 8217c23b892Sbalrog if ((buf[0] & 1) && (rctl & E1000_RCTL_MPE)) // promiscuous mcast 8227c23b892Sbalrog return 1; 8237c23b892Sbalrog 8247c23b892Sbalrog if ((rctl & E1000_RCTL_BAM) && !memcmp(buf, bcast, sizeof bcast)) 8257c23b892Sbalrog return 1; 8267c23b892Sbalrog 8277c23b892Sbalrog for (rp = s->mac_reg + RA; rp < s->mac_reg + RA + 32; rp += 2) { 8287c23b892Sbalrog if (!(rp[1] & E1000_RAH_AV)) 8297c23b892Sbalrog continue; 8307c23b892Sbalrog ra[0] = cpu_to_le32(rp[0]); 8317c23b892Sbalrog ra[1] = cpu_to_le32(rp[1]); 8327c23b892Sbalrog if (!memcmp(buf, (uint8_t *)ra, 6)) { 8337c23b892Sbalrog DBGOUT(RXFILTER, 8347c23b892Sbalrog "unicast match[%d]: %02x:%02x:%02x:%02x:%02x:%02x\n", 8357c23b892Sbalrog (int)(rp - s->mac_reg - RA)/2, 8367c23b892Sbalrog buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); 8377c23b892Sbalrog return 1; 8387c23b892Sbalrog } 8397c23b892Sbalrog } 8407c23b892Sbalrog DBGOUT(RXFILTER, "unicast mismatch: %02x:%02x:%02x:%02x:%02x:%02x\n", 8417c23b892Sbalrog buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); 8427c23b892Sbalrog 8437c23b892Sbalrog f = mta_shift[(rctl >> E1000_RCTL_MO_SHIFT) & 3]; 8447c23b892Sbalrog f = (((buf[5] << 8) | buf[4]) >> f) & 0xfff; 8457c23b892Sbalrog if (s->mac_reg[MTA + (f >> 5)] & (1 << (f & 0x1f))) 8467c23b892Sbalrog return 1; 8477c23b892Sbalrog DBGOUT(RXFILTER, 8487c23b892Sbalrog "dropping, inexact filter mismatch: %02x:%02x:%02x:%02x:%02x:%02x MO %d MTA[%d] %x\n", 8497c23b892Sbalrog buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], 8507c23b892Sbalrog (rctl >> E1000_RCTL_MO_SHIFT) & 3, f >> 5, 8517c23b892Sbalrog s->mac_reg[MTA + (f >> 5)]); 8527c23b892Sbalrog 8537c23b892Sbalrog return 0; 8547c23b892Sbalrog } 8557c23b892Sbalrog 85699ed7e30Saliguori static void 8574e68f7a0SStefan Hajnoczi e1000_set_link_status(NetClientState *nc) 85899ed7e30Saliguori { 859cc1f0f45SJason Wang E1000State *s = qemu_get_nic_opaque(nc); 86099ed7e30Saliguori uint32_t old_status = s->mac_reg[STATUS]; 86199ed7e30Saliguori 862d4044c2aSBjørn Mork if (nc->link_down) { 86371aadd3cSJason Wang e1000_link_down(s); 864d4044c2aSBjørn Mork } else { 86571aadd3cSJason Wang e1000_link_up(s); 866d4044c2aSBjørn Mork } 86799ed7e30Saliguori 86899ed7e30Saliguori if (s->mac_reg[STATUS] != old_status) 86999ed7e30Saliguori set_ics(s, 0, E1000_ICR_LSC); 87099ed7e30Saliguori } 87199ed7e30Saliguori 872322fd48aSMichael S. Tsirkin static bool e1000_has_rxbufs(E1000State *s, size_t total_size) 873322fd48aSMichael S. Tsirkin { 874322fd48aSMichael S. Tsirkin int bufs; 875322fd48aSMichael S. Tsirkin /* Fast-path short packets */ 876322fd48aSMichael S. Tsirkin if (total_size <= s->rxbuf_size) { 877e5b8b0d4SDmitry Fleytman return s->mac_reg[RDH] != s->mac_reg[RDT]; 878322fd48aSMichael S. Tsirkin } 879322fd48aSMichael S. Tsirkin if (s->mac_reg[RDH] < s->mac_reg[RDT]) { 880322fd48aSMichael S. Tsirkin bufs = s->mac_reg[RDT] - s->mac_reg[RDH]; 881e5b8b0d4SDmitry Fleytman } else if (s->mac_reg[RDH] > s->mac_reg[RDT]) { 882322fd48aSMichael S. Tsirkin bufs = s->mac_reg[RDLEN] / sizeof(struct e1000_rx_desc) + 883322fd48aSMichael S. Tsirkin s->mac_reg[RDT] - s->mac_reg[RDH]; 884322fd48aSMichael S. Tsirkin } else { 885322fd48aSMichael S. Tsirkin return false; 886322fd48aSMichael S. Tsirkin } 887322fd48aSMichael S. Tsirkin return total_size <= bufs * s->rxbuf_size; 888322fd48aSMichael S. Tsirkin } 889322fd48aSMichael S. Tsirkin 8906cdfab28SMichael S. Tsirkin static int 8914e68f7a0SStefan Hajnoczi e1000_can_receive(NetClientState *nc) 8926cdfab28SMichael S. Tsirkin { 893cc1f0f45SJason Wang E1000State *s = qemu_get_nic_opaque(nc); 8946cdfab28SMichael S. Tsirkin 895ddcb73b7SMichael S. Tsirkin return (s->mac_reg[STATUS] & E1000_STATUS_LU) && 896ddcb73b7SMichael S. Tsirkin (s->mac_reg[RCTL] & E1000_RCTL_EN) && e1000_has_rxbufs(s, 1); 8976cdfab28SMichael S. Tsirkin } 8986cdfab28SMichael S. Tsirkin 899d17161f6SKevin Wolf static uint64_t rx_desc_base(E1000State *s) 900d17161f6SKevin Wolf { 901d17161f6SKevin Wolf uint64_t bah = s->mac_reg[RDBAH]; 902d17161f6SKevin Wolf uint64_t bal = s->mac_reg[RDBAL] & ~0xf; 903d17161f6SKevin Wolf 904d17161f6SKevin Wolf return (bah << 32) + bal; 905d17161f6SKevin Wolf } 906d17161f6SKevin Wolf 9074f1c942bSMark McLoughlin static ssize_t 90897410ddeSVincenzo Maffione e1000_receive_iov(NetClientState *nc, const struct iovec *iov, int iovcnt) 9097c23b892Sbalrog { 910cc1f0f45SJason Wang E1000State *s = qemu_get_nic_opaque(nc); 911b08340d5SAndreas Färber PCIDevice *d = PCI_DEVICE(s); 9127c23b892Sbalrog struct e1000_rx_desc desc; 91362ecbd35SEduard - Gabriel Munteanu dma_addr_t base; 9147c23b892Sbalrog unsigned int n, rdt; 9157c23b892Sbalrog uint32_t rdh_start; 9168f2e8d1fSaliguori uint16_t vlan_special = 0; 91797410ddeSVincenzo Maffione uint8_t vlan_status = 0; 91878aeb23eSStefan Hajnoczi uint8_t min_buf[MIN_BUF_SIZE]; 91997410ddeSVincenzo Maffione struct iovec min_iov; 92097410ddeSVincenzo Maffione uint8_t *filter_buf = iov->iov_base; 92197410ddeSVincenzo Maffione size_t size = iov_size(iov, iovcnt); 92297410ddeSVincenzo Maffione size_t iov_ofs = 0; 923b19487e2SMichael S. Tsirkin size_t desc_offset; 924b19487e2SMichael S. Tsirkin size_t desc_size; 925b19487e2SMichael S. Tsirkin size_t total_size; 9267c23b892Sbalrog 927ddcb73b7SMichael S. Tsirkin if (!(s->mac_reg[STATUS] & E1000_STATUS_LU)) { 9284f1c942bSMark McLoughlin return -1; 929ddcb73b7SMichael S. Tsirkin } 930ddcb73b7SMichael S. Tsirkin 931ddcb73b7SMichael S. Tsirkin if (!(s->mac_reg[RCTL] & E1000_RCTL_EN)) { 932ddcb73b7SMichael S. Tsirkin return -1; 933ddcb73b7SMichael S. Tsirkin } 9347c23b892Sbalrog 93578aeb23eSStefan Hajnoczi /* Pad to minimum Ethernet frame length */ 93678aeb23eSStefan Hajnoczi if (size < sizeof(min_buf)) { 93797410ddeSVincenzo Maffione iov_to_buf(iov, iovcnt, 0, min_buf, size); 93878aeb23eSStefan Hajnoczi memset(&min_buf[size], 0, sizeof(min_buf) - size); 93997410ddeSVincenzo Maffione min_iov.iov_base = filter_buf = min_buf; 94097410ddeSVincenzo Maffione min_iov.iov_len = size = sizeof(min_buf); 94197410ddeSVincenzo Maffione iovcnt = 1; 94297410ddeSVincenzo Maffione iov = &min_iov; 94397410ddeSVincenzo Maffione } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) { 94497410ddeSVincenzo Maffione /* This is very unlikely, but may happen. */ 94597410ddeSVincenzo Maffione iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN); 94697410ddeSVincenzo Maffione filter_buf = min_buf; 94778aeb23eSStefan Hajnoczi } 94878aeb23eSStefan Hajnoczi 949b0d9ffcdSMichael Contreras /* Discard oversized packets if !LPE and !SBP. */ 9502c0331f4SMichael Contreras if ((size > MAXIMUM_ETHERNET_LPE_SIZE || 9512c0331f4SMichael Contreras (size > MAXIMUM_ETHERNET_VLAN_SIZE 9522c0331f4SMichael Contreras && !(s->mac_reg[RCTL] & E1000_RCTL_LPE))) 953b0d9ffcdSMichael Contreras && !(s->mac_reg[RCTL] & E1000_RCTL_SBP)) { 954b0d9ffcdSMichael Contreras return size; 955b0d9ffcdSMichael Contreras } 956b0d9ffcdSMichael Contreras 95797410ddeSVincenzo Maffione if (!receive_filter(s, filter_buf, size)) { 9584f1c942bSMark McLoughlin return size; 95997410ddeSVincenzo Maffione } 9607c23b892Sbalrog 96197410ddeSVincenzo Maffione if (vlan_enabled(s) && is_vlan_packet(s, filter_buf)) { 96297410ddeSVincenzo Maffione vlan_special = cpu_to_le16(be16_to_cpup((uint16_t *)(filter_buf 96397410ddeSVincenzo Maffione + 14))); 96497410ddeSVincenzo Maffione iov_ofs = 4; 96597410ddeSVincenzo Maffione if (filter_buf == iov->iov_base) { 96697410ddeSVincenzo Maffione memmove(filter_buf + 4, filter_buf, 12); 96797410ddeSVincenzo Maffione } else { 96897410ddeSVincenzo Maffione iov_from_buf(iov, iovcnt, 4, filter_buf, 12); 96997410ddeSVincenzo Maffione while (iov->iov_len <= iov_ofs) { 97097410ddeSVincenzo Maffione iov_ofs -= iov->iov_len; 97197410ddeSVincenzo Maffione iov++; 97297410ddeSVincenzo Maffione } 97397410ddeSVincenzo Maffione } 9748f2e8d1fSaliguori vlan_status = E1000_RXD_STAT_VP; 9758f2e8d1fSaliguori size -= 4; 9768f2e8d1fSaliguori } 9778f2e8d1fSaliguori 9787c23b892Sbalrog rdh_start = s->mac_reg[RDH]; 979b19487e2SMichael S. Tsirkin desc_offset = 0; 980b19487e2SMichael S. Tsirkin total_size = size + fcs_len(s); 981322fd48aSMichael S. Tsirkin if (!e1000_has_rxbufs(s, total_size)) { 982322fd48aSMichael S. Tsirkin set_ics(s, 0, E1000_ICS_RXO); 983322fd48aSMichael S. Tsirkin return -1; 984322fd48aSMichael S. Tsirkin } 9857c23b892Sbalrog do { 986b19487e2SMichael S. Tsirkin desc_size = total_size - desc_offset; 987b19487e2SMichael S. Tsirkin if (desc_size > s->rxbuf_size) { 988b19487e2SMichael S. Tsirkin desc_size = s->rxbuf_size; 989b19487e2SMichael S. Tsirkin } 990d17161f6SKevin Wolf base = rx_desc_base(s) + sizeof(desc) * s->mac_reg[RDH]; 991b08340d5SAndreas Färber pci_dma_read(d, base, &desc, sizeof(desc)); 9928f2e8d1fSaliguori desc.special = vlan_special; 9938f2e8d1fSaliguori desc.status |= (vlan_status | E1000_RXD_STAT_DD); 9947c23b892Sbalrog if (desc.buffer_addr) { 995b19487e2SMichael S. Tsirkin if (desc_offset < size) { 99697410ddeSVincenzo Maffione size_t iov_copy; 99797410ddeSVincenzo Maffione hwaddr ba = le64_to_cpu(desc.buffer_addr); 998b19487e2SMichael S. Tsirkin size_t copy_size = size - desc_offset; 999b19487e2SMichael S. Tsirkin if (copy_size > s->rxbuf_size) { 1000b19487e2SMichael S. Tsirkin copy_size = s->rxbuf_size; 1001b19487e2SMichael S. Tsirkin } 100297410ddeSVincenzo Maffione do { 100397410ddeSVincenzo Maffione iov_copy = MIN(copy_size, iov->iov_len - iov_ofs); 100497410ddeSVincenzo Maffione pci_dma_write(d, ba, iov->iov_base + iov_ofs, iov_copy); 100597410ddeSVincenzo Maffione copy_size -= iov_copy; 100697410ddeSVincenzo Maffione ba += iov_copy; 100797410ddeSVincenzo Maffione iov_ofs += iov_copy; 100897410ddeSVincenzo Maffione if (iov_ofs == iov->iov_len) { 100997410ddeSVincenzo Maffione iov++; 101097410ddeSVincenzo Maffione iov_ofs = 0; 101197410ddeSVincenzo Maffione } 101297410ddeSVincenzo Maffione } while (copy_size); 1013b19487e2SMichael S. Tsirkin } 1014b19487e2SMichael S. Tsirkin desc_offset += desc_size; 1015b19487e2SMichael S. Tsirkin desc.length = cpu_to_le16(desc_size); 1016ee912ccfSMichael S. Tsirkin if (desc_offset >= total_size) { 10177c23b892Sbalrog desc.status |= E1000_RXD_STAT_EOP | E1000_RXD_STAT_IXSM; 1018b19487e2SMichael S. Tsirkin } else { 1019ee912ccfSMichael S. Tsirkin /* Guest zeroing out status is not a hardware requirement. 1020ee912ccfSMichael S. Tsirkin Clear EOP in case guest didn't do it. */ 1021ee912ccfSMichael S. Tsirkin desc.status &= ~E1000_RXD_STAT_EOP; 1022b19487e2SMichael S. Tsirkin } 102343ad7e3eSJes Sorensen } else { // as per intel docs; skip descriptors with null buf addr 10247c23b892Sbalrog DBGOUT(RX, "Null RX descriptor!!\n"); 102543ad7e3eSJes Sorensen } 1026b08340d5SAndreas Färber pci_dma_write(d, base, &desc, sizeof(desc)); 10277c23b892Sbalrog 10287c23b892Sbalrog if (++s->mac_reg[RDH] * sizeof(desc) >= s->mac_reg[RDLEN]) 10297c23b892Sbalrog s->mac_reg[RDH] = 0; 10307c23b892Sbalrog /* see comment in start_xmit; same here */ 10317c23b892Sbalrog if (s->mac_reg[RDH] == rdh_start) { 10327c23b892Sbalrog DBGOUT(RXERR, "RDH wraparound @%x, RDT %x, RDLEN %x\n", 10337c23b892Sbalrog rdh_start, s->mac_reg[RDT], s->mac_reg[RDLEN]); 10347c23b892Sbalrog set_ics(s, 0, E1000_ICS_RXO); 10354f1c942bSMark McLoughlin return -1; 10367c23b892Sbalrog } 1037b19487e2SMichael S. Tsirkin } while (desc_offset < total_size); 10387c23b892Sbalrog 10397c23b892Sbalrog s->mac_reg[GPRC]++; 10407c23b892Sbalrog s->mac_reg[TPR]++; 1041a05e8a6eSMichael S. Tsirkin /* TOR - Total Octets Received: 1042a05e8a6eSMichael S. Tsirkin * This register includes bytes received in a packet from the <Destination 1043a05e8a6eSMichael S. Tsirkin * Address> field through the <CRC> field, inclusively. 1044a05e8a6eSMichael S. Tsirkin */ 1045a05e8a6eSMichael S. Tsirkin n = s->mac_reg[TORL] + size + /* Always include FCS length. */ 4; 1046a05e8a6eSMichael S. Tsirkin if (n < s->mac_reg[TORL]) 10477c23b892Sbalrog s->mac_reg[TORH]++; 1048a05e8a6eSMichael S. Tsirkin s->mac_reg[TORL] = n; 10497c23b892Sbalrog 10507c23b892Sbalrog n = E1000_ICS_RXT0; 10517c23b892Sbalrog if ((rdt = s->mac_reg[RDT]) < s->mac_reg[RDH]) 10527c23b892Sbalrog rdt += s->mac_reg[RDLEN] / sizeof(desc); 1053bf16cc8fSaliguori if (((rdt - s->mac_reg[RDH]) * sizeof(desc)) <= s->mac_reg[RDLEN] >> 1054bf16cc8fSaliguori s->rxbuf_min_shift) 10557c23b892Sbalrog n |= E1000_ICS_RXDMT0; 10567c23b892Sbalrog 10577c23b892Sbalrog set_ics(s, 0, n); 10584f1c942bSMark McLoughlin 10594f1c942bSMark McLoughlin return size; 10607c23b892Sbalrog } 10617c23b892Sbalrog 106297410ddeSVincenzo Maffione static ssize_t 106397410ddeSVincenzo Maffione e1000_receive(NetClientState *nc, const uint8_t *buf, size_t size) 106497410ddeSVincenzo Maffione { 106597410ddeSVincenzo Maffione const struct iovec iov = { 106697410ddeSVincenzo Maffione .iov_base = (uint8_t *)buf, 106797410ddeSVincenzo Maffione .iov_len = size 106897410ddeSVincenzo Maffione }; 106997410ddeSVincenzo Maffione 107097410ddeSVincenzo Maffione return e1000_receive_iov(nc, &iov, 1); 107197410ddeSVincenzo Maffione } 107297410ddeSVincenzo Maffione 10737c23b892Sbalrog static uint32_t 10747c23b892Sbalrog mac_readreg(E1000State *s, int index) 10757c23b892Sbalrog { 10767c23b892Sbalrog return s->mac_reg[index]; 10777c23b892Sbalrog } 10787c23b892Sbalrog 10797c23b892Sbalrog static uint32_t 10807c23b892Sbalrog mac_icr_read(E1000State *s, int index) 10817c23b892Sbalrog { 10827c23b892Sbalrog uint32_t ret = s->mac_reg[ICR]; 10837c23b892Sbalrog 10847c23b892Sbalrog DBGOUT(INTERRUPT, "ICR read: %x\n", ret); 10857c23b892Sbalrog set_interrupt_cause(s, 0, 0); 10867c23b892Sbalrog return ret; 10877c23b892Sbalrog } 10887c23b892Sbalrog 10897c23b892Sbalrog static uint32_t 10907c23b892Sbalrog mac_read_clr4(E1000State *s, int index) 10917c23b892Sbalrog { 10927c23b892Sbalrog uint32_t ret = s->mac_reg[index]; 10937c23b892Sbalrog 10947c23b892Sbalrog s->mac_reg[index] = 0; 10957c23b892Sbalrog return ret; 10967c23b892Sbalrog } 10977c23b892Sbalrog 10987c23b892Sbalrog static uint32_t 10997c23b892Sbalrog mac_read_clr8(E1000State *s, int index) 11007c23b892Sbalrog { 11017c23b892Sbalrog uint32_t ret = s->mac_reg[index]; 11027c23b892Sbalrog 11037c23b892Sbalrog s->mac_reg[index] = 0; 11047c23b892Sbalrog s->mac_reg[index-1] = 0; 11057c23b892Sbalrog return ret; 11067c23b892Sbalrog } 11077c23b892Sbalrog 11087c23b892Sbalrog static void 11097c23b892Sbalrog mac_writereg(E1000State *s, int index, uint32_t val) 11107c23b892Sbalrog { 11117c36507cSAmos Kong uint32_t macaddr[2]; 11127c36507cSAmos Kong 11137c23b892Sbalrog s->mac_reg[index] = val; 11147c36507cSAmos Kong 111590d131fbSMichael S. Tsirkin if (index == RA + 1) { 11167c36507cSAmos Kong macaddr[0] = cpu_to_le32(s->mac_reg[RA]); 11177c36507cSAmos Kong macaddr[1] = cpu_to_le32(s->mac_reg[RA + 1]); 11187c36507cSAmos Kong qemu_format_nic_info_str(qemu_get_queue(s->nic), (uint8_t *)macaddr); 11197c36507cSAmos Kong } 11207c23b892Sbalrog } 11217c23b892Sbalrog 11227c23b892Sbalrog static void 11237c23b892Sbalrog set_rdt(E1000State *s, int index, uint32_t val) 11247c23b892Sbalrog { 11257c23b892Sbalrog s->mac_reg[index] = val & 0xffff; 1126e8b4c680SPaolo Bonzini if (e1000_has_rxbufs(s, 1)) { 1127b356f76dSJason Wang qemu_flush_queued_packets(qemu_get_queue(s->nic)); 1128e8b4c680SPaolo Bonzini } 11297c23b892Sbalrog } 11307c23b892Sbalrog 11317c23b892Sbalrog static void 11327c23b892Sbalrog set_16bit(E1000State *s, int index, uint32_t val) 11337c23b892Sbalrog { 11347c23b892Sbalrog s->mac_reg[index] = val & 0xffff; 11357c23b892Sbalrog } 11367c23b892Sbalrog 11377c23b892Sbalrog static void 11387c23b892Sbalrog set_dlen(E1000State *s, int index, uint32_t val) 11397c23b892Sbalrog { 11407c23b892Sbalrog s->mac_reg[index] = val & 0xfff80; 11417c23b892Sbalrog } 11427c23b892Sbalrog 11437c23b892Sbalrog static void 11447c23b892Sbalrog set_tctl(E1000State *s, int index, uint32_t val) 11457c23b892Sbalrog { 11467c23b892Sbalrog s->mac_reg[index] = val; 11477c23b892Sbalrog s->mac_reg[TDT] &= 0xffff; 11487c23b892Sbalrog start_xmit(s); 11497c23b892Sbalrog } 11507c23b892Sbalrog 11517c23b892Sbalrog static void 11527c23b892Sbalrog set_icr(E1000State *s, int index, uint32_t val) 11537c23b892Sbalrog { 11547c23b892Sbalrog DBGOUT(INTERRUPT, "set_icr %x\n", val); 11557c23b892Sbalrog set_interrupt_cause(s, 0, s->mac_reg[ICR] & ~val); 11567c23b892Sbalrog } 11577c23b892Sbalrog 11587c23b892Sbalrog static void 11597c23b892Sbalrog set_imc(E1000State *s, int index, uint32_t val) 11607c23b892Sbalrog { 11617c23b892Sbalrog s->mac_reg[IMS] &= ~val; 11627c23b892Sbalrog set_ics(s, 0, 0); 11637c23b892Sbalrog } 11647c23b892Sbalrog 11657c23b892Sbalrog static void 11667c23b892Sbalrog set_ims(E1000State *s, int index, uint32_t val) 11677c23b892Sbalrog { 11687c23b892Sbalrog s->mac_reg[IMS] |= val; 11697c23b892Sbalrog set_ics(s, 0, 0); 11707c23b892Sbalrog } 11717c23b892Sbalrog 11727c23b892Sbalrog #define getreg(x) [x] = mac_readreg 11737c23b892Sbalrog static uint32_t (*macreg_readops[])(E1000State *, int) = { 11747c23b892Sbalrog getreg(PBA), getreg(RCTL), getreg(TDH), getreg(TXDCTL), 11757c23b892Sbalrog getreg(WUFC), getreg(TDT), getreg(CTRL), getreg(LEDCTL), 11767c23b892Sbalrog getreg(MANC), getreg(MDIC), getreg(SWSM), getreg(STATUS), 11777c23b892Sbalrog getreg(TORL), getreg(TOTL), getreg(IMS), getreg(TCTL), 1178b1332393SBill Paul getreg(RDH), getreg(RDT), getreg(VET), getreg(ICS), 1179a00b2335SKay Ackermann getreg(TDBAL), getreg(TDBAH), getreg(RDBAH), getreg(RDBAL), 1180e9845f09SVincenzo Maffione getreg(TDLEN), getreg(RDLEN), getreg(RDTR), getreg(RADV), 1181e9845f09SVincenzo Maffione getreg(TADV), getreg(ITR), 11827c23b892Sbalrog 11837c23b892Sbalrog [TOTH] = mac_read_clr8, [TORH] = mac_read_clr8, [GPRC] = mac_read_clr4, 11847c23b892Sbalrog [GPTC] = mac_read_clr4, [TPR] = mac_read_clr4, [TPT] = mac_read_clr4, 11857c23b892Sbalrog [ICR] = mac_icr_read, [EECD] = get_eecd, [EERD] = flash_eerd_read, 11867c23b892Sbalrog [CRCERRS ... MPC] = &mac_readreg, 11877c23b892Sbalrog [RA ... RA+31] = &mac_readreg, 11887c23b892Sbalrog [MTA ... MTA+127] = &mac_readreg, 11898f2e8d1fSaliguori [VFTA ... VFTA+127] = &mac_readreg, 11907c23b892Sbalrog }; 1191b1503cdaSmalc enum { NREADOPS = ARRAY_SIZE(macreg_readops) }; 11927c23b892Sbalrog 11937c23b892Sbalrog #define putreg(x) [x] = mac_writereg 11947c23b892Sbalrog static void (*macreg_writeops[])(E1000State *, int, uint32_t) = { 11957c23b892Sbalrog putreg(PBA), putreg(EERD), putreg(SWSM), putreg(WUFC), 11967c23b892Sbalrog putreg(TDBAL), putreg(TDBAH), putreg(TXDCTL), putreg(RDBAH), 1197cab3c825SKevin Wolf putreg(RDBAL), putreg(LEDCTL), putreg(VET), 11987c23b892Sbalrog [TDLEN] = set_dlen, [RDLEN] = set_dlen, [TCTL] = set_tctl, 11997c23b892Sbalrog [TDT] = set_tctl, [MDIC] = set_mdic, [ICS] = set_ics, 12007c23b892Sbalrog [TDH] = set_16bit, [RDH] = set_16bit, [RDT] = set_rdt, 12017c23b892Sbalrog [IMC] = set_imc, [IMS] = set_ims, [ICR] = set_icr, 1202cab3c825SKevin Wolf [EECD] = set_eecd, [RCTL] = set_rx_control, [CTRL] = set_ctrl, 1203e9845f09SVincenzo Maffione [RDTR] = set_16bit, [RADV] = set_16bit, [TADV] = set_16bit, 1204e9845f09SVincenzo Maffione [ITR] = set_16bit, 12057c23b892Sbalrog [RA ... RA+31] = &mac_writereg, 12067c23b892Sbalrog [MTA ... MTA+127] = &mac_writereg, 12078f2e8d1fSaliguori [VFTA ... VFTA+127] = &mac_writereg, 12087c23b892Sbalrog }; 1209b9d03e35SJason Wang 1210b1503cdaSmalc enum { NWRITEOPS = ARRAY_SIZE(macreg_writeops) }; 12117c23b892Sbalrog 12127c23b892Sbalrog static void 1213a8170e5eSAvi Kivity e1000_mmio_write(void *opaque, hwaddr addr, uint64_t val, 1214ad00a9b9SAvi Kivity unsigned size) 12157c23b892Sbalrog { 12167c23b892Sbalrog E1000State *s = opaque; 12178da3ff18Spbrook unsigned int index = (addr & 0x1ffff) >> 2; 12187c23b892Sbalrog 121943ad7e3eSJes Sorensen if (index < NWRITEOPS && macreg_writeops[index]) { 12206b59fc74Saurel32 macreg_writeops[index](s, index, val); 122143ad7e3eSJes Sorensen } else if (index < NREADOPS && macreg_readops[index]) { 1222ad00a9b9SAvi Kivity DBGOUT(MMIO, "e1000_mmio_writel RO %x: 0x%04"PRIx64"\n", index<<2, val); 122343ad7e3eSJes Sorensen } else { 1224ad00a9b9SAvi Kivity DBGOUT(UNKNOWN, "MMIO unknown write addr=0x%08x,val=0x%08"PRIx64"\n", 12257c23b892Sbalrog index<<2, val); 12267c23b892Sbalrog } 122743ad7e3eSJes Sorensen } 12287c23b892Sbalrog 1229ad00a9b9SAvi Kivity static uint64_t 1230a8170e5eSAvi Kivity e1000_mmio_read(void *opaque, hwaddr addr, unsigned size) 12317c23b892Sbalrog { 12327c23b892Sbalrog E1000State *s = opaque; 12338da3ff18Spbrook unsigned int index = (addr & 0x1ffff) >> 2; 12347c23b892Sbalrog 12357c23b892Sbalrog if (index < NREADOPS && macreg_readops[index]) 12366b59fc74Saurel32 { 123732600a30SAlexander Graf return macreg_readops[index](s, index); 12386b59fc74Saurel32 } 12397c23b892Sbalrog DBGOUT(UNKNOWN, "MMIO unknown read addr=0x%08x\n", index<<2); 12407c23b892Sbalrog return 0; 12417c23b892Sbalrog } 12427c23b892Sbalrog 1243ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_mmio_ops = { 1244ad00a9b9SAvi Kivity .read = e1000_mmio_read, 1245ad00a9b9SAvi Kivity .write = e1000_mmio_write, 1246ad00a9b9SAvi Kivity .endianness = DEVICE_LITTLE_ENDIAN, 1247ad00a9b9SAvi Kivity .impl = { 1248ad00a9b9SAvi Kivity .min_access_size = 4, 1249ad00a9b9SAvi Kivity .max_access_size = 4, 1250ad00a9b9SAvi Kivity }, 1251ad00a9b9SAvi Kivity }; 1252ad00a9b9SAvi Kivity 1253a8170e5eSAvi Kivity static uint64_t e1000_io_read(void *opaque, hwaddr addr, 1254ad00a9b9SAvi Kivity unsigned size) 12557c23b892Sbalrog { 1256ad00a9b9SAvi Kivity E1000State *s = opaque; 1257ad00a9b9SAvi Kivity 1258ad00a9b9SAvi Kivity (void)s; 1259ad00a9b9SAvi Kivity return 0; 12607c23b892Sbalrog } 12617c23b892Sbalrog 1262a8170e5eSAvi Kivity static void e1000_io_write(void *opaque, hwaddr addr, 1263ad00a9b9SAvi Kivity uint64_t val, unsigned size) 12647c23b892Sbalrog { 1265ad00a9b9SAvi Kivity E1000State *s = opaque; 1266ad00a9b9SAvi Kivity 1267ad00a9b9SAvi Kivity (void)s; 12687c23b892Sbalrog } 12697c23b892Sbalrog 1270ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_io_ops = { 1271ad00a9b9SAvi Kivity .read = e1000_io_read, 1272ad00a9b9SAvi Kivity .write = e1000_io_write, 1273ad00a9b9SAvi Kivity .endianness = DEVICE_LITTLE_ENDIAN, 1274ad00a9b9SAvi Kivity }; 1275ad00a9b9SAvi Kivity 1276e482dc3eSJuan Quintela static bool is_version_1(void *opaque, int version_id) 12777c23b892Sbalrog { 1278e482dc3eSJuan Quintela return version_id == 1; 12797c23b892Sbalrog } 12807c23b892Sbalrog 1281ddcb73b7SMichael S. Tsirkin static void e1000_pre_save(void *opaque) 1282ddcb73b7SMichael S. Tsirkin { 1283ddcb73b7SMichael S. Tsirkin E1000State *s = opaque; 1284ddcb73b7SMichael S. Tsirkin NetClientState *nc = qemu_get_queue(s->nic); 12852af234e6SMichael S. Tsirkin 1286e9845f09SVincenzo Maffione /* If the mitigation timer is active, emulate a timeout now. */ 1287e9845f09SVincenzo Maffione if (s->mit_timer_on) { 1288e9845f09SVincenzo Maffione e1000_mit_timer(s); 1289e9845f09SVincenzo Maffione } 1290e9845f09SVincenzo Maffione 12912af234e6SMichael S. Tsirkin if (!(s->compat_flags & E1000_FLAG_AUTONEG)) { 12922af234e6SMichael S. Tsirkin return; 12932af234e6SMichael S. Tsirkin } 12942af234e6SMichael S. Tsirkin 1295ddcb73b7SMichael S. Tsirkin /* 1296ddcb73b7SMichael S. Tsirkin * If link is down and auto-negotiation is ongoing, complete 1297ddcb73b7SMichael S. Tsirkin * auto-negotiation immediately. This allows is to look at 1298ddcb73b7SMichael S. Tsirkin * MII_SR_AUTONEG_COMPLETE to infer link status on load. 1299ddcb73b7SMichael S. Tsirkin */ 1300ddcb73b7SMichael S. Tsirkin if (nc->link_down && 1301ddcb73b7SMichael S. Tsirkin s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN && 1302ddcb73b7SMichael S. Tsirkin s->phy_reg[PHY_CTRL] & MII_CR_RESTART_AUTO_NEG) { 1303ddcb73b7SMichael S. Tsirkin s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE; 1304ddcb73b7SMichael S. Tsirkin } 1305ddcb73b7SMichael S. Tsirkin } 1306ddcb73b7SMichael S. Tsirkin 1307e4b82364SAmos Kong static int e1000_post_load(void *opaque, int version_id) 1308e4b82364SAmos Kong { 1309e4b82364SAmos Kong E1000State *s = opaque; 1310b356f76dSJason Wang NetClientState *nc = qemu_get_queue(s->nic); 1311e4b82364SAmos Kong 1312e9845f09SVincenzo Maffione if (!(s->compat_flags & E1000_FLAG_MIT)) { 1313e9845f09SVincenzo Maffione s->mac_reg[ITR] = s->mac_reg[RDTR] = s->mac_reg[RADV] = 1314e9845f09SVincenzo Maffione s->mac_reg[TADV] = 0; 1315e9845f09SVincenzo Maffione s->mit_irq_level = false; 1316e9845f09SVincenzo Maffione } 1317e9845f09SVincenzo Maffione s->mit_ide = 0; 1318e9845f09SVincenzo Maffione s->mit_timer_on = false; 1319e9845f09SVincenzo Maffione 1320e4b82364SAmos Kong /* nc.link_down can't be migrated, so infer link_down according 1321ddcb73b7SMichael S. Tsirkin * to link status bit in mac_reg[STATUS]. 1322ddcb73b7SMichael S. Tsirkin * Alternatively, restart link negotiation if it was in progress. */ 1323b356f76dSJason Wang nc->link_down = (s->mac_reg[STATUS] & E1000_STATUS_LU) == 0; 13242af234e6SMichael S. Tsirkin 13252af234e6SMichael S. Tsirkin if (!(s->compat_flags & E1000_FLAG_AUTONEG)) { 13262af234e6SMichael S. Tsirkin return 0; 13272af234e6SMichael S. Tsirkin } 13282af234e6SMichael S. Tsirkin 1329ddcb73b7SMichael S. Tsirkin if (s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN && 1330ddcb73b7SMichael S. Tsirkin s->phy_reg[PHY_CTRL] & MII_CR_RESTART_AUTO_NEG && 1331ddcb73b7SMichael S. Tsirkin !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) { 1332ddcb73b7SMichael S. Tsirkin nc->link_down = false; 1333bc72ad67SAlex Bligh timer_mod(s->autoneg_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500); 1334ddcb73b7SMichael S. Tsirkin } 1335e4b82364SAmos Kong 1336e4b82364SAmos Kong return 0; 1337e4b82364SAmos Kong } 1338e4b82364SAmos Kong 1339e9845f09SVincenzo Maffione static bool e1000_mit_state_needed(void *opaque) 1340e9845f09SVincenzo Maffione { 1341e9845f09SVincenzo Maffione E1000State *s = opaque; 1342e9845f09SVincenzo Maffione 1343e9845f09SVincenzo Maffione return s->compat_flags & E1000_FLAG_MIT; 1344e9845f09SVincenzo Maffione } 1345e9845f09SVincenzo Maffione 1346e9845f09SVincenzo Maffione static const VMStateDescription vmstate_e1000_mit_state = { 1347e9845f09SVincenzo Maffione .name = "e1000/mit_state", 1348e9845f09SVincenzo Maffione .version_id = 1, 1349e9845f09SVincenzo Maffione .minimum_version_id = 1, 1350e9845f09SVincenzo Maffione .minimum_version_id_old = 1, 1351e9845f09SVincenzo Maffione .fields = (VMStateField[]) { 1352e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[RDTR], E1000State), 1353e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[RADV], E1000State), 1354e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[TADV], E1000State), 1355e9845f09SVincenzo Maffione VMSTATE_UINT32(mac_reg[ITR], E1000State), 1356e9845f09SVincenzo Maffione VMSTATE_BOOL(mit_irq_level, E1000State), 1357e9845f09SVincenzo Maffione VMSTATE_END_OF_LIST() 1358e9845f09SVincenzo Maffione } 1359e9845f09SVincenzo Maffione }; 1360e9845f09SVincenzo Maffione 1361e482dc3eSJuan Quintela static const VMStateDescription vmstate_e1000 = { 1362e482dc3eSJuan Quintela .name = "e1000", 1363e482dc3eSJuan Quintela .version_id = 2, 1364e482dc3eSJuan Quintela .minimum_version_id = 1, 1365e482dc3eSJuan Quintela .minimum_version_id_old = 1, 1366ddcb73b7SMichael S. Tsirkin .pre_save = e1000_pre_save, 1367e4b82364SAmos Kong .post_load = e1000_post_load, 1368e482dc3eSJuan Quintela .fields = (VMStateField []) { 1369b08340d5SAndreas Färber VMSTATE_PCI_DEVICE(parent_obj, E1000State), 1370e482dc3eSJuan Quintela VMSTATE_UNUSED_TEST(is_version_1, 4), /* was instance id */ 1371e482dc3eSJuan Quintela VMSTATE_UNUSED(4), /* Was mmio_base. */ 1372e482dc3eSJuan Quintela VMSTATE_UINT32(rxbuf_size, E1000State), 1373e482dc3eSJuan Quintela VMSTATE_UINT32(rxbuf_min_shift, E1000State), 1374e482dc3eSJuan Quintela VMSTATE_UINT32(eecd_state.val_in, E1000State), 1375e482dc3eSJuan Quintela VMSTATE_UINT16(eecd_state.bitnum_in, E1000State), 1376e482dc3eSJuan Quintela VMSTATE_UINT16(eecd_state.bitnum_out, E1000State), 1377e482dc3eSJuan Quintela VMSTATE_UINT16(eecd_state.reading, E1000State), 1378e482dc3eSJuan Quintela VMSTATE_UINT32(eecd_state.old_eecd, E1000State), 1379e482dc3eSJuan Quintela VMSTATE_UINT8(tx.ipcss, E1000State), 1380e482dc3eSJuan Quintela VMSTATE_UINT8(tx.ipcso, E1000State), 1381e482dc3eSJuan Quintela VMSTATE_UINT16(tx.ipcse, E1000State), 1382e482dc3eSJuan Quintela VMSTATE_UINT8(tx.tucss, E1000State), 1383e482dc3eSJuan Quintela VMSTATE_UINT8(tx.tucso, E1000State), 1384e482dc3eSJuan Quintela VMSTATE_UINT16(tx.tucse, E1000State), 1385e482dc3eSJuan Quintela VMSTATE_UINT32(tx.paylen, E1000State), 1386e482dc3eSJuan Quintela VMSTATE_UINT8(tx.hdr_len, E1000State), 1387e482dc3eSJuan Quintela VMSTATE_UINT16(tx.mss, E1000State), 1388e482dc3eSJuan Quintela VMSTATE_UINT16(tx.size, E1000State), 1389e482dc3eSJuan Quintela VMSTATE_UINT16(tx.tso_frames, E1000State), 1390e482dc3eSJuan Quintela VMSTATE_UINT8(tx.sum_needed, E1000State), 1391e482dc3eSJuan Quintela VMSTATE_INT8(tx.ip, E1000State), 1392e482dc3eSJuan Quintela VMSTATE_INT8(tx.tcp, E1000State), 1393e482dc3eSJuan Quintela VMSTATE_BUFFER(tx.header, E1000State), 1394e482dc3eSJuan Quintela VMSTATE_BUFFER(tx.data, E1000State), 1395e482dc3eSJuan Quintela VMSTATE_UINT16_ARRAY(eeprom_data, E1000State, 64), 1396e482dc3eSJuan Quintela VMSTATE_UINT16_ARRAY(phy_reg, E1000State, 0x20), 1397e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[CTRL], E1000State), 1398e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[EECD], E1000State), 1399e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[EERD], E1000State), 1400e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[GPRC], E1000State), 1401e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[GPTC], E1000State), 1402e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[ICR], E1000State), 1403e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[ICS], E1000State), 1404e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[IMC], E1000State), 1405e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[IMS], E1000State), 1406e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[LEDCTL], E1000State), 1407e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[MANC], E1000State), 1408e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[MDIC], E1000State), 1409e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[MPC], E1000State), 1410e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[PBA], E1000State), 1411e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RCTL], E1000State), 1412e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDBAH], E1000State), 1413e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDBAL], E1000State), 1414e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDH], E1000State), 1415e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDLEN], E1000State), 1416e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[RDT], E1000State), 1417e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[STATUS], E1000State), 1418e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[SWSM], E1000State), 1419e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TCTL], E1000State), 1420e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDBAH], E1000State), 1421e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDBAL], E1000State), 1422e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDH], E1000State), 1423e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDLEN], E1000State), 1424e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TDT], E1000State), 1425e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TORH], E1000State), 1426e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TORL], E1000State), 1427e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TOTH], E1000State), 1428e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TOTL], E1000State), 1429e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TPR], E1000State), 1430e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TPT], E1000State), 1431e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[TXDCTL], E1000State), 1432e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[WUFC], E1000State), 1433e482dc3eSJuan Quintela VMSTATE_UINT32(mac_reg[VET], E1000State), 1434e482dc3eSJuan Quintela VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, RA, 32), 1435e482dc3eSJuan Quintela VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, MTA, 128), 1436e482dc3eSJuan Quintela VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, VFTA, 128), 1437e482dc3eSJuan Quintela VMSTATE_END_OF_LIST() 1438e9845f09SVincenzo Maffione }, 1439e9845f09SVincenzo Maffione .subsections = (VMStateSubsection[]) { 1440e9845f09SVincenzo Maffione { 1441e9845f09SVincenzo Maffione .vmsd = &vmstate_e1000_mit_state, 1442e9845f09SVincenzo Maffione .needed = e1000_mit_state_needed, 1443e9845f09SVincenzo Maffione }, { 1444e9845f09SVincenzo Maffione /* empty */ 1445e9845f09SVincenzo Maffione } 14467c23b892Sbalrog } 1447e482dc3eSJuan Quintela }; 14487c23b892Sbalrog 1449*8597f2e1SGabriel L. Somlo /* 1450*8597f2e1SGabriel L. Somlo * EEPROM contents documented in Tables 5-2 and 5-3, pp. 98-102. 1451*8597f2e1SGabriel L. Somlo * Note: A valid DevId will be inserted during pci_e1000_init(). 1452*8597f2e1SGabriel L. Somlo */ 145388b4e9dbSblueswir1 static const uint16_t e1000_eeprom_template[64] = { 14547c23b892Sbalrog 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0x0000, 1455*8597f2e1SGabriel L. Somlo 0x3000, 0x1000, 0x6403, 0 /*DevId*/, 0x8086, 0 /*DevId*/, 0x8086, 0x3040, 14567c23b892Sbalrog 0x0008, 0x2000, 0x7e14, 0x0048, 0x1000, 0x00d8, 0x0000, 0x2700, 14577c23b892Sbalrog 0x6cc9, 0x3150, 0x0722, 0x040b, 0x0984, 0x0000, 0xc000, 0x0706, 14587c23b892Sbalrog 0x1008, 0x0000, 0x0f04, 0x7fff, 0x4d01, 0xffff, 0xffff, 0xffff, 14597c23b892Sbalrog 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 14607c23b892Sbalrog 0x0100, 0x4000, 0x121c, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 14617c23b892Sbalrog 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x0000, 14627c23b892Sbalrog }; 14637c23b892Sbalrog 14647c23b892Sbalrog /* PCI interface */ 14657c23b892Sbalrog 14667c23b892Sbalrog static void 1467ad00a9b9SAvi Kivity e1000_mmio_setup(E1000State *d) 14687c23b892Sbalrog { 1469f65ed4c1Saliguori int i; 1470f65ed4c1Saliguori const uint32_t excluded_regs[] = { 1471f65ed4c1Saliguori E1000_MDIC, E1000_ICR, E1000_ICS, E1000_IMS, 1472f65ed4c1Saliguori E1000_IMC, E1000_TCTL, E1000_TDT, PNPMMIO_SIZE 1473f65ed4c1Saliguori }; 1474f65ed4c1Saliguori 1475eedfac6fSPaolo Bonzini memory_region_init_io(&d->mmio, OBJECT(d), &e1000_mmio_ops, d, 1476eedfac6fSPaolo Bonzini "e1000-mmio", PNPMMIO_SIZE); 1477ad00a9b9SAvi Kivity memory_region_add_coalescing(&d->mmio, 0, excluded_regs[0]); 1478f65ed4c1Saliguori for (i = 0; excluded_regs[i] != PNPMMIO_SIZE; i++) 1479ad00a9b9SAvi Kivity memory_region_add_coalescing(&d->mmio, excluded_regs[i] + 4, 1480ad00a9b9SAvi Kivity excluded_regs[i+1] - excluded_regs[i] - 4); 1481eedfac6fSPaolo Bonzini memory_region_init_io(&d->io, OBJECT(d), &e1000_io_ops, d, "e1000-io", IOPORT_SIZE); 14827c23b892Sbalrog } 14837c23b892Sbalrog 1484b946a153Saliguori static void 14854e68f7a0SStefan Hajnoczi e1000_cleanup(NetClientState *nc) 1486b946a153Saliguori { 1487cc1f0f45SJason Wang E1000State *s = qemu_get_nic_opaque(nc); 1488b946a153Saliguori 1489a03e2aecSMark McLoughlin s->nic = NULL; 1490b946a153Saliguori } 1491b946a153Saliguori 1492f90c2bcdSAlex Williamson static void 14934b09be85Saliguori pci_e1000_uninit(PCIDevice *dev) 14944b09be85Saliguori { 1495567a3c9eSPeter Crosthwaite E1000State *d = E1000(dev); 14964b09be85Saliguori 1497bc72ad67SAlex Bligh timer_del(d->autoneg_timer); 1498bc72ad67SAlex Bligh timer_free(d->autoneg_timer); 1499e9845f09SVincenzo Maffione timer_del(d->mit_timer); 1500e9845f09SVincenzo Maffione timer_free(d->mit_timer); 1501ad00a9b9SAvi Kivity memory_region_destroy(&d->mmio); 1502ad00a9b9SAvi Kivity memory_region_destroy(&d->io); 1503948ecf21SJason Wang qemu_del_nic(d->nic); 15044b09be85Saliguori } 15054b09be85Saliguori 1506a03e2aecSMark McLoughlin static NetClientInfo net_e1000_info = { 15072be64a68SLaszlo Ersek .type = NET_CLIENT_OPTIONS_KIND_NIC, 1508a03e2aecSMark McLoughlin .size = sizeof(NICState), 1509a03e2aecSMark McLoughlin .can_receive = e1000_can_receive, 1510a03e2aecSMark McLoughlin .receive = e1000_receive, 151197410ddeSVincenzo Maffione .receive_iov = e1000_receive_iov, 1512a03e2aecSMark McLoughlin .cleanup = e1000_cleanup, 1513a03e2aecSMark McLoughlin .link_status_changed = e1000_set_link_status, 1514a03e2aecSMark McLoughlin }; 1515a03e2aecSMark McLoughlin 151681a322d4SGerd Hoffmann static int pci_e1000_init(PCIDevice *pci_dev) 15177c23b892Sbalrog { 1518567a3c9eSPeter Crosthwaite DeviceState *dev = DEVICE(pci_dev); 1519567a3c9eSPeter Crosthwaite E1000State *d = E1000(pci_dev); 1520*8597f2e1SGabriel L. Somlo PCIDeviceClass *pdc = PCI_DEVICE_GET_CLASS(pci_dev); 15217c23b892Sbalrog uint8_t *pci_conf; 15227c23b892Sbalrog uint16_t checksum = 0; 15237c23b892Sbalrog int i; 1524fbdaa002SGerd Hoffmann uint8_t *macaddr; 1525aff427a1SChris Wright 1526b08340d5SAndreas Färber pci_conf = pci_dev->config; 15277c23b892Sbalrog 1528a9cbacb0SMichael S. Tsirkin /* TODO: RST# value should be 0, PCI spec 6.2.4 */ 1529a9cbacb0SMichael S. Tsirkin pci_conf[PCI_CACHE_LINE_SIZE] = 0x10; 15307c23b892Sbalrog 1531817e0b6fSMichael S. Tsirkin pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */ 15327c23b892Sbalrog 1533ad00a9b9SAvi Kivity e1000_mmio_setup(d); 15347c23b892Sbalrog 1535b08340d5SAndreas Färber pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio); 15367c23b892Sbalrog 1537b08340d5SAndreas Färber pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->io); 15387c23b892Sbalrog 15397c23b892Sbalrog memmove(d->eeprom_data, e1000_eeprom_template, 15407c23b892Sbalrog sizeof e1000_eeprom_template); 1541fbdaa002SGerd Hoffmann qemu_macaddr_default_if_unset(&d->conf.macaddr); 1542fbdaa002SGerd Hoffmann macaddr = d->conf.macaddr.a; 15437c23b892Sbalrog for (i = 0; i < 3; i++) 15449d07d757SPaul Brook d->eeprom_data[i] = (macaddr[2*i+1]<<8) | macaddr[2*i]; 1545*8597f2e1SGabriel L. Somlo d->eeprom_data[11] = d->eeprom_data[13] = pdc->device_id; 15467c23b892Sbalrog for (i = 0; i < EEPROM_CHECKSUM_REG; i++) 15477c23b892Sbalrog checksum += d->eeprom_data[i]; 15487c23b892Sbalrog checksum = (uint16_t) EEPROM_SUM - checksum; 15497c23b892Sbalrog d->eeprom_data[EEPROM_CHECKSUM_REG] = checksum; 15507c23b892Sbalrog 1551a03e2aecSMark McLoughlin d->nic = qemu_new_nic(&net_e1000_info, &d->conf, 1552567a3c9eSPeter Crosthwaite object_get_typename(OBJECT(d)), dev->id, d); 15537c23b892Sbalrog 1554b356f76dSJason Wang qemu_format_nic_info_str(qemu_get_queue(d->nic), macaddr); 15551ca4d09aSGleb Natapov 1556567a3c9eSPeter Crosthwaite add_boot_device_path(d->conf.bootindex, dev, "/ethernet-phy@0"); 15571ca4d09aSGleb Natapov 1558bc72ad67SAlex Bligh d->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, e1000_autoneg_timer, d); 1559e9845f09SVincenzo Maffione d->mit_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000_mit_timer, d); 1560b9d03e35SJason Wang 156181a322d4SGerd Hoffmann return 0; 15627c23b892Sbalrog } 15639d07d757SPaul Brook 1564fbdaa002SGerd Hoffmann static void qdev_e1000_reset(DeviceState *dev) 1565fbdaa002SGerd Hoffmann { 1566567a3c9eSPeter Crosthwaite E1000State *d = E1000(dev); 1567fbdaa002SGerd Hoffmann e1000_reset(d); 1568fbdaa002SGerd Hoffmann } 1569fbdaa002SGerd Hoffmann 157040021f08SAnthony Liguori static Property e1000_properties[] = { 1571fbdaa002SGerd Hoffmann DEFINE_NIC_PROPERTIES(E1000State, conf), 15722af234e6SMichael S. Tsirkin DEFINE_PROP_BIT("autonegotiation", E1000State, 15732af234e6SMichael S. Tsirkin compat_flags, E1000_FLAG_AUTONEG_BIT, true), 1574e9845f09SVincenzo Maffione DEFINE_PROP_BIT("mitigation", E1000State, 1575e9845f09SVincenzo Maffione compat_flags, E1000_FLAG_MIT_BIT, true), 1576fbdaa002SGerd Hoffmann DEFINE_PROP_END_OF_LIST(), 157740021f08SAnthony Liguori }; 157840021f08SAnthony Liguori 1579*8597f2e1SGabriel L. Somlo typedef struct E1000Info { 1580*8597f2e1SGabriel L. Somlo const char *name; 1581*8597f2e1SGabriel L. Somlo uint16_t device_id; 1582*8597f2e1SGabriel L. Somlo uint8_t revision; 1583*8597f2e1SGabriel L. Somlo uint16_t phy_id2; 1584*8597f2e1SGabriel L. Somlo bool is_8257xx; 1585*8597f2e1SGabriel L. Somlo } E1000Info; 1586*8597f2e1SGabriel L. Somlo 158740021f08SAnthony Liguori static void e1000_class_init(ObjectClass *klass, void *data) 158840021f08SAnthony Liguori { 158939bffca2SAnthony Liguori DeviceClass *dc = DEVICE_CLASS(klass); 159040021f08SAnthony Liguori PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 1591*8597f2e1SGabriel L. Somlo E1000BaseClass *e = E1000_DEVICE_CLASS(klass); 1592*8597f2e1SGabriel L. Somlo const E1000Info *info = data; 159340021f08SAnthony Liguori 159440021f08SAnthony Liguori k->init = pci_e1000_init; 159540021f08SAnthony Liguori k->exit = pci_e1000_uninit; 1596c45e5b5bSGerd Hoffmann k->romfile = "efi-e1000.rom"; 159740021f08SAnthony Liguori k->vendor_id = PCI_VENDOR_ID_INTEL; 1598*8597f2e1SGabriel L. Somlo k->device_id = info->device_id; 1599*8597f2e1SGabriel L. Somlo k->revision = info->revision; 1600*8597f2e1SGabriel L. Somlo e->phy_id2 = info->phy_id2; 1601*8597f2e1SGabriel L. Somlo e->is_8257xx = info->is_8257xx; 160240021f08SAnthony Liguori k->class_id = PCI_CLASS_NETWORK_ETHERNET; 1603125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 160439bffca2SAnthony Liguori dc->desc = "Intel Gigabit Ethernet"; 160539bffca2SAnthony Liguori dc->reset = qdev_e1000_reset; 160639bffca2SAnthony Liguori dc->vmsd = &vmstate_e1000; 160739bffca2SAnthony Liguori dc->props = e1000_properties; 1608fbdaa002SGerd Hoffmann } 160940021f08SAnthony Liguori 1610*8597f2e1SGabriel L. Somlo static const TypeInfo e1000_base_info = { 1611*8597f2e1SGabriel L. Somlo .name = TYPE_E1000_BASE, 161239bffca2SAnthony Liguori .parent = TYPE_PCI_DEVICE, 161339bffca2SAnthony Liguori .instance_size = sizeof(E1000State), 1614*8597f2e1SGabriel L. Somlo .class_size = sizeof(E1000BaseClass), 1615*8597f2e1SGabriel L. Somlo .abstract = true, 1616*8597f2e1SGabriel L. Somlo }; 1617*8597f2e1SGabriel L. Somlo 1618*8597f2e1SGabriel L. Somlo static const E1000Info e1000_devices[] = { 1619*8597f2e1SGabriel L. Somlo { 1620*8597f2e1SGabriel L. Somlo .name = "e1000-82540em", 1621*8597f2e1SGabriel L. Somlo .device_id = E1000_DEV_ID_82540EM, 1622*8597f2e1SGabriel L. Somlo .revision = 0x03, 1623*8597f2e1SGabriel L. Somlo .phy_id2 = E1000_PHY_ID2_8254xx_DEFAULT, 1624*8597f2e1SGabriel L. Somlo }, 1625*8597f2e1SGabriel L. Somlo { 1626*8597f2e1SGabriel L. Somlo .name = "e1000-82544gc", 1627*8597f2e1SGabriel L. Somlo .device_id = E1000_DEV_ID_82544GC_COPPER, 1628*8597f2e1SGabriel L. Somlo .revision = 0x03, 1629*8597f2e1SGabriel L. Somlo .phy_id2 = E1000_PHY_ID2_82544x, 1630*8597f2e1SGabriel L. Somlo }, 1631*8597f2e1SGabriel L. Somlo { 1632*8597f2e1SGabriel L. Somlo .name = "e1000-82545em", 1633*8597f2e1SGabriel L. Somlo .device_id = E1000_DEV_ID_82545EM_COPPER, 1634*8597f2e1SGabriel L. Somlo .revision = 0x03, 1635*8597f2e1SGabriel L. Somlo .phy_id2 = E1000_PHY_ID2_8254xx_DEFAULT, 1636*8597f2e1SGabriel L. Somlo }, 1637*8597f2e1SGabriel L. Somlo { 1638*8597f2e1SGabriel L. Somlo .name = "e1000-82573l", 1639*8597f2e1SGabriel L. Somlo .device_id = E1000_DEV_ID_82573L, 1640*8597f2e1SGabriel L. Somlo .revision = 0x03, 1641*8597f2e1SGabriel L. Somlo .phy_id2 = E1000_PHY_ID2_82573x, 1642*8597f2e1SGabriel L. Somlo .is_8257xx = true, 1643*8597f2e1SGabriel L. Somlo }, 1644*8597f2e1SGabriel L. Somlo }; 1645*8597f2e1SGabriel L. Somlo 1646*8597f2e1SGabriel L. Somlo static const TypeInfo e1000_default_info = { 1647*8597f2e1SGabriel L. Somlo .name = "e1000", 1648*8597f2e1SGabriel L. Somlo .parent = "e1000-82540em", 16490aab0d3aSGerd Hoffmann }; 16500aab0d3aSGerd Hoffmann 165183f7d43aSAndreas Färber static void e1000_register_types(void) 16529d07d757SPaul Brook { 1653*8597f2e1SGabriel L. Somlo int i; 1654*8597f2e1SGabriel L. Somlo 1655*8597f2e1SGabriel L. Somlo type_register_static(&e1000_base_info); 1656*8597f2e1SGabriel L. Somlo for (i = 0; i < ARRAY_SIZE(e1000_devices); i++) { 1657*8597f2e1SGabriel L. Somlo const E1000Info *info = &e1000_devices[i]; 1658*8597f2e1SGabriel L. Somlo TypeInfo type_info = {}; 1659*8597f2e1SGabriel L. Somlo 1660*8597f2e1SGabriel L. Somlo type_info.name = info->name; 1661*8597f2e1SGabriel L. Somlo type_info.parent = TYPE_E1000_BASE; 1662*8597f2e1SGabriel L. Somlo type_info.class_data = (void *)info; 1663*8597f2e1SGabriel L. Somlo type_info.class_init = e1000_class_init; 1664*8597f2e1SGabriel L. Somlo 1665*8597f2e1SGabriel L. Somlo type_register(&type_info); 1666*8597f2e1SGabriel L. Somlo } 1667*8597f2e1SGabriel L. Somlo type_register_static(&e1000_default_info); 16689d07d757SPaul Brook } 16699d07d757SPaul Brook 167083f7d43aSAndreas Färber type_init(e1000_register_types) 1671