xref: /qemu/hw/net/e1000.c (revision bc0f0674f037a01f2ce0870ad6270a356a7a8347)
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"
3620302e71SMichael S. Tsirkin #include "qemu/range.h"
377c23b892Sbalrog 
3847b43a1fSPaolo Bonzini #include "e1000_regs.h"
397c23b892Sbalrog 
4027124888SJes Sorensen #define E1000_DEBUG
417c23b892Sbalrog 
4227124888SJes Sorensen #ifdef E1000_DEBUG
437c23b892Sbalrog enum {
447c23b892Sbalrog     DEBUG_GENERAL,      DEBUG_IO,       DEBUG_MMIO,     DEBUG_INTERRUPT,
457c23b892Sbalrog     DEBUG_RX,           DEBUG_TX,       DEBUG_MDIC,     DEBUG_EEPROM,
467c23b892Sbalrog     DEBUG_UNKNOWN,      DEBUG_TXSUM,    DEBUG_TXERR,    DEBUG_RXERR,
47f9c1cdf4SJason Wang     DEBUG_RXFILTER,     DEBUG_PHY,      DEBUG_NOTYET,
487c23b892Sbalrog };
497c23b892Sbalrog #define DBGBIT(x)    (1<<DEBUG_##x)
507c23b892Sbalrog static int debugflags = DBGBIT(TXERR) | DBGBIT(GENERAL);
517c23b892Sbalrog 
526c7f4b47SBlue Swirl #define DBGOUT(what, fmt, ...) do { \
537c23b892Sbalrog     if (debugflags & DBGBIT(what)) \
546c7f4b47SBlue Swirl         fprintf(stderr, "e1000: " fmt, ## __VA_ARGS__); \
557c23b892Sbalrog     } while (0)
567c23b892Sbalrog #else
576c7f4b47SBlue Swirl #define DBGOUT(what, fmt, ...) do {} while (0)
587c23b892Sbalrog #endif
597c23b892Sbalrog 
607c23b892Sbalrog #define IOPORT_SIZE       0x40
61e94bbefeSaurel32 #define PNPMMIO_SIZE      0x20000
6278aeb23eSStefan Hajnoczi #define MIN_BUF_SIZE      60 /* Min. octets in an ethernet frame sans FCS */
637c23b892Sbalrog 
64b0d9ffcdSMichael Contreras /* this is the size past which hardware will drop packets when setting LPE=0 */
65b0d9ffcdSMichael Contreras #define MAXIMUM_ETHERNET_VLAN_SIZE 1522
662c0331f4SMichael Contreras /* this is the size past which hardware will drop packets when setting LPE=1 */
672c0331f4SMichael Contreras #define MAXIMUM_ETHERNET_LPE_SIZE 16384
68b0d9ffcdSMichael Contreras 
6997410ddeSVincenzo Maffione #define MAXIMUM_ETHERNET_HDR_LEN (14+4)
7097410ddeSVincenzo Maffione 
717c23b892Sbalrog /*
727c23b892Sbalrog  * HW models:
738597f2e1SGabriel L. Somlo  *  E1000_DEV_ID_82540EM works with Windows, Linux, and OS X <= 10.8
747c23b892Sbalrog  *  E1000_DEV_ID_82544GC_COPPER appears to work; not well tested
758597f2e1SGabriel L. Somlo  *  E1000_DEV_ID_82545EM_COPPER works with Linux and OS X >= 10.6
767c23b892Sbalrog  *  Others never tested
777c23b892Sbalrog  */
787c23b892Sbalrog 
797c23b892Sbalrog typedef struct E1000State_st {
80b08340d5SAndreas Färber     /*< private >*/
81b08340d5SAndreas Färber     PCIDevice parent_obj;
82b08340d5SAndreas Färber     /*< public >*/
83b08340d5SAndreas Färber 
84a03e2aecSMark McLoughlin     NICState *nic;
85fbdaa002SGerd Hoffmann     NICConf conf;
86ad00a9b9SAvi Kivity     MemoryRegion mmio;
87ad00a9b9SAvi Kivity     MemoryRegion io;
887c23b892Sbalrog 
897c23b892Sbalrog     uint32_t mac_reg[0x8000];
907c23b892Sbalrog     uint16_t phy_reg[0x20];
917c23b892Sbalrog     uint16_t eeprom_data[64];
927c23b892Sbalrog 
937c23b892Sbalrog     uint32_t rxbuf_size;
947c23b892Sbalrog     uint32_t rxbuf_min_shift;
957c23b892Sbalrog     struct e1000_tx {
967c23b892Sbalrog         unsigned char header[256];
978f2e8d1fSaliguori         unsigned char vlan_header[4];
98b10fec9bSStefan Weil         /* Fields vlan and data must not be reordered or separated. */
998f2e8d1fSaliguori         unsigned char vlan[4];
1007c23b892Sbalrog         unsigned char data[0x10000];
1017c23b892Sbalrog         uint16_t size;
1027c23b892Sbalrog         unsigned char sum_needed;
1038f2e8d1fSaliguori         unsigned char vlan_needed;
1047c23b892Sbalrog         uint8_t ipcss;
1057c23b892Sbalrog         uint8_t ipcso;
1067c23b892Sbalrog         uint16_t ipcse;
1077c23b892Sbalrog         uint8_t tucss;
1087c23b892Sbalrog         uint8_t tucso;
1097c23b892Sbalrog         uint16_t tucse;
1107c23b892Sbalrog         uint8_t hdr_len;
1117c23b892Sbalrog         uint16_t mss;
1127c23b892Sbalrog         uint32_t paylen;
1137c23b892Sbalrog         uint16_t tso_frames;
1147c23b892Sbalrog         char tse;
115b6c4f71fSblueswir1         int8_t ip;
116b6c4f71fSblueswir1         int8_t tcp;
1171b0009dbSbalrog         char cptse;     // current packet tse bit
1187c23b892Sbalrog     } tx;
1197c23b892Sbalrog 
1207c23b892Sbalrog     struct {
12120f3e863SLeonid Bloch         uint32_t val_in;    /* shifted in from guest driver */
1227c23b892Sbalrog         uint16_t bitnum_in;
1237c23b892Sbalrog         uint16_t bitnum_out;
1247c23b892Sbalrog         uint16_t reading;
1257c23b892Sbalrog         uint32_t old_eecd;
1267c23b892Sbalrog     } eecd_state;
127b9d03e35SJason Wang 
128b9d03e35SJason Wang     QEMUTimer *autoneg_timer;
1292af234e6SMichael S. Tsirkin 
130e9845f09SVincenzo Maffione     QEMUTimer *mit_timer;      /* Mitigation timer. */
131e9845f09SVincenzo Maffione     bool mit_timer_on;         /* Mitigation timer is running. */
132e9845f09SVincenzo Maffione     bool mit_irq_level;        /* Tracks interrupt pin level. */
133e9845f09SVincenzo Maffione     uint32_t mit_ide;          /* Tracks E1000_TXD_CMD_IDE bit. */
134e9845f09SVincenzo Maffione 
1352af234e6SMichael S. Tsirkin /* Compatibility flags for migration to/from qemu 1.3.0 and older */
1362af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG_BIT 0
137e9845f09SVincenzo Maffione #define E1000_FLAG_MIT_BIT 1
1389e117734SLeonid Bloch #define E1000_FLAG_MAC_BIT 2
1392af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG (1 << E1000_FLAG_AUTONEG_BIT)
140e9845f09SVincenzo Maffione #define E1000_FLAG_MIT (1 << E1000_FLAG_MIT_BIT)
1419e117734SLeonid Bloch #define E1000_FLAG_MAC (1 << E1000_FLAG_MAC_BIT)
1422af234e6SMichael S. Tsirkin     uint32_t compat_flags;
1437c23b892Sbalrog } E1000State;
1447c23b892Sbalrog 
145*bc0f0674SLeonid Bloch #define chkflag(x)     (s->compat_flags & E1000_FLAG_##x)
146*bc0f0674SLeonid Bloch 
1478597f2e1SGabriel L. Somlo typedef struct E1000BaseClass {
1488597f2e1SGabriel L. Somlo     PCIDeviceClass parent_class;
1498597f2e1SGabriel L. Somlo     uint16_t phy_id2;
1508597f2e1SGabriel L. Somlo } E1000BaseClass;
1518597f2e1SGabriel L. Somlo 
1528597f2e1SGabriel L. Somlo #define TYPE_E1000_BASE "e1000-base"
153567a3c9eSPeter Crosthwaite 
154567a3c9eSPeter Crosthwaite #define E1000(obj) \
1558597f2e1SGabriel L. Somlo     OBJECT_CHECK(E1000State, (obj), TYPE_E1000_BASE)
1568597f2e1SGabriel L. Somlo 
1578597f2e1SGabriel L. Somlo #define E1000_DEVICE_CLASS(klass) \
1588597f2e1SGabriel L. Somlo      OBJECT_CLASS_CHECK(E1000BaseClass, (klass), TYPE_E1000_BASE)
1598597f2e1SGabriel L. Somlo #define E1000_DEVICE_GET_CLASS(obj) \
1608597f2e1SGabriel L. Somlo     OBJECT_GET_CLASS(E1000BaseClass, (obj), TYPE_E1000_BASE)
161567a3c9eSPeter Crosthwaite 
1627c23b892Sbalrog #define defreg(x)    x = (E1000_##x>>2)
1637c23b892Sbalrog enum {
1647c23b892Sbalrog     defreg(CTRL),    defreg(EECD),    defreg(EERD),    defreg(GPRC),
1657c23b892Sbalrog     defreg(GPTC),    defreg(ICR),     defreg(ICS),     defreg(IMC),
1667c23b892Sbalrog     defreg(IMS),     defreg(LEDCTL),  defreg(MANC),    defreg(MDIC),
1677c23b892Sbalrog     defreg(MPC),     defreg(PBA),     defreg(RCTL),    defreg(RDBAH),
1687c23b892Sbalrog     defreg(RDBAL),   defreg(RDH),     defreg(RDLEN),   defreg(RDT),
1697c23b892Sbalrog     defreg(STATUS),  defreg(SWSM),    defreg(TCTL),    defreg(TDBAH),
1707c23b892Sbalrog     defreg(TDBAL),   defreg(TDH),     defreg(TDLEN),   defreg(TDT),
1717c23b892Sbalrog     defreg(TORH),    defreg(TORL),    defreg(TOTH),    defreg(TOTL),
1727c23b892Sbalrog     defreg(TPR),     defreg(TPT),     defreg(TXDCTL),  defreg(WUFC),
1738f2e8d1fSaliguori     defreg(RA),      defreg(MTA),     defreg(CRCERRS), defreg(VFTA),
174e9845f09SVincenzo Maffione     defreg(VET),     defreg(RDTR),    defreg(RADV),    defreg(TADV),
175e9845f09SVincenzo Maffione     defreg(ITR),
1767c23b892Sbalrog };
1777c23b892Sbalrog 
17871aadd3cSJason Wang static void
17971aadd3cSJason Wang e1000_link_down(E1000State *s)
18071aadd3cSJason Wang {
18171aadd3cSJason Wang     s->mac_reg[STATUS] &= ~E1000_STATUS_LU;
18271aadd3cSJason Wang     s->phy_reg[PHY_STATUS] &= ~MII_SR_LINK_STATUS;
1836a2acedbSGabriel L. Somlo     s->phy_reg[PHY_STATUS] &= ~MII_SR_AUTONEG_COMPLETE;
1846883b591SGabriel L. Somlo     s->phy_reg[PHY_LP_ABILITY] &= ~MII_LPAR_LPACK;
18571aadd3cSJason Wang }
18671aadd3cSJason Wang 
18771aadd3cSJason Wang static void
18871aadd3cSJason Wang e1000_link_up(E1000State *s)
18971aadd3cSJason Wang {
19071aadd3cSJason Wang     s->mac_reg[STATUS] |= E1000_STATUS_LU;
19171aadd3cSJason Wang     s->phy_reg[PHY_STATUS] |= MII_SR_LINK_STATUS;
1925df6a185SStefan Hajnoczi 
1935df6a185SStefan Hajnoczi     /* E1000_STATUS_LU is tested by e1000_can_receive() */
1945df6a185SStefan Hajnoczi     qemu_flush_queued_packets(qemu_get_queue(s->nic));
19571aadd3cSJason Wang }
19671aadd3cSJason Wang 
1971195fed9SGabriel L. Somlo static bool
1981195fed9SGabriel L. Somlo have_autoneg(E1000State *s)
1991195fed9SGabriel L. Somlo {
200*bc0f0674SLeonid Bloch     return chkflag(AUTONEG) && (s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN);
2011195fed9SGabriel L. Somlo }
2021195fed9SGabriel L. Somlo 
203b9d03e35SJason Wang static void
204b9d03e35SJason Wang set_phy_ctrl(E1000State *s, int index, uint16_t val)
205b9d03e35SJason Wang {
2061195fed9SGabriel L. Somlo     /* bits 0-5 reserved; MII_CR_[RESTART_AUTO_NEG,RESET] are self clearing */
2071195fed9SGabriel L. Somlo     s->phy_reg[PHY_CTRL] = val & ~(0x3f |
2081195fed9SGabriel L. Somlo                                    MII_CR_RESET |
2091195fed9SGabriel L. Somlo                                    MII_CR_RESTART_AUTO_NEG);
2101195fed9SGabriel L. Somlo 
2112af234e6SMichael S. Tsirkin     /*
2122af234e6SMichael S. Tsirkin      * QEMU 1.3 does not support link auto-negotiation emulation, so if we
2132af234e6SMichael S. Tsirkin      * migrate during auto negotiation, after migration the link will be
2142af234e6SMichael S. Tsirkin      * down.
2152af234e6SMichael S. Tsirkin      */
2161195fed9SGabriel L. Somlo     if (have_autoneg(s) && (val & MII_CR_RESTART_AUTO_NEG)) {
217b9d03e35SJason Wang         e1000_link_down(s);
218b9d03e35SJason Wang         DBGOUT(PHY, "Start link auto negotiation\n");
2191195fed9SGabriel L. Somlo         timer_mod(s->autoneg_timer,
2201195fed9SGabriel L. Somlo                   qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
221b9d03e35SJason Wang     }
222b9d03e35SJason Wang }
223b9d03e35SJason Wang 
224b9d03e35SJason Wang static void (*phyreg_writeops[])(E1000State *, int, uint16_t) = {
225b9d03e35SJason Wang     [PHY_CTRL] = set_phy_ctrl,
226b9d03e35SJason Wang };
227b9d03e35SJason Wang 
228b9d03e35SJason Wang enum { NPHYWRITEOPS = ARRAY_SIZE(phyreg_writeops) };
229b9d03e35SJason Wang 
2307c23b892Sbalrog enum { PHY_R = 1, PHY_W = 2, PHY_RW = PHY_R | PHY_W };
23188b4e9dbSblueswir1 static const char phy_regcap[0x20] = {
2327c23b892Sbalrog     [PHY_STATUS]      = PHY_R,     [M88E1000_EXT_PHY_SPEC_CTRL] = PHY_RW,
2337c23b892Sbalrog     [PHY_ID1]         = PHY_R,     [M88E1000_PHY_SPEC_CTRL]     = PHY_RW,
2347c23b892Sbalrog     [PHY_CTRL]        = PHY_RW,    [PHY_1000T_CTRL]             = PHY_RW,
2357c23b892Sbalrog     [PHY_LP_ABILITY]  = PHY_R,     [PHY_1000T_STATUS]           = PHY_R,
2367c23b892Sbalrog     [PHY_AUTONEG_ADV] = PHY_RW,    [M88E1000_RX_ERR_CNTR]       = PHY_R,
2376883b591SGabriel L. Somlo     [PHY_ID2]         = PHY_R,     [M88E1000_PHY_SPEC_STATUS]   = PHY_R,
2386883b591SGabriel L. Somlo     [PHY_AUTONEG_EXP] = PHY_R,
2397c23b892Sbalrog };
2407c23b892Sbalrog 
2418597f2e1SGabriel L. Somlo /* PHY_ID2 documented in 8254x_GBe_SDM.pdf, pp. 250 */
242814cd3acSMichael S. Tsirkin static const uint16_t phy_reg_init[] = {
2439616c290SGabriel L. Somlo     [PHY_CTRL]   = MII_CR_SPEED_SELECT_MSB |
2449616c290SGabriel L. Somlo                    MII_CR_FULL_DUPLEX |
2459616c290SGabriel L. Somlo                    MII_CR_AUTO_NEG_EN,
2469616c290SGabriel L. Somlo 
2479616c290SGabriel L. Somlo     [PHY_STATUS] = MII_SR_EXTENDED_CAPS |
2489616c290SGabriel L. Somlo                    MII_SR_LINK_STATUS |   /* link initially up */
2499616c290SGabriel L. Somlo                    MII_SR_AUTONEG_CAPS |
2509616c290SGabriel L. Somlo                    /* MII_SR_AUTONEG_COMPLETE: initially NOT completed */
2519616c290SGabriel L. Somlo                    MII_SR_PREAMBLE_SUPPRESS |
2529616c290SGabriel L. Somlo                    MII_SR_EXTENDED_STATUS |
2539616c290SGabriel L. Somlo                    MII_SR_10T_HD_CAPS |
2549616c290SGabriel L. Somlo                    MII_SR_10T_FD_CAPS |
2559616c290SGabriel L. Somlo                    MII_SR_100X_HD_CAPS |
2569616c290SGabriel L. Somlo                    MII_SR_100X_FD_CAPS,
2579616c290SGabriel L. Somlo 
2589616c290SGabriel L. Somlo     [PHY_ID1] = 0x141,
2599616c290SGabriel L. Somlo     /* [PHY_ID2] configured per DevId, from e1000_reset() */
2609616c290SGabriel L. Somlo     [PHY_AUTONEG_ADV] = 0xde1,
2619616c290SGabriel L. Somlo     [PHY_LP_ABILITY] = 0x1e0,
2629616c290SGabriel L. Somlo     [PHY_1000T_CTRL] = 0x0e00,
2639616c290SGabriel L. Somlo     [PHY_1000T_STATUS] = 0x3c00,
2649616c290SGabriel L. Somlo     [M88E1000_PHY_SPEC_CTRL] = 0x360,
265814cd3acSMichael S. Tsirkin     [M88E1000_PHY_SPEC_STATUS] = 0xac00,
2669616c290SGabriel L. Somlo     [M88E1000_EXT_PHY_SPEC_CTRL] = 0x0d60,
267814cd3acSMichael S. Tsirkin };
268814cd3acSMichael S. Tsirkin 
269814cd3acSMichael S. Tsirkin static const uint32_t mac_reg_init[] = {
270814cd3acSMichael S. Tsirkin     [PBA]     = 0x00100030,
271814cd3acSMichael S. Tsirkin     [LEDCTL]  = 0x602,
272814cd3acSMichael S. Tsirkin     [CTRL]    = E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN0 |
273814cd3acSMichael S. Tsirkin                 E1000_CTRL_SPD_1000 | E1000_CTRL_SLU,
274814cd3acSMichael S. Tsirkin     [STATUS]  = 0x80000000 | E1000_STATUS_GIO_MASTER_ENABLE |
275814cd3acSMichael S. Tsirkin                 E1000_STATUS_ASDV | E1000_STATUS_MTXCKOK |
276814cd3acSMichael S. Tsirkin                 E1000_STATUS_SPEED_1000 | E1000_STATUS_FD |
277814cd3acSMichael S. Tsirkin                 E1000_STATUS_LU,
278814cd3acSMichael S. Tsirkin     [MANC]    = E1000_MANC_EN_MNG2HOST | E1000_MANC_RCV_TCO_EN |
279814cd3acSMichael S. Tsirkin                 E1000_MANC_ARP_EN | E1000_MANC_0298_EN |
280814cd3acSMichael S. Tsirkin                 E1000_MANC_RMCP_EN,
281814cd3acSMichael S. Tsirkin };
282814cd3acSMichael S. Tsirkin 
283e9845f09SVincenzo Maffione /* Helper function, *curr == 0 means the value is not set */
284e9845f09SVincenzo Maffione static inline void
285e9845f09SVincenzo Maffione mit_update_delay(uint32_t *curr, uint32_t value)
286e9845f09SVincenzo Maffione {
287e9845f09SVincenzo Maffione     if (value && (*curr == 0 || value < *curr)) {
288e9845f09SVincenzo Maffione         *curr = value;
289e9845f09SVincenzo Maffione     }
290e9845f09SVincenzo Maffione }
291e9845f09SVincenzo Maffione 
2927c23b892Sbalrog static void
2937c23b892Sbalrog set_interrupt_cause(E1000State *s, int index, uint32_t val)
2947c23b892Sbalrog {
295b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
296e9845f09SVincenzo Maffione     uint32_t pending_ints;
297e9845f09SVincenzo Maffione     uint32_t mit_delay;
298b08340d5SAndreas Färber 
2997c23b892Sbalrog     s->mac_reg[ICR] = val;
300a52a8841SMichael S. Tsirkin 
301a52a8841SMichael S. Tsirkin     /*
302a52a8841SMichael S. Tsirkin      * Make sure ICR and ICS registers have the same value.
303a52a8841SMichael S. Tsirkin      * The spec says that the ICS register is write-only.  However in practice,
304a52a8841SMichael S. Tsirkin      * on real hardware ICS is readable, and for reads it has the same value as
305a52a8841SMichael S. Tsirkin      * ICR (except that ICS does not have the clear on read behaviour of ICR).
306a52a8841SMichael S. Tsirkin      *
307a52a8841SMichael S. Tsirkin      * The VxWorks PRO/1000 driver uses this behaviour.
308a52a8841SMichael S. Tsirkin      */
309b1332393SBill Paul     s->mac_reg[ICS] = val;
310a52a8841SMichael S. Tsirkin 
311e9845f09SVincenzo Maffione     pending_ints = (s->mac_reg[IMS] & s->mac_reg[ICR]);
312e9845f09SVincenzo Maffione     if (!s->mit_irq_level && pending_ints) {
313e9845f09SVincenzo Maffione         /*
314e9845f09SVincenzo Maffione          * Here we detect a potential raising edge. We postpone raising the
315e9845f09SVincenzo Maffione          * interrupt line if we are inside the mitigation delay window
316e9845f09SVincenzo Maffione          * (s->mit_timer_on == 1).
317e9845f09SVincenzo Maffione          * We provide a partial implementation of interrupt mitigation,
318e9845f09SVincenzo Maffione          * emulating only RADV, TADV and ITR (lower 16 bits, 1024ns units for
319e9845f09SVincenzo Maffione          * RADV and TADV, 256ns units for ITR). RDTR is only used to enable
320e9845f09SVincenzo Maffione          * RADV; relative timers based on TIDV and RDTR are not implemented.
321e9845f09SVincenzo Maffione          */
322e9845f09SVincenzo Maffione         if (s->mit_timer_on) {
323e9845f09SVincenzo Maffione             return;
324e9845f09SVincenzo Maffione         }
325*bc0f0674SLeonid Bloch         if (chkflag(MIT)) {
326e9845f09SVincenzo Maffione             /* Compute the next mitigation delay according to pending
327e9845f09SVincenzo Maffione              * interrupts and the current values of RADV (provided
328e9845f09SVincenzo Maffione              * RDTR!=0), TADV and ITR.
329e9845f09SVincenzo Maffione              * Then rearm the timer.
330e9845f09SVincenzo Maffione              */
331e9845f09SVincenzo Maffione             mit_delay = 0;
332e9845f09SVincenzo Maffione             if (s->mit_ide &&
333e9845f09SVincenzo Maffione                     (pending_ints & (E1000_ICR_TXQE | E1000_ICR_TXDW))) {
334e9845f09SVincenzo Maffione                 mit_update_delay(&mit_delay, s->mac_reg[TADV] * 4);
335e9845f09SVincenzo Maffione             }
336e9845f09SVincenzo Maffione             if (s->mac_reg[RDTR] && (pending_ints & E1000_ICS_RXT0)) {
337e9845f09SVincenzo Maffione                 mit_update_delay(&mit_delay, s->mac_reg[RADV] * 4);
338e9845f09SVincenzo Maffione             }
339e9845f09SVincenzo Maffione             mit_update_delay(&mit_delay, s->mac_reg[ITR]);
340e9845f09SVincenzo Maffione 
341e9845f09SVincenzo Maffione             if (mit_delay) {
342e9845f09SVincenzo Maffione                 s->mit_timer_on = 1;
343e9845f09SVincenzo Maffione                 timer_mod(s->mit_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
344e9845f09SVincenzo Maffione                           mit_delay * 256);
345e9845f09SVincenzo Maffione             }
346e9845f09SVincenzo Maffione             s->mit_ide = 0;
347e9845f09SVincenzo Maffione         }
348e9845f09SVincenzo Maffione     }
349e9845f09SVincenzo Maffione 
350e9845f09SVincenzo Maffione     s->mit_irq_level = (pending_ints != 0);
3519e64f8a3SMarcel Apfelbaum     pci_set_irq(d, s->mit_irq_level);
352e9845f09SVincenzo Maffione }
353e9845f09SVincenzo Maffione 
354e9845f09SVincenzo Maffione static void
355e9845f09SVincenzo Maffione e1000_mit_timer(void *opaque)
356e9845f09SVincenzo Maffione {
357e9845f09SVincenzo Maffione     E1000State *s = opaque;
358e9845f09SVincenzo Maffione 
359e9845f09SVincenzo Maffione     s->mit_timer_on = 0;
360e9845f09SVincenzo Maffione     /* Call set_interrupt_cause to update the irq level (if necessary). */
361e9845f09SVincenzo Maffione     set_interrupt_cause(s, 0, s->mac_reg[ICR]);
3627c23b892Sbalrog }
3637c23b892Sbalrog 
3647c23b892Sbalrog static void
3657c23b892Sbalrog set_ics(E1000State *s, int index, uint32_t val)
3667c23b892Sbalrog {
3677c23b892Sbalrog     DBGOUT(INTERRUPT, "set_ics %x, ICR %x, IMR %x\n", val, s->mac_reg[ICR],
3687c23b892Sbalrog         s->mac_reg[IMS]);
3697c23b892Sbalrog     set_interrupt_cause(s, 0, val | s->mac_reg[ICR]);
3707c23b892Sbalrog }
3717c23b892Sbalrog 
372d52aec95SGabriel L. Somlo static void
373d52aec95SGabriel L. Somlo e1000_autoneg_timer(void *opaque)
374d52aec95SGabriel L. Somlo {
375d52aec95SGabriel L. Somlo     E1000State *s = opaque;
376d52aec95SGabriel L. Somlo     if (!qemu_get_queue(s->nic)->link_down) {
377d52aec95SGabriel L. Somlo         e1000_link_up(s);
378d52aec95SGabriel L. Somlo         s->phy_reg[PHY_LP_ABILITY] |= MII_LPAR_LPACK;
379d52aec95SGabriel L. Somlo         s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
380d52aec95SGabriel L. Somlo         DBGOUT(PHY, "Auto negotiation is completed\n");
381d52aec95SGabriel L. Somlo         set_ics(s, 0, E1000_ICS_LSC); /* signal link status change to guest */
382d52aec95SGabriel L. Somlo     }
383d52aec95SGabriel L. Somlo }
384d52aec95SGabriel L. Somlo 
3857c23b892Sbalrog static int
3867c23b892Sbalrog rxbufsize(uint32_t v)
3877c23b892Sbalrog {
3887c23b892Sbalrog     v &= E1000_RCTL_BSEX | E1000_RCTL_SZ_16384 | E1000_RCTL_SZ_8192 |
3897c23b892Sbalrog          E1000_RCTL_SZ_4096 | E1000_RCTL_SZ_2048 | E1000_RCTL_SZ_1024 |
3907c23b892Sbalrog          E1000_RCTL_SZ_512 | E1000_RCTL_SZ_256;
3917c23b892Sbalrog     switch (v) {
3927c23b892Sbalrog     case E1000_RCTL_BSEX | E1000_RCTL_SZ_16384:
3937c23b892Sbalrog         return 16384;
3947c23b892Sbalrog     case E1000_RCTL_BSEX | E1000_RCTL_SZ_8192:
3957c23b892Sbalrog         return 8192;
3967c23b892Sbalrog     case E1000_RCTL_BSEX | E1000_RCTL_SZ_4096:
3977c23b892Sbalrog         return 4096;
3987c23b892Sbalrog     case E1000_RCTL_SZ_1024:
3997c23b892Sbalrog         return 1024;
4007c23b892Sbalrog     case E1000_RCTL_SZ_512:
4017c23b892Sbalrog         return 512;
4027c23b892Sbalrog     case E1000_RCTL_SZ_256:
4037c23b892Sbalrog         return 256;
4047c23b892Sbalrog     }
4057c23b892Sbalrog     return 2048;
4067c23b892Sbalrog }
4077c23b892Sbalrog 
408814cd3acSMichael S. Tsirkin static void e1000_reset(void *opaque)
409814cd3acSMichael S. Tsirkin {
410814cd3acSMichael S. Tsirkin     E1000State *d = opaque;
4118597f2e1SGabriel L. Somlo     E1000BaseClass *edc = E1000_DEVICE_GET_CLASS(d);
412372254c6SGabriel L. Somlo     uint8_t *macaddr = d->conf.macaddr.a;
413372254c6SGabriel L. Somlo     int i;
414814cd3acSMichael S. Tsirkin 
415bc72ad67SAlex Bligh     timer_del(d->autoneg_timer);
416e9845f09SVincenzo Maffione     timer_del(d->mit_timer);
417e9845f09SVincenzo Maffione     d->mit_timer_on = 0;
418e9845f09SVincenzo Maffione     d->mit_irq_level = 0;
419e9845f09SVincenzo Maffione     d->mit_ide = 0;
420814cd3acSMichael S. Tsirkin     memset(d->phy_reg, 0, sizeof d->phy_reg);
421814cd3acSMichael S. Tsirkin     memmove(d->phy_reg, phy_reg_init, sizeof phy_reg_init);
4228597f2e1SGabriel L. Somlo     d->phy_reg[PHY_ID2] = edc->phy_id2;
423814cd3acSMichael S. Tsirkin     memset(d->mac_reg, 0, sizeof d->mac_reg);
424814cd3acSMichael S. Tsirkin     memmove(d->mac_reg, mac_reg_init, sizeof mac_reg_init);
425814cd3acSMichael S. Tsirkin     d->rxbuf_min_shift = 1;
426814cd3acSMichael S. Tsirkin     memset(&d->tx, 0, sizeof d->tx);
427814cd3acSMichael S. Tsirkin 
428b356f76dSJason Wang     if (qemu_get_queue(d->nic)->link_down) {
42971aadd3cSJason Wang         e1000_link_down(d);
430814cd3acSMichael S. Tsirkin     }
431372254c6SGabriel L. Somlo 
432372254c6SGabriel L. Somlo     /* Some guests expect pre-initialized RAH/RAL (AddrValid flag + MACaddr) */
433372254c6SGabriel L. Somlo     d->mac_reg[RA] = 0;
434372254c6SGabriel L. Somlo     d->mac_reg[RA + 1] = E1000_RAH_AV;
435372254c6SGabriel L. Somlo     for (i = 0; i < 4; i++) {
436372254c6SGabriel L. Somlo         d->mac_reg[RA] |= macaddr[i] << (8 * i);
437372254c6SGabriel L. Somlo         d->mac_reg[RA + 1] |= (i < 2) ? macaddr[i + 4] << (8 * i) : 0;
438372254c6SGabriel L. Somlo     }
439655d3b63SAmos Kong     qemu_format_nic_info_str(qemu_get_queue(d->nic), macaddr);
440814cd3acSMichael S. Tsirkin }
441814cd3acSMichael S. Tsirkin 
4427c23b892Sbalrog static void
443cab3c825SKevin Wolf set_ctrl(E1000State *s, int index, uint32_t val)
444cab3c825SKevin Wolf {
445cab3c825SKevin Wolf     /* RST is self clearing */
446cab3c825SKevin Wolf     s->mac_reg[CTRL] = val & ~E1000_CTRL_RST;
447cab3c825SKevin Wolf }
448cab3c825SKevin Wolf 
449cab3c825SKevin Wolf static void
4507c23b892Sbalrog set_rx_control(E1000State *s, int index, uint32_t val)
4517c23b892Sbalrog {
4527c23b892Sbalrog     s->mac_reg[RCTL] = val;
4537c23b892Sbalrog     s->rxbuf_size = rxbufsize(val);
4547c23b892Sbalrog     s->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1;
4557c23b892Sbalrog     DBGOUT(RX, "RCTL: %d, mac_reg[RCTL] = 0x%x\n", s->mac_reg[RDT],
4567c23b892Sbalrog            s->mac_reg[RCTL]);
457b356f76dSJason Wang     qemu_flush_queued_packets(qemu_get_queue(s->nic));
4587c23b892Sbalrog }
4597c23b892Sbalrog 
4607c23b892Sbalrog static void
4617c23b892Sbalrog set_mdic(E1000State *s, int index, uint32_t val)
4627c23b892Sbalrog {
4637c23b892Sbalrog     uint32_t data = val & E1000_MDIC_DATA_MASK;
4647c23b892Sbalrog     uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT);
4657c23b892Sbalrog 
4667c23b892Sbalrog     if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) // phy #
4677c23b892Sbalrog         val = s->mac_reg[MDIC] | E1000_MDIC_ERROR;
4687c23b892Sbalrog     else if (val & E1000_MDIC_OP_READ) {
4697c23b892Sbalrog         DBGOUT(MDIC, "MDIC read reg 0x%x\n", addr);
4707c23b892Sbalrog         if (!(phy_regcap[addr] & PHY_R)) {
4717c23b892Sbalrog             DBGOUT(MDIC, "MDIC read reg %x unhandled\n", addr);
4727c23b892Sbalrog             val |= E1000_MDIC_ERROR;
4737c23b892Sbalrog         } else
4747c23b892Sbalrog             val = (val ^ data) | s->phy_reg[addr];
4757c23b892Sbalrog     } else if (val & E1000_MDIC_OP_WRITE) {
4767c23b892Sbalrog         DBGOUT(MDIC, "MDIC write reg 0x%x, value 0x%x\n", addr, data);
4777c23b892Sbalrog         if (!(phy_regcap[addr] & PHY_W)) {
4787c23b892Sbalrog             DBGOUT(MDIC, "MDIC write reg %x unhandled\n", addr);
4797c23b892Sbalrog             val |= E1000_MDIC_ERROR;
480b9d03e35SJason Wang         } else {
481b9d03e35SJason Wang             if (addr < NPHYWRITEOPS && phyreg_writeops[addr]) {
482b9d03e35SJason Wang                 phyreg_writeops[addr](s, index, data);
4831195fed9SGabriel L. Somlo             } else {
4847c23b892Sbalrog                 s->phy_reg[addr] = data;
4857c23b892Sbalrog             }
486b9d03e35SJason Wang         }
4871195fed9SGabriel L. Somlo     }
4887c23b892Sbalrog     s->mac_reg[MDIC] = val | E1000_MDIC_READY;
48917fbbb0bSJason Wang 
49017fbbb0bSJason Wang     if (val & E1000_MDIC_INT_EN) {
4917c23b892Sbalrog         set_ics(s, 0, E1000_ICR_MDAC);
4927c23b892Sbalrog     }
49317fbbb0bSJason Wang }
4947c23b892Sbalrog 
4957c23b892Sbalrog static uint32_t
4967c23b892Sbalrog get_eecd(E1000State *s, int index)
4977c23b892Sbalrog {
4987c23b892Sbalrog     uint32_t ret = E1000_EECD_PRES|E1000_EECD_GNT | s->eecd_state.old_eecd;
4997c23b892Sbalrog 
5007c23b892Sbalrog     DBGOUT(EEPROM, "reading eeprom bit %d (reading %d)\n",
5017c23b892Sbalrog            s->eecd_state.bitnum_out, s->eecd_state.reading);
5027c23b892Sbalrog     if (!s->eecd_state.reading ||
5037c23b892Sbalrog         ((s->eeprom_data[(s->eecd_state.bitnum_out >> 4) & 0x3f] >>
5047c23b892Sbalrog           ((s->eecd_state.bitnum_out & 0xf) ^ 0xf))) & 1)
5057c23b892Sbalrog         ret |= E1000_EECD_DO;
5067c23b892Sbalrog     return ret;
5077c23b892Sbalrog }
5087c23b892Sbalrog 
5097c23b892Sbalrog static void
5107c23b892Sbalrog set_eecd(E1000State *s, int index, uint32_t val)
5117c23b892Sbalrog {
5127c23b892Sbalrog     uint32_t oldval = s->eecd_state.old_eecd;
5137c23b892Sbalrog 
5147c23b892Sbalrog     s->eecd_state.old_eecd = val & (E1000_EECD_SK | E1000_EECD_CS |
5157c23b892Sbalrog             E1000_EECD_DI|E1000_EECD_FWE_MASK|E1000_EECD_REQ);
51620f3e863SLeonid Bloch     if (!(E1000_EECD_CS & val)) {            /* CS inactive; nothing to do */
5179651ac55SIzumi Tsutsui         return;
51820f3e863SLeonid Bloch     }
51920f3e863SLeonid Bloch     if (E1000_EECD_CS & (val ^ oldval)) {    /* CS rise edge; reset state */
5209651ac55SIzumi Tsutsui         s->eecd_state.val_in = 0;
5219651ac55SIzumi Tsutsui         s->eecd_state.bitnum_in = 0;
5229651ac55SIzumi Tsutsui         s->eecd_state.bitnum_out = 0;
5239651ac55SIzumi Tsutsui         s->eecd_state.reading = 0;
5249651ac55SIzumi Tsutsui     }
52520f3e863SLeonid Bloch     if (!(E1000_EECD_SK & (val ^ oldval))) {    /* no clock edge */
5267c23b892Sbalrog         return;
52720f3e863SLeonid Bloch     }
52820f3e863SLeonid Bloch     if (!(E1000_EECD_SK & val)) {               /* falling edge */
5297c23b892Sbalrog         s->eecd_state.bitnum_out++;
5307c23b892Sbalrog         return;
5317c23b892Sbalrog     }
5327c23b892Sbalrog     s->eecd_state.val_in <<= 1;
5337c23b892Sbalrog     if (val & E1000_EECD_DI)
5347c23b892Sbalrog         s->eecd_state.val_in |= 1;
5357c23b892Sbalrog     if (++s->eecd_state.bitnum_in == 9 && !s->eecd_state.reading) {
5367c23b892Sbalrog         s->eecd_state.bitnum_out = ((s->eecd_state.val_in & 0x3f)<<4)-1;
5377c23b892Sbalrog         s->eecd_state.reading = (((s->eecd_state.val_in >> 6) & 7) ==
5387c23b892Sbalrog             EEPROM_READ_OPCODE_MICROWIRE);
5397c23b892Sbalrog     }
5407c23b892Sbalrog     DBGOUT(EEPROM, "eeprom bitnum in %d out %d, reading %d\n",
5417c23b892Sbalrog            s->eecd_state.bitnum_in, s->eecd_state.bitnum_out,
5427c23b892Sbalrog            s->eecd_state.reading);
5437c23b892Sbalrog }
5447c23b892Sbalrog 
5457c23b892Sbalrog static uint32_t
5467c23b892Sbalrog flash_eerd_read(E1000State *s, int x)
5477c23b892Sbalrog {
5487c23b892Sbalrog     unsigned int index, r = s->mac_reg[EERD] & ~E1000_EEPROM_RW_REG_START;
5497c23b892Sbalrog 
550b1332393SBill Paul     if ((s->mac_reg[EERD] & E1000_EEPROM_RW_REG_START) == 0)
551b1332393SBill Paul         return (s->mac_reg[EERD]);
552b1332393SBill Paul 
5537c23b892Sbalrog     if ((index = r >> E1000_EEPROM_RW_ADDR_SHIFT) > EEPROM_CHECKSUM_REG)
554b1332393SBill Paul         return (E1000_EEPROM_RW_REG_DONE | r);
555b1332393SBill Paul 
556b1332393SBill Paul     return ((s->eeprom_data[index] << E1000_EEPROM_RW_REG_DATA) |
557b1332393SBill Paul            E1000_EEPROM_RW_REG_DONE | r);
5587c23b892Sbalrog }
5597c23b892Sbalrog 
5607c23b892Sbalrog static void
5617c23b892Sbalrog putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t css, uint32_t cse)
5627c23b892Sbalrog {
563c6a6a5e3Saliguori     uint32_t sum;
564c6a6a5e3Saliguori 
5657c23b892Sbalrog     if (cse && cse < n)
5667c23b892Sbalrog         n = cse + 1;
567c6a6a5e3Saliguori     if (sloc < n-1) {
568c6a6a5e3Saliguori         sum = net_checksum_add(n-css, data+css);
569d8ee2591SPeter Maydell         stw_be_p(data + sloc, net_checksum_finish(sum));
570c6a6a5e3Saliguori     }
5717c23b892Sbalrog }
5727c23b892Sbalrog 
5738f2e8d1fSaliguori static inline int
5748f2e8d1fSaliguori vlan_enabled(E1000State *s)
5758f2e8d1fSaliguori {
5768f2e8d1fSaliguori     return ((s->mac_reg[CTRL] & E1000_CTRL_VME) != 0);
5778f2e8d1fSaliguori }
5788f2e8d1fSaliguori 
5798f2e8d1fSaliguori static inline int
5808f2e8d1fSaliguori vlan_rx_filter_enabled(E1000State *s)
5818f2e8d1fSaliguori {
5828f2e8d1fSaliguori     return ((s->mac_reg[RCTL] & E1000_RCTL_VFE) != 0);
5838f2e8d1fSaliguori }
5848f2e8d1fSaliguori 
5858f2e8d1fSaliguori static inline int
5868f2e8d1fSaliguori is_vlan_packet(E1000State *s, const uint8_t *buf)
5878f2e8d1fSaliguori {
5888f2e8d1fSaliguori     return (be16_to_cpup((uint16_t *)(buf + 12)) ==
5894e60a250SShannon Zhao                 le16_to_cpu(s->mac_reg[VET]));
5908f2e8d1fSaliguori }
5918f2e8d1fSaliguori 
5928f2e8d1fSaliguori static inline int
5938f2e8d1fSaliguori is_vlan_txd(uint32_t txd_lower)
5948f2e8d1fSaliguori {
5958f2e8d1fSaliguori     return ((txd_lower & E1000_TXD_CMD_VLE) != 0);
5968f2e8d1fSaliguori }
5978f2e8d1fSaliguori 
59855e8d1ceSMichael S. Tsirkin /* FCS aka Ethernet CRC-32. We don't get it from backends and can't
59955e8d1ceSMichael S. Tsirkin  * fill it in, just pad descriptor length by 4 bytes unless guest
600a05e8a6eSMichael S. Tsirkin  * told us to strip it off the packet. */
60155e8d1ceSMichael S. Tsirkin static inline int
60255e8d1ceSMichael S. Tsirkin fcs_len(E1000State *s)
60355e8d1ceSMichael S. Tsirkin {
60455e8d1ceSMichael S. Tsirkin     return (s->mac_reg[RCTL] & E1000_RCTL_SECRC) ? 0 : 4;
60555e8d1ceSMichael S. Tsirkin }
60655e8d1ceSMichael S. Tsirkin 
6077c23b892Sbalrog static void
60893e37d76SJason Wang e1000_send_packet(E1000State *s, const uint8_t *buf, int size)
60993e37d76SJason Wang {
610b356f76dSJason Wang     NetClientState *nc = qemu_get_queue(s->nic);
61193e37d76SJason Wang     if (s->phy_reg[PHY_CTRL] & MII_CR_LOOPBACK) {
612b356f76dSJason Wang         nc->info->receive(nc, buf, size);
61393e37d76SJason Wang     } else {
614b356f76dSJason Wang         qemu_send_packet(nc, buf, size);
61593e37d76SJason Wang     }
61693e37d76SJason Wang }
61793e37d76SJason Wang 
61893e37d76SJason Wang static void
6197c23b892Sbalrog xmit_seg(E1000State *s)
6207c23b892Sbalrog {
6217c23b892Sbalrog     uint16_t len, *sp;
6227c23b892Sbalrog     unsigned int frames = s->tx.tso_frames, css, sofar, n;
6237c23b892Sbalrog     struct e1000_tx *tp = &s->tx;
6247c23b892Sbalrog 
6251b0009dbSbalrog     if (tp->tse && tp->cptse) {
6267c23b892Sbalrog         css = tp->ipcss;
6277c23b892Sbalrog         DBGOUT(TXSUM, "frames %d size %d ipcss %d\n",
6287c23b892Sbalrog                frames, tp->size, css);
62920f3e863SLeonid Bloch         if (tp->ip) {    /* IPv4 */
630d8ee2591SPeter Maydell             stw_be_p(tp->data+css+2, tp->size - css);
631d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4,
6327c23b892Sbalrog                      be16_to_cpup((uint16_t *)(tp->data+css+4))+frames);
63320f3e863SLeonid Bloch         } else {         /* IPv6 */
634d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4, tp->size - css);
63520f3e863SLeonid Bloch         }
6367c23b892Sbalrog         css = tp->tucss;
6377c23b892Sbalrog         len = tp->size - css;
6387c23b892Sbalrog         DBGOUT(TXSUM, "tcp %d tucss %d len %d\n", tp->tcp, css, len);
6397c23b892Sbalrog         if (tp->tcp) {
6407c23b892Sbalrog             sofar = frames * tp->mss;
6416bd194abSPeter Maydell             stl_be_p(tp->data+css+4, ldl_be_p(tp->data+css+4)+sofar); /* seq */
6427c23b892Sbalrog             if (tp->paylen - sofar > tp->mss)
64320f3e863SLeonid Bloch                 tp->data[css + 13] &= ~9;    /* PSH, FIN */
64420f3e863SLeonid Bloch         } else    /* UDP */
645d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4, len);
6467c23b892Sbalrog         if (tp->sum_needed & E1000_TXD_POPTS_TXSM) {
647e685b4ebSAlex Williamson             unsigned int phsum;
6487c23b892Sbalrog             // add pseudo-header length before checksum calculation
6497c23b892Sbalrog             sp = (uint16_t *)(tp->data + tp->tucso);
650e685b4ebSAlex Williamson             phsum = be16_to_cpup(sp) + len;
651e685b4ebSAlex Williamson             phsum = (phsum >> 16) + (phsum & 0xffff);
652d8ee2591SPeter Maydell             stw_be_p(sp, phsum);
6537c23b892Sbalrog         }
6547c23b892Sbalrog         tp->tso_frames++;
6557c23b892Sbalrog     }
6567c23b892Sbalrog 
6577c23b892Sbalrog     if (tp->sum_needed & E1000_TXD_POPTS_TXSM)
6587c23b892Sbalrog         putsum(tp->data, tp->size, tp->tucso, tp->tucss, tp->tucse);
6597c23b892Sbalrog     if (tp->sum_needed & E1000_TXD_POPTS_IXSM)
6607c23b892Sbalrog         putsum(tp->data, tp->size, tp->ipcso, tp->ipcss, tp->ipcse);
6618f2e8d1fSaliguori     if (tp->vlan_needed) {
662b10fec9bSStefan Weil         memmove(tp->vlan, tp->data, 4);
663b10fec9bSStefan Weil         memmove(tp->data, tp->data + 4, 8);
6648f2e8d1fSaliguori         memcpy(tp->data + 8, tp->vlan_header, 4);
66593e37d76SJason Wang         e1000_send_packet(s, tp->vlan, tp->size + 4);
66620f3e863SLeonid Bloch     } else {
66793e37d76SJason Wang         e1000_send_packet(s, tp->data, tp->size);
66820f3e863SLeonid Bloch     }
66920f3e863SLeonid Bloch 
6707c23b892Sbalrog     s->mac_reg[TPT]++;
6717c23b892Sbalrog     s->mac_reg[GPTC]++;
6727c23b892Sbalrog     n = s->mac_reg[TOTL];
6737c23b892Sbalrog     if ((s->mac_reg[TOTL] += s->tx.size) < n)
6747c23b892Sbalrog         s->mac_reg[TOTH]++;
6757c23b892Sbalrog }
6767c23b892Sbalrog 
6777c23b892Sbalrog static void
6787c23b892Sbalrog process_tx_desc(E1000State *s, struct e1000_tx_desc *dp)
6797c23b892Sbalrog {
680b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
6817c23b892Sbalrog     uint32_t txd_lower = le32_to_cpu(dp->lower.data);
6827c23b892Sbalrog     uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D);
6837c23b892Sbalrog     unsigned int split_size = txd_lower & 0xffff, bytes, sz, op;
684a0ae17a6SAndrew Jones     unsigned int msh = 0xfffff;
6857c23b892Sbalrog     uint64_t addr;
6867c23b892Sbalrog     struct e1000_context_desc *xp = (struct e1000_context_desc *)dp;
6877c23b892Sbalrog     struct e1000_tx *tp = &s->tx;
6887c23b892Sbalrog 
689e9845f09SVincenzo Maffione     s->mit_ide |= (txd_lower & E1000_TXD_CMD_IDE);
69020f3e863SLeonid Bloch     if (dtype == E1000_TXD_CMD_DEXT) {    /* context descriptor */
6917c23b892Sbalrog         op = le32_to_cpu(xp->cmd_and_length);
6927c23b892Sbalrog         tp->ipcss = xp->lower_setup.ip_fields.ipcss;
6937c23b892Sbalrog         tp->ipcso = xp->lower_setup.ip_fields.ipcso;
6947c23b892Sbalrog         tp->ipcse = le16_to_cpu(xp->lower_setup.ip_fields.ipcse);
6957c23b892Sbalrog         tp->tucss = xp->upper_setup.tcp_fields.tucss;
6967c23b892Sbalrog         tp->tucso = xp->upper_setup.tcp_fields.tucso;
6977c23b892Sbalrog         tp->tucse = le16_to_cpu(xp->upper_setup.tcp_fields.tucse);
6987c23b892Sbalrog         tp->paylen = op & 0xfffff;
6997c23b892Sbalrog         tp->hdr_len = xp->tcp_seg_setup.fields.hdr_len;
7007c23b892Sbalrog         tp->mss = le16_to_cpu(xp->tcp_seg_setup.fields.mss);
7017c23b892Sbalrog         tp->ip = (op & E1000_TXD_CMD_IP) ? 1 : 0;
7027c23b892Sbalrog         tp->tcp = (op & E1000_TXD_CMD_TCP) ? 1 : 0;
7037c23b892Sbalrog         tp->tse = (op & E1000_TXD_CMD_TSE) ? 1 : 0;
7047c23b892Sbalrog         tp->tso_frames = 0;
70520f3e863SLeonid Bloch         if (tp->tucso == 0) {    /* this is probably wrong */
7067c23b892Sbalrog             DBGOUT(TXSUM, "TCP/UDP: cso 0!\n");
7077c23b892Sbalrog             tp->tucso = tp->tucss + (tp->tcp ? 16 : 6);
7087c23b892Sbalrog         }
7097c23b892Sbalrog         return;
7101b0009dbSbalrog     } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) {
7111b0009dbSbalrog         // data descriptor
712735e77ecSStefan Hajnoczi         if (tp->size == 0) {
7137c23b892Sbalrog             tp->sum_needed = le32_to_cpu(dp->upper.data) >> 8;
714735e77ecSStefan Hajnoczi         }
7151b0009dbSbalrog         tp->cptse = ( txd_lower & E1000_TXD_CMD_TSE ) ? 1 : 0;
71643ad7e3eSJes Sorensen     } else {
7171b0009dbSbalrog         // legacy descriptor
7181b0009dbSbalrog         tp->cptse = 0;
71943ad7e3eSJes Sorensen     }
7207c23b892Sbalrog 
7218f2e8d1fSaliguori     if (vlan_enabled(s) && is_vlan_txd(txd_lower) &&
7228f2e8d1fSaliguori         (tp->cptse || txd_lower & E1000_TXD_CMD_EOP)) {
7238f2e8d1fSaliguori         tp->vlan_needed = 1;
724d8ee2591SPeter Maydell         stw_be_p(tp->vlan_header,
7254e60a250SShannon Zhao                       le16_to_cpu(s->mac_reg[VET]));
726d8ee2591SPeter Maydell         stw_be_p(tp->vlan_header + 2,
7278f2e8d1fSaliguori                       le16_to_cpu(dp->upper.fields.special));
7288f2e8d1fSaliguori     }
7298f2e8d1fSaliguori 
7307c23b892Sbalrog     addr = le64_to_cpu(dp->buffer_addr);
7311b0009dbSbalrog     if (tp->tse && tp->cptse) {
732a0ae17a6SAndrew Jones         msh = tp->hdr_len + tp->mss;
7337c23b892Sbalrog         do {
7347c23b892Sbalrog             bytes = split_size;
7357c23b892Sbalrog             if (tp->size + bytes > msh)
7367c23b892Sbalrog                 bytes = msh - tp->size;
73765f82df0SAnthony Liguori 
73865f82df0SAnthony Liguori             bytes = MIN(sizeof(tp->data) - tp->size, bytes);
739b08340d5SAndreas Färber             pci_dma_read(d, addr, tp->data + tp->size, bytes);
740a0ae17a6SAndrew Jones             sz = tp->size + bytes;
741a0ae17a6SAndrew Jones             if (sz >= tp->hdr_len && tp->size < tp->hdr_len) {
742a0ae17a6SAndrew Jones                 memmove(tp->header, tp->data, tp->hdr_len);
743a0ae17a6SAndrew Jones             }
7447c23b892Sbalrog             tp->size = sz;
7457c23b892Sbalrog             addr += bytes;
7467c23b892Sbalrog             if (sz == msh) {
7477c23b892Sbalrog                 xmit_seg(s);
748a0ae17a6SAndrew Jones                 memmove(tp->data, tp->header, tp->hdr_len);
749a0ae17a6SAndrew Jones                 tp->size = tp->hdr_len;
7507c23b892Sbalrog             }
751b947ac2bSP J P             split_size -= bytes;
752b947ac2bSP J P         } while (bytes && split_size);
7531b0009dbSbalrog     } else if (!tp->tse && tp->cptse) {
7541b0009dbSbalrog         // context descriptor TSE is not set, while data descriptor TSE is set
755362f5fb5SStefan Weil         DBGOUT(TXERR, "TCP segmentation error\n");
7561b0009dbSbalrog     } else {
75765f82df0SAnthony Liguori         split_size = MIN(sizeof(tp->data) - tp->size, split_size);
758b08340d5SAndreas Färber         pci_dma_read(d, addr, tp->data + tp->size, split_size);
7591b0009dbSbalrog         tp->size += split_size;
7601b0009dbSbalrog     }
7617c23b892Sbalrog 
7627c23b892Sbalrog     if (!(txd_lower & E1000_TXD_CMD_EOP))
7637c23b892Sbalrog         return;
764a0ae17a6SAndrew Jones     if (!(tp->tse && tp->cptse && tp->size < tp->hdr_len)) {
7657c23b892Sbalrog         xmit_seg(s);
766a0ae17a6SAndrew Jones     }
7677c23b892Sbalrog     tp->tso_frames = 0;
7687c23b892Sbalrog     tp->sum_needed = 0;
7698f2e8d1fSaliguori     tp->vlan_needed = 0;
7707c23b892Sbalrog     tp->size = 0;
7711b0009dbSbalrog     tp->cptse = 0;
7727c23b892Sbalrog }
7737c23b892Sbalrog 
7747c23b892Sbalrog static uint32_t
77562ecbd35SEduard - Gabriel Munteanu txdesc_writeback(E1000State *s, dma_addr_t base, struct e1000_tx_desc *dp)
7767c23b892Sbalrog {
777b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
7787c23b892Sbalrog     uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data);
7797c23b892Sbalrog 
7807c23b892Sbalrog     if (!(txd_lower & (E1000_TXD_CMD_RS|E1000_TXD_CMD_RPS)))
7817c23b892Sbalrog         return 0;
7827c23b892Sbalrog     txd_upper = (le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD) &
7837c23b892Sbalrog                 ~(E1000_TXD_STAT_EC | E1000_TXD_STAT_LC | E1000_TXD_STAT_TU);
7847c23b892Sbalrog     dp->upper.data = cpu_to_le32(txd_upper);
785b08340d5SAndreas Färber     pci_dma_write(d, base + ((char *)&dp->upper - (char *)dp),
78600c3a05bSDavid Gibson                   &dp->upper, sizeof(dp->upper));
7877c23b892Sbalrog     return E1000_ICR_TXDW;
7887c23b892Sbalrog }
7897c23b892Sbalrog 
790d17161f6SKevin Wolf static uint64_t tx_desc_base(E1000State *s)
791d17161f6SKevin Wolf {
792d17161f6SKevin Wolf     uint64_t bah = s->mac_reg[TDBAH];
793d17161f6SKevin Wolf     uint64_t bal = s->mac_reg[TDBAL] & ~0xf;
794d17161f6SKevin Wolf 
795d17161f6SKevin Wolf     return (bah << 32) + bal;
796d17161f6SKevin Wolf }
797d17161f6SKevin Wolf 
7987c23b892Sbalrog static void
7997c23b892Sbalrog start_xmit(E1000State *s)
8007c23b892Sbalrog {
801b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
80262ecbd35SEduard - Gabriel Munteanu     dma_addr_t base;
8037c23b892Sbalrog     struct e1000_tx_desc desc;
8047c23b892Sbalrog     uint32_t tdh_start = s->mac_reg[TDH], cause = E1000_ICS_TXQE;
8057c23b892Sbalrog 
8067c23b892Sbalrog     if (!(s->mac_reg[TCTL] & E1000_TCTL_EN)) {
8077c23b892Sbalrog         DBGOUT(TX, "tx disabled\n");
8087c23b892Sbalrog         return;
8097c23b892Sbalrog     }
8107c23b892Sbalrog 
8117c23b892Sbalrog     while (s->mac_reg[TDH] != s->mac_reg[TDT]) {
812d17161f6SKevin Wolf         base = tx_desc_base(s) +
8137c23b892Sbalrog                sizeof(struct e1000_tx_desc) * s->mac_reg[TDH];
814b08340d5SAndreas Färber         pci_dma_read(d, base, &desc, sizeof(desc));
8157c23b892Sbalrog 
8167c23b892Sbalrog         DBGOUT(TX, "index %d: %p : %x %x\n", s->mac_reg[TDH],
8176106075bSths                (void *)(intptr_t)desc.buffer_addr, desc.lower.data,
8187c23b892Sbalrog                desc.upper.data);
8197c23b892Sbalrog 
8207c23b892Sbalrog         process_tx_desc(s, &desc);
82162ecbd35SEduard - Gabriel Munteanu         cause |= txdesc_writeback(s, base, &desc);
8227c23b892Sbalrog 
8237c23b892Sbalrog         if (++s->mac_reg[TDH] * sizeof(desc) >= s->mac_reg[TDLEN])
8247c23b892Sbalrog             s->mac_reg[TDH] = 0;
8257c23b892Sbalrog         /*
8267c23b892Sbalrog          * the following could happen only if guest sw assigns
8277c23b892Sbalrog          * bogus values to TDT/TDLEN.
8287c23b892Sbalrog          * there's nothing too intelligent we could do about this.
8297c23b892Sbalrog          */
8307c23b892Sbalrog         if (s->mac_reg[TDH] == tdh_start) {
8317c23b892Sbalrog             DBGOUT(TXERR, "TDH wraparound @%x, TDT %x, TDLEN %x\n",
8327c23b892Sbalrog                    tdh_start, s->mac_reg[TDT], s->mac_reg[TDLEN]);
8337c23b892Sbalrog             break;
8347c23b892Sbalrog         }
8357c23b892Sbalrog     }
8367c23b892Sbalrog     set_ics(s, 0, cause);
8377c23b892Sbalrog }
8387c23b892Sbalrog 
8397c23b892Sbalrog static int
8407c23b892Sbalrog receive_filter(E1000State *s, const uint8_t *buf, int size)
8417c23b892Sbalrog {
842af2960f9SBlue Swirl     static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
843af2960f9SBlue Swirl     static const int mta_shift[] = {4, 3, 2, 0};
8447c23b892Sbalrog     uint32_t f, rctl = s->mac_reg[RCTL], ra[2], *rp;
8457c23b892Sbalrog 
8468f2e8d1fSaliguori     if (is_vlan_packet(s, buf) && vlan_rx_filter_enabled(s)) {
8478f2e8d1fSaliguori         uint16_t vid = be16_to_cpup((uint16_t *)(buf + 14));
8488f2e8d1fSaliguori         uint32_t vfta = le32_to_cpup((uint32_t *)(s->mac_reg + VFTA) +
8498f2e8d1fSaliguori                                      ((vid >> 5) & 0x7f));
8508f2e8d1fSaliguori         if ((vfta & (1 << (vid & 0x1f))) == 0)
8518f2e8d1fSaliguori             return 0;
8528f2e8d1fSaliguori     }
8538f2e8d1fSaliguori 
8547c23b892Sbalrog     if (rctl & E1000_RCTL_UPE)			// promiscuous
8557c23b892Sbalrog         return 1;
8567c23b892Sbalrog 
8577c23b892Sbalrog     if ((buf[0] & 1) && (rctl & E1000_RCTL_MPE))	// promiscuous mcast
8587c23b892Sbalrog         return 1;
8597c23b892Sbalrog 
8607c23b892Sbalrog     if ((rctl & E1000_RCTL_BAM) && !memcmp(buf, bcast, sizeof bcast))
8617c23b892Sbalrog         return 1;
8627c23b892Sbalrog 
8637c23b892Sbalrog     for (rp = s->mac_reg + RA; rp < s->mac_reg + RA + 32; rp += 2) {
8647c23b892Sbalrog         if (!(rp[1] & E1000_RAH_AV))
8657c23b892Sbalrog             continue;
8667c23b892Sbalrog         ra[0] = cpu_to_le32(rp[0]);
8677c23b892Sbalrog         ra[1] = cpu_to_le32(rp[1]);
8687c23b892Sbalrog         if (!memcmp(buf, (uint8_t *)ra, 6)) {
8697c23b892Sbalrog             DBGOUT(RXFILTER,
8707c23b892Sbalrog                    "unicast match[%d]: %02x:%02x:%02x:%02x:%02x:%02x\n",
8717c23b892Sbalrog                    (int)(rp - s->mac_reg - RA)/2,
8727c23b892Sbalrog                    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
8737c23b892Sbalrog             return 1;
8747c23b892Sbalrog         }
8757c23b892Sbalrog     }
8767c23b892Sbalrog     DBGOUT(RXFILTER, "unicast mismatch: %02x:%02x:%02x:%02x:%02x:%02x\n",
8777c23b892Sbalrog            buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
8787c23b892Sbalrog 
8797c23b892Sbalrog     f = mta_shift[(rctl >> E1000_RCTL_MO_SHIFT) & 3];
8807c23b892Sbalrog     f = (((buf[5] << 8) | buf[4]) >> f) & 0xfff;
8817c23b892Sbalrog     if (s->mac_reg[MTA + (f >> 5)] & (1 << (f & 0x1f)))
8827c23b892Sbalrog         return 1;
8837c23b892Sbalrog     DBGOUT(RXFILTER,
8847c23b892Sbalrog            "dropping, inexact filter mismatch: %02x:%02x:%02x:%02x:%02x:%02x MO %d MTA[%d] %x\n",
8857c23b892Sbalrog            buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
8867c23b892Sbalrog            (rctl >> E1000_RCTL_MO_SHIFT) & 3, f >> 5,
8877c23b892Sbalrog            s->mac_reg[MTA + (f >> 5)]);
8887c23b892Sbalrog 
8897c23b892Sbalrog     return 0;
8907c23b892Sbalrog }
8917c23b892Sbalrog 
89299ed7e30Saliguori static void
8934e68f7a0SStefan Hajnoczi e1000_set_link_status(NetClientState *nc)
89499ed7e30Saliguori {
895cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
89699ed7e30Saliguori     uint32_t old_status = s->mac_reg[STATUS];
89799ed7e30Saliguori 
898d4044c2aSBjørn Mork     if (nc->link_down) {
89971aadd3cSJason Wang         e1000_link_down(s);
900d4044c2aSBjørn Mork     } else {
901d7a41552SGabriel L. Somlo         if (have_autoneg(s) &&
9026a2acedbSGabriel L. Somlo             !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
9036a2acedbSGabriel L. Somlo             /* emulate auto-negotiation if supported */
9046a2acedbSGabriel L. Somlo             timer_mod(s->autoneg_timer,
9056a2acedbSGabriel L. Somlo                       qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
9066a2acedbSGabriel L. Somlo         } else {
90771aadd3cSJason Wang             e1000_link_up(s);
908d4044c2aSBjørn Mork         }
9096a2acedbSGabriel L. Somlo     }
91099ed7e30Saliguori 
91199ed7e30Saliguori     if (s->mac_reg[STATUS] != old_status)
91299ed7e30Saliguori         set_ics(s, 0, E1000_ICR_LSC);
91399ed7e30Saliguori }
91499ed7e30Saliguori 
915322fd48aSMichael S. Tsirkin static bool e1000_has_rxbufs(E1000State *s, size_t total_size)
916322fd48aSMichael S. Tsirkin {
917322fd48aSMichael S. Tsirkin     int bufs;
918322fd48aSMichael S. Tsirkin     /* Fast-path short packets */
919322fd48aSMichael S. Tsirkin     if (total_size <= s->rxbuf_size) {
920e5b8b0d4SDmitry Fleytman         return s->mac_reg[RDH] != s->mac_reg[RDT];
921322fd48aSMichael S. Tsirkin     }
922322fd48aSMichael S. Tsirkin     if (s->mac_reg[RDH] < s->mac_reg[RDT]) {
923322fd48aSMichael S. Tsirkin         bufs = s->mac_reg[RDT] - s->mac_reg[RDH];
924e5b8b0d4SDmitry Fleytman     } else if (s->mac_reg[RDH] > s->mac_reg[RDT]) {
925322fd48aSMichael S. Tsirkin         bufs = s->mac_reg[RDLEN] /  sizeof(struct e1000_rx_desc) +
926322fd48aSMichael S. Tsirkin             s->mac_reg[RDT] - s->mac_reg[RDH];
927322fd48aSMichael S. Tsirkin     } else {
928322fd48aSMichael S. Tsirkin         return false;
929322fd48aSMichael S. Tsirkin     }
930322fd48aSMichael S. Tsirkin     return total_size <= bufs * s->rxbuf_size;
931322fd48aSMichael S. Tsirkin }
932322fd48aSMichael S. Tsirkin 
9336cdfab28SMichael S. Tsirkin static int
9344e68f7a0SStefan Hajnoczi e1000_can_receive(NetClientState *nc)
9356cdfab28SMichael S. Tsirkin {
936cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
9376cdfab28SMichael S. Tsirkin 
938ddcb73b7SMichael S. Tsirkin     return (s->mac_reg[STATUS] & E1000_STATUS_LU) &&
93920302e71SMichael S. Tsirkin         (s->mac_reg[RCTL] & E1000_RCTL_EN) &&
94020302e71SMichael S. Tsirkin         (s->parent_obj.config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
94120302e71SMichael S. Tsirkin         e1000_has_rxbufs(s, 1);
9426cdfab28SMichael S. Tsirkin }
9436cdfab28SMichael S. Tsirkin 
944d17161f6SKevin Wolf static uint64_t rx_desc_base(E1000State *s)
945d17161f6SKevin Wolf {
946d17161f6SKevin Wolf     uint64_t bah = s->mac_reg[RDBAH];
947d17161f6SKevin Wolf     uint64_t bal = s->mac_reg[RDBAL] & ~0xf;
948d17161f6SKevin Wolf 
949d17161f6SKevin Wolf     return (bah << 32) + bal;
950d17161f6SKevin Wolf }
951d17161f6SKevin Wolf 
9524f1c942bSMark McLoughlin static ssize_t
95397410ddeSVincenzo Maffione e1000_receive_iov(NetClientState *nc, const struct iovec *iov, int iovcnt)
9547c23b892Sbalrog {
955cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
956b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
9577c23b892Sbalrog     struct e1000_rx_desc desc;
95862ecbd35SEduard - Gabriel Munteanu     dma_addr_t base;
9597c23b892Sbalrog     unsigned int n, rdt;
9607c23b892Sbalrog     uint32_t rdh_start;
9618f2e8d1fSaliguori     uint16_t vlan_special = 0;
96297410ddeSVincenzo Maffione     uint8_t vlan_status = 0;
96378aeb23eSStefan Hajnoczi     uint8_t min_buf[MIN_BUF_SIZE];
96497410ddeSVincenzo Maffione     struct iovec min_iov;
96597410ddeSVincenzo Maffione     uint8_t *filter_buf = iov->iov_base;
96697410ddeSVincenzo Maffione     size_t size = iov_size(iov, iovcnt);
96797410ddeSVincenzo Maffione     size_t iov_ofs = 0;
968b19487e2SMichael S. Tsirkin     size_t desc_offset;
969b19487e2SMichael S. Tsirkin     size_t desc_size;
970b19487e2SMichael S. Tsirkin     size_t total_size;
9717c23b892Sbalrog 
972ddcb73b7SMichael S. Tsirkin     if (!(s->mac_reg[STATUS] & E1000_STATUS_LU)) {
9734f1c942bSMark McLoughlin         return -1;
974ddcb73b7SMichael S. Tsirkin     }
975ddcb73b7SMichael S. Tsirkin 
976ddcb73b7SMichael S. Tsirkin     if (!(s->mac_reg[RCTL] & E1000_RCTL_EN)) {
977ddcb73b7SMichael S. Tsirkin         return -1;
978ddcb73b7SMichael S. Tsirkin     }
9797c23b892Sbalrog 
98078aeb23eSStefan Hajnoczi     /* Pad to minimum Ethernet frame length */
98178aeb23eSStefan Hajnoczi     if (size < sizeof(min_buf)) {
98297410ddeSVincenzo Maffione         iov_to_buf(iov, iovcnt, 0, min_buf, size);
98378aeb23eSStefan Hajnoczi         memset(&min_buf[size], 0, sizeof(min_buf) - size);
98497410ddeSVincenzo Maffione         min_iov.iov_base = filter_buf = min_buf;
98597410ddeSVincenzo Maffione         min_iov.iov_len = size = sizeof(min_buf);
98697410ddeSVincenzo Maffione         iovcnt = 1;
98797410ddeSVincenzo Maffione         iov = &min_iov;
98897410ddeSVincenzo Maffione     } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) {
98997410ddeSVincenzo Maffione         /* This is very unlikely, but may happen. */
99097410ddeSVincenzo Maffione         iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN);
99197410ddeSVincenzo Maffione         filter_buf = min_buf;
99278aeb23eSStefan Hajnoczi     }
99378aeb23eSStefan Hajnoczi 
994b0d9ffcdSMichael Contreras     /* Discard oversized packets if !LPE and !SBP. */
9952c0331f4SMichael Contreras     if ((size > MAXIMUM_ETHERNET_LPE_SIZE ||
9962c0331f4SMichael Contreras         (size > MAXIMUM_ETHERNET_VLAN_SIZE
9972c0331f4SMichael Contreras         && !(s->mac_reg[RCTL] & E1000_RCTL_LPE)))
998b0d9ffcdSMichael Contreras         && !(s->mac_reg[RCTL] & E1000_RCTL_SBP)) {
999b0d9ffcdSMichael Contreras         return size;
1000b0d9ffcdSMichael Contreras     }
1001b0d9ffcdSMichael Contreras 
100297410ddeSVincenzo Maffione     if (!receive_filter(s, filter_buf, size)) {
10034f1c942bSMark McLoughlin         return size;
100497410ddeSVincenzo Maffione     }
10057c23b892Sbalrog 
100697410ddeSVincenzo Maffione     if (vlan_enabled(s) && is_vlan_packet(s, filter_buf)) {
100797410ddeSVincenzo Maffione         vlan_special = cpu_to_le16(be16_to_cpup((uint16_t *)(filter_buf
100897410ddeSVincenzo Maffione                                                                 + 14)));
100997410ddeSVincenzo Maffione         iov_ofs = 4;
101097410ddeSVincenzo Maffione         if (filter_buf == iov->iov_base) {
101197410ddeSVincenzo Maffione             memmove(filter_buf + 4, filter_buf, 12);
101297410ddeSVincenzo Maffione         } else {
101397410ddeSVincenzo Maffione             iov_from_buf(iov, iovcnt, 4, filter_buf, 12);
101497410ddeSVincenzo Maffione             while (iov->iov_len <= iov_ofs) {
101597410ddeSVincenzo Maffione                 iov_ofs -= iov->iov_len;
101697410ddeSVincenzo Maffione                 iov++;
101797410ddeSVincenzo Maffione             }
101897410ddeSVincenzo Maffione         }
10198f2e8d1fSaliguori         vlan_status = E1000_RXD_STAT_VP;
10208f2e8d1fSaliguori         size -= 4;
10218f2e8d1fSaliguori     }
10228f2e8d1fSaliguori 
10237c23b892Sbalrog     rdh_start = s->mac_reg[RDH];
1024b19487e2SMichael S. Tsirkin     desc_offset = 0;
1025b19487e2SMichael S. Tsirkin     total_size = size + fcs_len(s);
1026322fd48aSMichael S. Tsirkin     if (!e1000_has_rxbufs(s, total_size)) {
1027322fd48aSMichael S. Tsirkin             set_ics(s, 0, E1000_ICS_RXO);
1028322fd48aSMichael S. Tsirkin             return -1;
1029322fd48aSMichael S. Tsirkin     }
10307c23b892Sbalrog     do {
1031b19487e2SMichael S. Tsirkin         desc_size = total_size - desc_offset;
1032b19487e2SMichael S. Tsirkin         if (desc_size > s->rxbuf_size) {
1033b19487e2SMichael S. Tsirkin             desc_size = s->rxbuf_size;
1034b19487e2SMichael S. Tsirkin         }
1035d17161f6SKevin Wolf         base = rx_desc_base(s) + sizeof(desc) * s->mac_reg[RDH];
1036b08340d5SAndreas Färber         pci_dma_read(d, base, &desc, sizeof(desc));
10378f2e8d1fSaliguori         desc.special = vlan_special;
10388f2e8d1fSaliguori         desc.status |= (vlan_status | E1000_RXD_STAT_DD);
10397c23b892Sbalrog         if (desc.buffer_addr) {
1040b19487e2SMichael S. Tsirkin             if (desc_offset < size) {
104197410ddeSVincenzo Maffione                 size_t iov_copy;
104297410ddeSVincenzo Maffione                 hwaddr ba = le64_to_cpu(desc.buffer_addr);
1043b19487e2SMichael S. Tsirkin                 size_t copy_size = size - desc_offset;
1044b19487e2SMichael S. Tsirkin                 if (copy_size > s->rxbuf_size) {
1045b19487e2SMichael S. Tsirkin                     copy_size = s->rxbuf_size;
1046b19487e2SMichael S. Tsirkin                 }
104797410ddeSVincenzo Maffione                 do {
104897410ddeSVincenzo Maffione                     iov_copy = MIN(copy_size, iov->iov_len - iov_ofs);
104997410ddeSVincenzo Maffione                     pci_dma_write(d, ba, iov->iov_base + iov_ofs, iov_copy);
105097410ddeSVincenzo Maffione                     copy_size -= iov_copy;
105197410ddeSVincenzo Maffione                     ba += iov_copy;
105297410ddeSVincenzo Maffione                     iov_ofs += iov_copy;
105397410ddeSVincenzo Maffione                     if (iov_ofs == iov->iov_len) {
105497410ddeSVincenzo Maffione                         iov++;
105597410ddeSVincenzo Maffione                         iov_ofs = 0;
105697410ddeSVincenzo Maffione                     }
105797410ddeSVincenzo Maffione                 } while (copy_size);
1058b19487e2SMichael S. Tsirkin             }
1059b19487e2SMichael S. Tsirkin             desc_offset += desc_size;
1060b19487e2SMichael S. Tsirkin             desc.length = cpu_to_le16(desc_size);
1061ee912ccfSMichael S. Tsirkin             if (desc_offset >= total_size) {
10627c23b892Sbalrog                 desc.status |= E1000_RXD_STAT_EOP | E1000_RXD_STAT_IXSM;
1063b19487e2SMichael S. Tsirkin             } else {
1064ee912ccfSMichael S. Tsirkin                 /* Guest zeroing out status is not a hardware requirement.
1065ee912ccfSMichael S. Tsirkin                    Clear EOP in case guest didn't do it. */
1066ee912ccfSMichael S. Tsirkin                 desc.status &= ~E1000_RXD_STAT_EOP;
1067b19487e2SMichael S. Tsirkin             }
106843ad7e3eSJes Sorensen         } else { // as per intel docs; skip descriptors with null buf addr
10697c23b892Sbalrog             DBGOUT(RX, "Null RX descriptor!!\n");
107043ad7e3eSJes Sorensen         }
1071b08340d5SAndreas Färber         pci_dma_write(d, base, &desc, sizeof(desc));
10727c23b892Sbalrog 
10737c23b892Sbalrog         if (++s->mac_reg[RDH] * sizeof(desc) >= s->mac_reg[RDLEN])
10747c23b892Sbalrog             s->mac_reg[RDH] = 0;
10757c23b892Sbalrog         /* see comment in start_xmit; same here */
10767c23b892Sbalrog         if (s->mac_reg[RDH] == rdh_start) {
10777c23b892Sbalrog             DBGOUT(RXERR, "RDH wraparound @%x, RDT %x, RDLEN %x\n",
10787c23b892Sbalrog                    rdh_start, s->mac_reg[RDT], s->mac_reg[RDLEN]);
10797c23b892Sbalrog             set_ics(s, 0, E1000_ICS_RXO);
10804f1c942bSMark McLoughlin             return -1;
10817c23b892Sbalrog         }
1082b19487e2SMichael S. Tsirkin     } while (desc_offset < total_size);
10837c23b892Sbalrog 
10847c23b892Sbalrog     s->mac_reg[GPRC]++;
10857c23b892Sbalrog     s->mac_reg[TPR]++;
1086a05e8a6eSMichael S. Tsirkin     /* TOR - Total Octets Received:
1087a05e8a6eSMichael S. Tsirkin      * This register includes bytes received in a packet from the <Destination
1088a05e8a6eSMichael S. Tsirkin      * Address> field through the <CRC> field, inclusively.
1089a05e8a6eSMichael S. Tsirkin      */
1090a05e8a6eSMichael S. Tsirkin     n = s->mac_reg[TORL] + size + /* Always include FCS length. */ 4;
1091a05e8a6eSMichael S. Tsirkin     if (n < s->mac_reg[TORL])
10927c23b892Sbalrog         s->mac_reg[TORH]++;
1093a05e8a6eSMichael S. Tsirkin     s->mac_reg[TORL] = n;
10947c23b892Sbalrog 
10957c23b892Sbalrog     n = E1000_ICS_RXT0;
10967c23b892Sbalrog     if ((rdt = s->mac_reg[RDT]) < s->mac_reg[RDH])
10977c23b892Sbalrog         rdt += s->mac_reg[RDLEN] / sizeof(desc);
1098bf16cc8fSaliguori     if (((rdt - s->mac_reg[RDH]) * sizeof(desc)) <= s->mac_reg[RDLEN] >>
1099bf16cc8fSaliguori         s->rxbuf_min_shift)
11007c23b892Sbalrog         n |= E1000_ICS_RXDMT0;
11017c23b892Sbalrog 
11027c23b892Sbalrog     set_ics(s, 0, n);
11034f1c942bSMark McLoughlin 
11044f1c942bSMark McLoughlin     return size;
11057c23b892Sbalrog }
11067c23b892Sbalrog 
110797410ddeSVincenzo Maffione static ssize_t
110897410ddeSVincenzo Maffione e1000_receive(NetClientState *nc, const uint8_t *buf, size_t size)
110997410ddeSVincenzo Maffione {
111097410ddeSVincenzo Maffione     const struct iovec iov = {
111197410ddeSVincenzo Maffione         .iov_base = (uint8_t *)buf,
111297410ddeSVincenzo Maffione         .iov_len = size
111397410ddeSVincenzo Maffione     };
111497410ddeSVincenzo Maffione 
111597410ddeSVincenzo Maffione     return e1000_receive_iov(nc, &iov, 1);
111697410ddeSVincenzo Maffione }
111797410ddeSVincenzo Maffione 
11187c23b892Sbalrog static uint32_t
11197c23b892Sbalrog mac_readreg(E1000State *s, int index)
11207c23b892Sbalrog {
11217c23b892Sbalrog     return s->mac_reg[index];
11227c23b892Sbalrog }
11237c23b892Sbalrog 
11247c23b892Sbalrog static uint32_t
11257c23b892Sbalrog mac_icr_read(E1000State *s, int index)
11267c23b892Sbalrog {
11277c23b892Sbalrog     uint32_t ret = s->mac_reg[ICR];
11287c23b892Sbalrog 
11297c23b892Sbalrog     DBGOUT(INTERRUPT, "ICR read: %x\n", ret);
11307c23b892Sbalrog     set_interrupt_cause(s, 0, 0);
11317c23b892Sbalrog     return ret;
11327c23b892Sbalrog }
11337c23b892Sbalrog 
11347c23b892Sbalrog static uint32_t
11357c23b892Sbalrog mac_read_clr4(E1000State *s, int index)
11367c23b892Sbalrog {
11377c23b892Sbalrog     uint32_t ret = s->mac_reg[index];
11387c23b892Sbalrog 
11397c23b892Sbalrog     s->mac_reg[index] = 0;
11407c23b892Sbalrog     return ret;
11417c23b892Sbalrog }
11427c23b892Sbalrog 
11437c23b892Sbalrog static uint32_t
11447c23b892Sbalrog mac_read_clr8(E1000State *s, int index)
11457c23b892Sbalrog {
11467c23b892Sbalrog     uint32_t ret = s->mac_reg[index];
11477c23b892Sbalrog 
11487c23b892Sbalrog     s->mac_reg[index] = 0;
11497c23b892Sbalrog     s->mac_reg[index-1] = 0;
11507c23b892Sbalrog     return ret;
11517c23b892Sbalrog }
11527c23b892Sbalrog 
11537c23b892Sbalrog static void
11547c23b892Sbalrog mac_writereg(E1000State *s, int index, uint32_t val)
11557c23b892Sbalrog {
11567c36507cSAmos Kong     uint32_t macaddr[2];
11577c36507cSAmos Kong 
11587c23b892Sbalrog     s->mac_reg[index] = val;
11597c36507cSAmos Kong 
116090d131fbSMichael S. Tsirkin     if (index == RA + 1) {
11617c36507cSAmos Kong         macaddr[0] = cpu_to_le32(s->mac_reg[RA]);
11627c36507cSAmos Kong         macaddr[1] = cpu_to_le32(s->mac_reg[RA + 1]);
11637c36507cSAmos Kong         qemu_format_nic_info_str(qemu_get_queue(s->nic), (uint8_t *)macaddr);
11647c36507cSAmos Kong     }
11657c23b892Sbalrog }
11667c23b892Sbalrog 
11677c23b892Sbalrog static void
11687c23b892Sbalrog set_rdt(E1000State *s, int index, uint32_t val)
11697c23b892Sbalrog {
11707c23b892Sbalrog     s->mac_reg[index] = val & 0xffff;
1171e8b4c680SPaolo Bonzini     if (e1000_has_rxbufs(s, 1)) {
1172b356f76dSJason Wang         qemu_flush_queued_packets(qemu_get_queue(s->nic));
1173e8b4c680SPaolo Bonzini     }
11747c23b892Sbalrog }
11757c23b892Sbalrog 
11767c23b892Sbalrog static void
11777c23b892Sbalrog set_16bit(E1000State *s, int index, uint32_t val)
11787c23b892Sbalrog {
11797c23b892Sbalrog     s->mac_reg[index] = val & 0xffff;
11807c23b892Sbalrog }
11817c23b892Sbalrog 
11827c23b892Sbalrog static void
11837c23b892Sbalrog set_dlen(E1000State *s, int index, uint32_t val)
11847c23b892Sbalrog {
11857c23b892Sbalrog     s->mac_reg[index] = val & 0xfff80;
11867c23b892Sbalrog }
11877c23b892Sbalrog 
11887c23b892Sbalrog static void
11897c23b892Sbalrog set_tctl(E1000State *s, int index, uint32_t val)
11907c23b892Sbalrog {
11917c23b892Sbalrog     s->mac_reg[index] = val;
11927c23b892Sbalrog     s->mac_reg[TDT] &= 0xffff;
11937c23b892Sbalrog     start_xmit(s);
11947c23b892Sbalrog }
11957c23b892Sbalrog 
11967c23b892Sbalrog static void
11977c23b892Sbalrog set_icr(E1000State *s, int index, uint32_t val)
11987c23b892Sbalrog {
11997c23b892Sbalrog     DBGOUT(INTERRUPT, "set_icr %x\n", val);
12007c23b892Sbalrog     set_interrupt_cause(s, 0, s->mac_reg[ICR] & ~val);
12017c23b892Sbalrog }
12027c23b892Sbalrog 
12037c23b892Sbalrog static void
12047c23b892Sbalrog set_imc(E1000State *s, int index, uint32_t val)
12057c23b892Sbalrog {
12067c23b892Sbalrog     s->mac_reg[IMS] &= ~val;
12077c23b892Sbalrog     set_ics(s, 0, 0);
12087c23b892Sbalrog }
12097c23b892Sbalrog 
12107c23b892Sbalrog static void
12117c23b892Sbalrog set_ims(E1000State *s, int index, uint32_t val)
12127c23b892Sbalrog {
12137c23b892Sbalrog     s->mac_reg[IMS] |= val;
12147c23b892Sbalrog     set_ics(s, 0, 0);
12157c23b892Sbalrog }
12167c23b892Sbalrog 
12177c23b892Sbalrog #define getreg(x)    [x] = mac_readreg
12187c23b892Sbalrog static uint32_t (*macreg_readops[])(E1000State *, int) = {
12197c23b892Sbalrog     getreg(PBA),      getreg(RCTL),     getreg(TDH),      getreg(TXDCTL),
12207c23b892Sbalrog     getreg(WUFC),     getreg(TDT),      getreg(CTRL),     getreg(LEDCTL),
12217c23b892Sbalrog     getreg(MANC),     getreg(MDIC),     getreg(SWSM),     getreg(STATUS),
12227c23b892Sbalrog     getreg(TORL),     getreg(TOTL),     getreg(IMS),      getreg(TCTL),
1223b1332393SBill Paul     getreg(RDH),      getreg(RDT),      getreg(VET),      getreg(ICS),
1224a00b2335SKay Ackermann     getreg(TDBAL),    getreg(TDBAH),    getreg(RDBAH),    getreg(RDBAL),
1225e9845f09SVincenzo Maffione     getreg(TDLEN),    getreg(RDLEN),    getreg(RDTR),     getreg(RADV),
1226e9845f09SVincenzo Maffione     getreg(TADV),     getreg(ITR),
12277c23b892Sbalrog 
122820f3e863SLeonid Bloch     [TOTH]    = mac_read_clr8,      [TORH]    = mac_read_clr8,
122920f3e863SLeonid Bloch     [GPRC]    = mac_read_clr4,      [GPTC]    = mac_read_clr4,
123020f3e863SLeonid Bloch     [TPT]     = mac_read_clr4,      [TPR]     = mac_read_clr4,
123120f3e863SLeonid Bloch     [ICR]     = mac_icr_read,       [EECD]    = get_eecd,
123220f3e863SLeonid Bloch     [EERD]    = flash_eerd_read,
123320f3e863SLeonid Bloch 
12347c23b892Sbalrog     [CRCERRS ... MPC]   = &mac_readreg,
12357c23b892Sbalrog     [RA ... RA+31]      = &mac_readreg,
12367c23b892Sbalrog     [MTA ... MTA+127]   = &mac_readreg,
12378f2e8d1fSaliguori     [VFTA ... VFTA+127] = &mac_readreg,
12387c23b892Sbalrog };
1239b1503cdaSmalc enum { NREADOPS = ARRAY_SIZE(macreg_readops) };
12407c23b892Sbalrog 
12417c23b892Sbalrog #define putreg(x)    [x] = mac_writereg
12427c23b892Sbalrog static void (*macreg_writeops[])(E1000State *, int, uint32_t) = {
12437c23b892Sbalrog     putreg(PBA),      putreg(EERD),     putreg(SWSM),     putreg(WUFC),
12447c23b892Sbalrog     putreg(TDBAL),    putreg(TDBAH),    putreg(TXDCTL),   putreg(RDBAH),
1245cab3c825SKevin Wolf     putreg(RDBAL),    putreg(LEDCTL),   putreg(VET),
124620f3e863SLeonid Bloch 
12477c23b892Sbalrog     [TDLEN]  = set_dlen,   [RDLEN]  = set_dlen,       [TCTL] = set_tctl,
12487c23b892Sbalrog     [TDT]    = set_tctl,   [MDIC]   = set_mdic,       [ICS]  = set_ics,
12497c23b892Sbalrog     [TDH]    = set_16bit,  [RDH]    = set_16bit,      [RDT]  = set_rdt,
12507c23b892Sbalrog     [IMC]    = set_imc,    [IMS]    = set_ims,        [ICR]  = set_icr,
1251cab3c825SKevin Wolf     [EECD]   = set_eecd,   [RCTL]   = set_rx_control, [CTRL] = set_ctrl,
1252e9845f09SVincenzo Maffione     [RDTR]   = set_16bit,  [RADV]   = set_16bit,      [TADV] = set_16bit,
1253e9845f09SVincenzo Maffione     [ITR]    = set_16bit,
125420f3e863SLeonid Bloch 
12557c23b892Sbalrog     [RA ... RA+31]      = &mac_writereg,
12567c23b892Sbalrog     [MTA ... MTA+127]   = &mac_writereg,
12578f2e8d1fSaliguori     [VFTA ... VFTA+127] = &mac_writereg,
12587c23b892Sbalrog };
1259b9d03e35SJason Wang 
1260b1503cdaSmalc enum { NWRITEOPS = ARRAY_SIZE(macreg_writeops) };
12617c23b892Sbalrog 
1262*bc0f0674SLeonid Bloch enum { MAC_ACCESS_PARTIAL = 1, MAC_ACCESS_FLAG_NEEDED = 2 };
1263*bc0f0674SLeonid Bloch 
1264*bc0f0674SLeonid Bloch #define markflag(x)    ((E1000_FLAG_##x << 2) | MAC_ACCESS_FLAG_NEEDED)
1265*bc0f0674SLeonid Bloch /* In the array below the meaning of the bits is: [f|f|f|f|f|f|n|p]
1266*bc0f0674SLeonid Bloch  * f - flag bits (up to 6 possible flags)
1267*bc0f0674SLeonid Bloch  * n - flag needed
1268*bc0f0674SLeonid Bloch  * p - partially implenented */
1269*bc0f0674SLeonid Bloch static const uint8_t mac_reg_access[0x8000] = {
1270*bc0f0674SLeonid Bloch     [RDTR]    = markflag(MIT),    [TADV]    = markflag(MIT),
1271*bc0f0674SLeonid Bloch     [RADV]    = markflag(MIT),    [ITR]     = markflag(MIT),
1272*bc0f0674SLeonid Bloch };
1273*bc0f0674SLeonid Bloch 
12747c23b892Sbalrog static void
1275a8170e5eSAvi Kivity e1000_mmio_write(void *opaque, hwaddr addr, uint64_t val,
1276ad00a9b9SAvi Kivity                  unsigned size)
12777c23b892Sbalrog {
12787c23b892Sbalrog     E1000State *s = opaque;
12798da3ff18Spbrook     unsigned int index = (addr & 0x1ffff) >> 2;
12807c23b892Sbalrog 
128143ad7e3eSJes Sorensen     if (index < NWRITEOPS && macreg_writeops[index]) {
1282*bc0f0674SLeonid Bloch         if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED)
1283*bc0f0674SLeonid Bloch             || (s->compat_flags & (mac_reg_access[index] >> 2))) {
1284*bc0f0674SLeonid Bloch             if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
1285*bc0f0674SLeonid Bloch                 DBGOUT(GENERAL, "Writing to register at offset: 0x%08x. "
1286*bc0f0674SLeonid Bloch                        "It is not fully implemented.\n", index<<2);
1287*bc0f0674SLeonid Bloch             }
12886b59fc74Saurel32             macreg_writeops[index](s, index, val);
1289*bc0f0674SLeonid Bloch         } else {    /* "flag needed" bit is set, but the flag is not active */
1290*bc0f0674SLeonid Bloch             DBGOUT(MMIO, "MMIO write attempt to disabled reg. addr=0x%08x\n",
1291*bc0f0674SLeonid Bloch                    index<<2);
1292*bc0f0674SLeonid Bloch         }
129343ad7e3eSJes Sorensen     } else if (index < NREADOPS && macreg_readops[index]) {
1294*bc0f0674SLeonid Bloch         DBGOUT(MMIO, "e1000_mmio_writel RO %x: 0x%04"PRIx64"\n",
1295*bc0f0674SLeonid Bloch                index<<2, val);
129643ad7e3eSJes Sorensen     } else {
1297ad00a9b9SAvi Kivity         DBGOUT(UNKNOWN, "MMIO unknown write addr=0x%08x,val=0x%08"PRIx64"\n",
12987c23b892Sbalrog                index<<2, val);
12997c23b892Sbalrog     }
130043ad7e3eSJes Sorensen }
13017c23b892Sbalrog 
1302ad00a9b9SAvi Kivity static uint64_t
1303a8170e5eSAvi Kivity e1000_mmio_read(void *opaque, hwaddr addr, unsigned size)
13047c23b892Sbalrog {
13057c23b892Sbalrog     E1000State *s = opaque;
13068da3ff18Spbrook     unsigned int index = (addr & 0x1ffff) >> 2;
13077c23b892Sbalrog 
1308*bc0f0674SLeonid Bloch     if (index < NREADOPS && macreg_readops[index]) {
1309*bc0f0674SLeonid Bloch         if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED)
1310*bc0f0674SLeonid Bloch             || (s->compat_flags & (mac_reg_access[index] >> 2))) {
1311*bc0f0674SLeonid Bloch             if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
1312*bc0f0674SLeonid Bloch                 DBGOUT(GENERAL, "Reading register at offset: 0x%08x. "
1313*bc0f0674SLeonid Bloch                        "It is not fully implemented.\n", index<<2);
13146b59fc74Saurel32             }
1315*bc0f0674SLeonid Bloch             return macreg_readops[index](s, index);
1316*bc0f0674SLeonid Bloch         } else {    /* "flag needed" bit is set, but the flag is not active */
1317*bc0f0674SLeonid Bloch             DBGOUT(MMIO, "MMIO read attempt of disabled reg. addr=0x%08x\n",
1318*bc0f0674SLeonid Bloch                    index<<2);
1319*bc0f0674SLeonid Bloch         }
1320*bc0f0674SLeonid Bloch     } else {
13217c23b892Sbalrog         DBGOUT(UNKNOWN, "MMIO unknown read addr=0x%08x\n", index<<2);
1322*bc0f0674SLeonid Bloch     }
13237c23b892Sbalrog     return 0;
13247c23b892Sbalrog }
13257c23b892Sbalrog 
1326ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_mmio_ops = {
1327ad00a9b9SAvi Kivity     .read = e1000_mmio_read,
1328ad00a9b9SAvi Kivity     .write = e1000_mmio_write,
1329ad00a9b9SAvi Kivity     .endianness = DEVICE_LITTLE_ENDIAN,
1330ad00a9b9SAvi Kivity     .impl = {
1331ad00a9b9SAvi Kivity         .min_access_size = 4,
1332ad00a9b9SAvi Kivity         .max_access_size = 4,
1333ad00a9b9SAvi Kivity     },
1334ad00a9b9SAvi Kivity };
1335ad00a9b9SAvi Kivity 
1336a8170e5eSAvi Kivity static uint64_t e1000_io_read(void *opaque, hwaddr addr,
1337ad00a9b9SAvi Kivity                               unsigned size)
13387c23b892Sbalrog {
1339ad00a9b9SAvi Kivity     E1000State *s = opaque;
1340ad00a9b9SAvi Kivity 
1341ad00a9b9SAvi Kivity     (void)s;
1342ad00a9b9SAvi Kivity     return 0;
13437c23b892Sbalrog }
13447c23b892Sbalrog 
1345a8170e5eSAvi Kivity static void e1000_io_write(void *opaque, hwaddr addr,
1346ad00a9b9SAvi Kivity                            uint64_t val, unsigned size)
13477c23b892Sbalrog {
1348ad00a9b9SAvi Kivity     E1000State *s = opaque;
1349ad00a9b9SAvi Kivity 
1350ad00a9b9SAvi Kivity     (void)s;
13517c23b892Sbalrog }
13527c23b892Sbalrog 
1353ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_io_ops = {
1354ad00a9b9SAvi Kivity     .read = e1000_io_read,
1355ad00a9b9SAvi Kivity     .write = e1000_io_write,
1356ad00a9b9SAvi Kivity     .endianness = DEVICE_LITTLE_ENDIAN,
1357ad00a9b9SAvi Kivity };
1358ad00a9b9SAvi Kivity 
1359e482dc3eSJuan Quintela static bool is_version_1(void *opaque, int version_id)
13607c23b892Sbalrog {
1361e482dc3eSJuan Quintela     return version_id == 1;
13627c23b892Sbalrog }
13637c23b892Sbalrog 
1364ddcb73b7SMichael S. Tsirkin static void e1000_pre_save(void *opaque)
1365ddcb73b7SMichael S. Tsirkin {
1366ddcb73b7SMichael S. Tsirkin     E1000State *s = opaque;
1367ddcb73b7SMichael S. Tsirkin     NetClientState *nc = qemu_get_queue(s->nic);
13682af234e6SMichael S. Tsirkin 
1369e9845f09SVincenzo Maffione     /* If the mitigation timer is active, emulate a timeout now. */
1370e9845f09SVincenzo Maffione     if (s->mit_timer_on) {
1371e9845f09SVincenzo Maffione         e1000_mit_timer(s);
1372e9845f09SVincenzo Maffione     }
1373e9845f09SVincenzo Maffione 
1374ddcb73b7SMichael S. Tsirkin     /*
13756a2acedbSGabriel L. Somlo      * If link is down and auto-negotiation is supported and ongoing,
13766a2acedbSGabriel L. Somlo      * complete auto-negotiation immediately. This allows us to look
13776a2acedbSGabriel L. Somlo      * at MII_SR_AUTONEG_COMPLETE to infer link status on load.
1378ddcb73b7SMichael S. Tsirkin      */
1379d7a41552SGabriel L. Somlo     if (nc->link_down && have_autoneg(s)) {
1380ddcb73b7SMichael S. Tsirkin         s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
1381ddcb73b7SMichael S. Tsirkin     }
1382ddcb73b7SMichael S. Tsirkin }
1383ddcb73b7SMichael S. Tsirkin 
1384e4b82364SAmos Kong static int e1000_post_load(void *opaque, int version_id)
1385e4b82364SAmos Kong {
1386e4b82364SAmos Kong     E1000State *s = opaque;
1387b356f76dSJason Wang     NetClientState *nc = qemu_get_queue(s->nic);
1388e4b82364SAmos Kong 
1389*bc0f0674SLeonid Bloch     if (!chkflag(MIT)) {
1390e9845f09SVincenzo Maffione         s->mac_reg[ITR] = s->mac_reg[RDTR] = s->mac_reg[RADV] =
1391e9845f09SVincenzo Maffione             s->mac_reg[TADV] = 0;
1392e9845f09SVincenzo Maffione         s->mit_irq_level = false;
1393e9845f09SVincenzo Maffione     }
1394e9845f09SVincenzo Maffione     s->mit_ide = 0;
1395e9845f09SVincenzo Maffione     s->mit_timer_on = false;
1396e9845f09SVincenzo Maffione 
1397e4b82364SAmos Kong     /* nc.link_down can't be migrated, so infer link_down according
1398ddcb73b7SMichael S. Tsirkin      * to link status bit in mac_reg[STATUS].
1399ddcb73b7SMichael S. Tsirkin      * Alternatively, restart link negotiation if it was in progress. */
1400b356f76dSJason Wang     nc->link_down = (s->mac_reg[STATUS] & E1000_STATUS_LU) == 0;
14012af234e6SMichael S. Tsirkin 
1402d7a41552SGabriel L. Somlo     if (have_autoneg(s) &&
1403ddcb73b7SMichael S. Tsirkin         !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
1404ddcb73b7SMichael S. Tsirkin         nc->link_down = false;
1405d7a41552SGabriel L. Somlo         timer_mod(s->autoneg_timer,
1406d7a41552SGabriel L. Somlo                   qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
1407ddcb73b7SMichael S. Tsirkin     }
1408e4b82364SAmos Kong 
1409e4b82364SAmos Kong     return 0;
1410e4b82364SAmos Kong }
1411e4b82364SAmos Kong 
1412e9845f09SVincenzo Maffione static bool e1000_mit_state_needed(void *opaque)
1413e9845f09SVincenzo Maffione {
1414e9845f09SVincenzo Maffione     E1000State *s = opaque;
1415e9845f09SVincenzo Maffione 
1416*bc0f0674SLeonid Bloch     return chkflag(MIT);
1417e9845f09SVincenzo Maffione }
1418e9845f09SVincenzo Maffione 
14199e117734SLeonid Bloch static bool e1000_full_mac_needed(void *opaque)
14209e117734SLeonid Bloch {
14219e117734SLeonid Bloch     E1000State *s = opaque;
14229e117734SLeonid Bloch 
1423*bc0f0674SLeonid Bloch     return chkflag(MAC);
14249e117734SLeonid Bloch }
14259e117734SLeonid Bloch 
1426e9845f09SVincenzo Maffione static const VMStateDescription vmstate_e1000_mit_state = {
1427e9845f09SVincenzo Maffione     .name = "e1000/mit_state",
1428e9845f09SVincenzo Maffione     .version_id = 1,
1429e9845f09SVincenzo Maffione     .minimum_version_id = 1,
14305cd8cadaSJuan Quintela     .needed = e1000_mit_state_needed,
1431e9845f09SVincenzo Maffione     .fields = (VMStateField[]) {
1432e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[RDTR], E1000State),
1433e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[RADV], E1000State),
1434e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[TADV], E1000State),
1435e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[ITR], E1000State),
1436e9845f09SVincenzo Maffione         VMSTATE_BOOL(mit_irq_level, E1000State),
1437e9845f09SVincenzo Maffione         VMSTATE_END_OF_LIST()
1438e9845f09SVincenzo Maffione     }
1439e9845f09SVincenzo Maffione };
1440e9845f09SVincenzo Maffione 
14419e117734SLeonid Bloch static const VMStateDescription vmstate_e1000_full_mac_state = {
14429e117734SLeonid Bloch     .name = "e1000/full_mac_state",
14439e117734SLeonid Bloch     .version_id = 1,
14449e117734SLeonid Bloch     .minimum_version_id = 1,
14459e117734SLeonid Bloch     .needed = e1000_full_mac_needed,
14469e117734SLeonid Bloch     .fields = (VMStateField[]) {
14479e117734SLeonid Bloch         VMSTATE_UINT32_ARRAY(mac_reg, E1000State, 0x8000),
14489e117734SLeonid Bloch         VMSTATE_END_OF_LIST()
14499e117734SLeonid Bloch     }
14509e117734SLeonid Bloch };
14519e117734SLeonid Bloch 
1452e482dc3eSJuan Quintela static const VMStateDescription vmstate_e1000 = {
1453e482dc3eSJuan Quintela     .name = "e1000",
1454e482dc3eSJuan Quintela     .version_id = 2,
1455e482dc3eSJuan Quintela     .minimum_version_id = 1,
1456ddcb73b7SMichael S. Tsirkin     .pre_save = e1000_pre_save,
1457e4b82364SAmos Kong     .post_load = e1000_post_load,
1458e482dc3eSJuan Quintela     .fields = (VMStateField[]) {
1459b08340d5SAndreas Färber         VMSTATE_PCI_DEVICE(parent_obj, E1000State),
1460e482dc3eSJuan Quintela         VMSTATE_UNUSED_TEST(is_version_1, 4), /* was instance id */
1461e482dc3eSJuan Quintela         VMSTATE_UNUSED(4), /* Was mmio_base.  */
1462e482dc3eSJuan Quintela         VMSTATE_UINT32(rxbuf_size, E1000State),
1463e482dc3eSJuan Quintela         VMSTATE_UINT32(rxbuf_min_shift, E1000State),
1464e482dc3eSJuan Quintela         VMSTATE_UINT32(eecd_state.val_in, E1000State),
1465e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.bitnum_in, E1000State),
1466e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.bitnum_out, E1000State),
1467e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.reading, E1000State),
1468e482dc3eSJuan Quintela         VMSTATE_UINT32(eecd_state.old_eecd, E1000State),
1469e482dc3eSJuan Quintela         VMSTATE_UINT8(tx.ipcss, E1000State),
1470e482dc3eSJuan Quintela         VMSTATE_UINT8(tx.ipcso, E1000State),
1471e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.ipcse, E1000State),
1472e482dc3eSJuan Quintela         VMSTATE_UINT8(tx.tucss, E1000State),
1473e482dc3eSJuan Quintela         VMSTATE_UINT8(tx.tucso, E1000State),
1474e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.tucse, E1000State),
1475e482dc3eSJuan Quintela         VMSTATE_UINT32(tx.paylen, E1000State),
1476e482dc3eSJuan Quintela         VMSTATE_UINT8(tx.hdr_len, E1000State),
1477e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.mss, E1000State),
1478e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.size, E1000State),
1479e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.tso_frames, E1000State),
1480e482dc3eSJuan Quintela         VMSTATE_UINT8(tx.sum_needed, E1000State),
1481e482dc3eSJuan Quintela         VMSTATE_INT8(tx.ip, E1000State),
1482e482dc3eSJuan Quintela         VMSTATE_INT8(tx.tcp, E1000State),
1483e482dc3eSJuan Quintela         VMSTATE_BUFFER(tx.header, E1000State),
1484e482dc3eSJuan Quintela         VMSTATE_BUFFER(tx.data, E1000State),
1485e482dc3eSJuan Quintela         VMSTATE_UINT16_ARRAY(eeprom_data, E1000State, 64),
1486e482dc3eSJuan Quintela         VMSTATE_UINT16_ARRAY(phy_reg, E1000State, 0x20),
1487e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[CTRL], E1000State),
1488e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[EECD], E1000State),
1489e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[EERD], E1000State),
1490e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[GPRC], E1000State),
1491e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[GPTC], E1000State),
1492e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[ICR], E1000State),
1493e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[ICS], E1000State),
1494e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[IMC], E1000State),
1495e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[IMS], E1000State),
1496e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[LEDCTL], E1000State),
1497e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MANC], E1000State),
1498e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MDIC], E1000State),
1499e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MPC], E1000State),
1500e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[PBA], E1000State),
1501e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RCTL], E1000State),
1502e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDBAH], E1000State),
1503e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDBAL], E1000State),
1504e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDH], E1000State),
1505e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDLEN], E1000State),
1506e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDT], E1000State),
1507e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[STATUS], E1000State),
1508e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[SWSM], E1000State),
1509e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TCTL], E1000State),
1510e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDBAH], E1000State),
1511e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDBAL], E1000State),
1512e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDH], E1000State),
1513e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDLEN], E1000State),
1514e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDT], E1000State),
1515e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TORH], E1000State),
1516e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TORL], E1000State),
1517e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TOTH], E1000State),
1518e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TOTL], E1000State),
1519e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TPR], E1000State),
1520e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TPT], E1000State),
1521e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TXDCTL], E1000State),
1522e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[WUFC], E1000State),
1523e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[VET], E1000State),
1524e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, RA, 32),
1525e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, MTA, 128),
1526e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, VFTA, 128),
1527e482dc3eSJuan Quintela         VMSTATE_END_OF_LIST()
1528e9845f09SVincenzo Maffione     },
15295cd8cadaSJuan Quintela     .subsections = (const VMStateDescription*[]) {
15305cd8cadaSJuan Quintela         &vmstate_e1000_mit_state,
15319e117734SLeonid Bloch         &vmstate_e1000_full_mac_state,
15325cd8cadaSJuan Quintela         NULL
15337c23b892Sbalrog     }
1534e482dc3eSJuan Quintela };
15357c23b892Sbalrog 
15368597f2e1SGabriel L. Somlo /*
15378597f2e1SGabriel L. Somlo  * EEPROM contents documented in Tables 5-2 and 5-3, pp. 98-102.
15388597f2e1SGabriel L. Somlo  * Note: A valid DevId will be inserted during pci_e1000_init().
15398597f2e1SGabriel L. Somlo  */
154088b4e9dbSblueswir1 static const uint16_t e1000_eeprom_template[64] = {
15417c23b892Sbalrog     0x0000, 0x0000, 0x0000, 0x0000,      0xffff, 0x0000,      0x0000, 0x0000,
15428597f2e1SGabriel L. Somlo     0x3000, 0x1000, 0x6403, 0 /*DevId*/, 0x8086, 0 /*DevId*/, 0x8086, 0x3040,
15437c23b892Sbalrog     0x0008, 0x2000, 0x7e14, 0x0048,      0x1000, 0x00d8,      0x0000, 0x2700,
15447c23b892Sbalrog     0x6cc9, 0x3150, 0x0722, 0x040b,      0x0984, 0x0000,      0xc000, 0x0706,
15457c23b892Sbalrog     0x1008, 0x0000, 0x0f04, 0x7fff,      0x4d01, 0xffff,      0xffff, 0xffff,
15467c23b892Sbalrog     0xffff, 0xffff, 0xffff, 0xffff,      0xffff, 0xffff,      0xffff, 0xffff,
15477c23b892Sbalrog     0x0100, 0x4000, 0x121c, 0xffff,      0xffff, 0xffff,      0xffff, 0xffff,
15487c23b892Sbalrog     0xffff, 0xffff, 0xffff, 0xffff,      0xffff, 0xffff,      0xffff, 0x0000,
15497c23b892Sbalrog };
15507c23b892Sbalrog 
15517c23b892Sbalrog /* PCI interface */
15527c23b892Sbalrog 
15537c23b892Sbalrog static void
1554ad00a9b9SAvi Kivity e1000_mmio_setup(E1000State *d)
15557c23b892Sbalrog {
1556f65ed4c1Saliguori     int i;
1557f65ed4c1Saliguori     const uint32_t excluded_regs[] = {
1558f65ed4c1Saliguori         E1000_MDIC, E1000_ICR, E1000_ICS, E1000_IMS,
1559f65ed4c1Saliguori         E1000_IMC, E1000_TCTL, E1000_TDT, PNPMMIO_SIZE
1560f65ed4c1Saliguori     };
1561f65ed4c1Saliguori 
1562eedfac6fSPaolo Bonzini     memory_region_init_io(&d->mmio, OBJECT(d), &e1000_mmio_ops, d,
1563eedfac6fSPaolo Bonzini                           "e1000-mmio", PNPMMIO_SIZE);
1564ad00a9b9SAvi Kivity     memory_region_add_coalescing(&d->mmio, 0, excluded_regs[0]);
1565f65ed4c1Saliguori     for (i = 0; excluded_regs[i] != PNPMMIO_SIZE; i++)
1566ad00a9b9SAvi Kivity         memory_region_add_coalescing(&d->mmio, excluded_regs[i] + 4,
1567ad00a9b9SAvi Kivity                                      excluded_regs[i+1] - excluded_regs[i] - 4);
1568eedfac6fSPaolo Bonzini     memory_region_init_io(&d->io, OBJECT(d), &e1000_io_ops, d, "e1000-io", IOPORT_SIZE);
15697c23b892Sbalrog }
15707c23b892Sbalrog 
1571b946a153Saliguori static void
15724b09be85Saliguori pci_e1000_uninit(PCIDevice *dev)
15734b09be85Saliguori {
1574567a3c9eSPeter Crosthwaite     E1000State *d = E1000(dev);
15754b09be85Saliguori 
1576bc72ad67SAlex Bligh     timer_del(d->autoneg_timer);
1577bc72ad67SAlex Bligh     timer_free(d->autoneg_timer);
1578e9845f09SVincenzo Maffione     timer_del(d->mit_timer);
1579e9845f09SVincenzo Maffione     timer_free(d->mit_timer);
1580948ecf21SJason Wang     qemu_del_nic(d->nic);
15814b09be85Saliguori }
15824b09be85Saliguori 
1583a03e2aecSMark McLoughlin static NetClientInfo net_e1000_info = {
15842be64a68SLaszlo Ersek     .type = NET_CLIENT_OPTIONS_KIND_NIC,
1585a03e2aecSMark McLoughlin     .size = sizeof(NICState),
1586a03e2aecSMark McLoughlin     .can_receive = e1000_can_receive,
1587a03e2aecSMark McLoughlin     .receive = e1000_receive,
158897410ddeSVincenzo Maffione     .receive_iov = e1000_receive_iov,
1589a03e2aecSMark McLoughlin     .link_status_changed = e1000_set_link_status,
1590a03e2aecSMark McLoughlin };
1591a03e2aecSMark McLoughlin 
159220302e71SMichael S. Tsirkin static void e1000_write_config(PCIDevice *pci_dev, uint32_t address,
159320302e71SMichael S. Tsirkin                                 uint32_t val, int len)
159420302e71SMichael S. Tsirkin {
159520302e71SMichael S. Tsirkin     E1000State *s = E1000(pci_dev);
159620302e71SMichael S. Tsirkin 
159720302e71SMichael S. Tsirkin     pci_default_write_config(pci_dev, address, val, len);
159820302e71SMichael S. Tsirkin 
159920302e71SMichael S. Tsirkin     if (range_covers_byte(address, len, PCI_COMMAND) &&
160020302e71SMichael S. Tsirkin         (pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
160120302e71SMichael S. Tsirkin         qemu_flush_queued_packets(qemu_get_queue(s->nic));
160220302e71SMichael S. Tsirkin     }
160320302e71SMichael S. Tsirkin }
160420302e71SMichael S. Tsirkin 
160520302e71SMichael S. Tsirkin 
16069af21dbeSMarkus Armbruster static void pci_e1000_realize(PCIDevice *pci_dev, Error **errp)
16077c23b892Sbalrog {
1608567a3c9eSPeter Crosthwaite     DeviceState *dev = DEVICE(pci_dev);
1609567a3c9eSPeter Crosthwaite     E1000State *d = E1000(pci_dev);
16108597f2e1SGabriel L. Somlo     PCIDeviceClass *pdc = PCI_DEVICE_GET_CLASS(pci_dev);
16117c23b892Sbalrog     uint8_t *pci_conf;
16127c23b892Sbalrog     uint16_t checksum = 0;
16137c23b892Sbalrog     int i;
1614fbdaa002SGerd Hoffmann     uint8_t *macaddr;
1615aff427a1SChris Wright 
161620302e71SMichael S. Tsirkin     pci_dev->config_write = e1000_write_config;
161720302e71SMichael S. Tsirkin 
1618b08340d5SAndreas Färber     pci_conf = pci_dev->config;
16197c23b892Sbalrog 
1620a9cbacb0SMichael S. Tsirkin     /* TODO: RST# value should be 0, PCI spec 6.2.4 */
1621a9cbacb0SMichael S. Tsirkin     pci_conf[PCI_CACHE_LINE_SIZE] = 0x10;
16227c23b892Sbalrog 
1623817e0b6fSMichael S. Tsirkin     pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
16247c23b892Sbalrog 
1625ad00a9b9SAvi Kivity     e1000_mmio_setup(d);
16267c23b892Sbalrog 
1627b08340d5SAndreas Färber     pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
16287c23b892Sbalrog 
1629b08340d5SAndreas Färber     pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->io);
16307c23b892Sbalrog 
16317c23b892Sbalrog     memmove(d->eeprom_data, e1000_eeprom_template,
16327c23b892Sbalrog         sizeof e1000_eeprom_template);
1633fbdaa002SGerd Hoffmann     qemu_macaddr_default_if_unset(&d->conf.macaddr);
1634fbdaa002SGerd Hoffmann     macaddr = d->conf.macaddr.a;
16357c23b892Sbalrog     for (i = 0; i < 3; i++)
16369d07d757SPaul Brook         d->eeprom_data[i] = (macaddr[2*i+1]<<8) | macaddr[2*i];
16378597f2e1SGabriel L. Somlo     d->eeprom_data[11] = d->eeprom_data[13] = pdc->device_id;
16387c23b892Sbalrog     for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
16397c23b892Sbalrog         checksum += d->eeprom_data[i];
16407c23b892Sbalrog     checksum = (uint16_t) EEPROM_SUM - checksum;
16417c23b892Sbalrog     d->eeprom_data[EEPROM_CHECKSUM_REG] = checksum;
16427c23b892Sbalrog 
1643a03e2aecSMark McLoughlin     d->nic = qemu_new_nic(&net_e1000_info, &d->conf,
1644567a3c9eSPeter Crosthwaite                           object_get_typename(OBJECT(d)), dev->id, d);
16457c23b892Sbalrog 
1646b356f76dSJason Wang     qemu_format_nic_info_str(qemu_get_queue(d->nic), macaddr);
16471ca4d09aSGleb Natapov 
1648bc72ad67SAlex Bligh     d->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, e1000_autoneg_timer, d);
1649e9845f09SVincenzo Maffione     d->mit_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000_mit_timer, d);
16507c23b892Sbalrog }
16519d07d757SPaul Brook 
1652fbdaa002SGerd Hoffmann static void qdev_e1000_reset(DeviceState *dev)
1653fbdaa002SGerd Hoffmann {
1654567a3c9eSPeter Crosthwaite     E1000State *d = E1000(dev);
1655fbdaa002SGerd Hoffmann     e1000_reset(d);
1656fbdaa002SGerd Hoffmann }
1657fbdaa002SGerd Hoffmann 
165840021f08SAnthony Liguori static Property e1000_properties[] = {
1659fbdaa002SGerd Hoffmann     DEFINE_NIC_PROPERTIES(E1000State, conf),
16602af234e6SMichael S. Tsirkin     DEFINE_PROP_BIT("autonegotiation", E1000State,
16612af234e6SMichael S. Tsirkin                     compat_flags, E1000_FLAG_AUTONEG_BIT, true),
1662e9845f09SVincenzo Maffione     DEFINE_PROP_BIT("mitigation", E1000State,
1663e9845f09SVincenzo Maffione                     compat_flags, E1000_FLAG_MIT_BIT, true),
1664fbdaa002SGerd Hoffmann     DEFINE_PROP_END_OF_LIST(),
166540021f08SAnthony Liguori };
166640021f08SAnthony Liguori 
16678597f2e1SGabriel L. Somlo typedef struct E1000Info {
16688597f2e1SGabriel L. Somlo     const char *name;
16698597f2e1SGabriel L. Somlo     uint16_t   device_id;
16708597f2e1SGabriel L. Somlo     uint8_t    revision;
16718597f2e1SGabriel L. Somlo     uint16_t   phy_id2;
16728597f2e1SGabriel L. Somlo } E1000Info;
16738597f2e1SGabriel L. Somlo 
167440021f08SAnthony Liguori static void e1000_class_init(ObjectClass *klass, void *data)
167540021f08SAnthony Liguori {
167639bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
167740021f08SAnthony Liguori     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
16788597f2e1SGabriel L. Somlo     E1000BaseClass *e = E1000_DEVICE_CLASS(klass);
16798597f2e1SGabriel L. Somlo     const E1000Info *info = data;
168040021f08SAnthony Liguori 
16819af21dbeSMarkus Armbruster     k->realize = pci_e1000_realize;
168240021f08SAnthony Liguori     k->exit = pci_e1000_uninit;
1683c45e5b5bSGerd Hoffmann     k->romfile = "efi-e1000.rom";
168440021f08SAnthony Liguori     k->vendor_id = PCI_VENDOR_ID_INTEL;
16858597f2e1SGabriel L. Somlo     k->device_id = info->device_id;
16868597f2e1SGabriel L. Somlo     k->revision = info->revision;
16878597f2e1SGabriel L. Somlo     e->phy_id2 = info->phy_id2;
168840021f08SAnthony Liguori     k->class_id = PCI_CLASS_NETWORK_ETHERNET;
1689125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
169039bffca2SAnthony Liguori     dc->desc = "Intel Gigabit Ethernet";
169139bffca2SAnthony Liguori     dc->reset = qdev_e1000_reset;
169239bffca2SAnthony Liguori     dc->vmsd = &vmstate_e1000;
169339bffca2SAnthony Liguori     dc->props = e1000_properties;
1694fbdaa002SGerd Hoffmann }
169540021f08SAnthony Liguori 
16965df3bf62SGonglei static void e1000_instance_init(Object *obj)
16975df3bf62SGonglei {
16985df3bf62SGonglei     E1000State *n = E1000(obj);
16995df3bf62SGonglei     device_add_bootindex_property(obj, &n->conf.bootindex,
17005df3bf62SGonglei                                   "bootindex", "/ethernet-phy@0",
17015df3bf62SGonglei                                   DEVICE(n), NULL);
17025df3bf62SGonglei }
17035df3bf62SGonglei 
17048597f2e1SGabriel L. Somlo static const TypeInfo e1000_base_info = {
17058597f2e1SGabriel L. Somlo     .name          = TYPE_E1000_BASE,
170639bffca2SAnthony Liguori     .parent        = TYPE_PCI_DEVICE,
170739bffca2SAnthony Liguori     .instance_size = sizeof(E1000State),
17085df3bf62SGonglei     .instance_init = e1000_instance_init,
17098597f2e1SGabriel L. Somlo     .class_size    = sizeof(E1000BaseClass),
17108597f2e1SGabriel L. Somlo     .abstract      = true,
17118597f2e1SGabriel L. Somlo };
17128597f2e1SGabriel L. Somlo 
17138597f2e1SGabriel L. Somlo static const E1000Info e1000_devices[] = {
17148597f2e1SGabriel L. Somlo     {
171583044020SJason Wang         .name      = "e1000",
17168597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82540EM,
17178597f2e1SGabriel L. Somlo         .revision  = 0x03,
17188597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_8254xx_DEFAULT,
17198597f2e1SGabriel L. Somlo     },
17208597f2e1SGabriel L. Somlo     {
17218597f2e1SGabriel L. Somlo         .name      = "e1000-82544gc",
17228597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82544GC_COPPER,
17238597f2e1SGabriel L. Somlo         .revision  = 0x03,
17248597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_82544x,
17258597f2e1SGabriel L. Somlo     },
17268597f2e1SGabriel L. Somlo     {
17278597f2e1SGabriel L. Somlo         .name      = "e1000-82545em",
17288597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82545EM_COPPER,
17298597f2e1SGabriel L. Somlo         .revision  = 0x03,
17308597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_8254xx_DEFAULT,
17318597f2e1SGabriel L. Somlo     },
17328597f2e1SGabriel L. Somlo };
17338597f2e1SGabriel L. Somlo 
173483f7d43aSAndreas Färber static void e1000_register_types(void)
17359d07d757SPaul Brook {
17368597f2e1SGabriel L. Somlo     int i;
17378597f2e1SGabriel L. Somlo 
17388597f2e1SGabriel L. Somlo     type_register_static(&e1000_base_info);
17398597f2e1SGabriel L. Somlo     for (i = 0; i < ARRAY_SIZE(e1000_devices); i++) {
17408597f2e1SGabriel L. Somlo         const E1000Info *info = &e1000_devices[i];
17418597f2e1SGabriel L. Somlo         TypeInfo type_info = {};
17428597f2e1SGabriel L. Somlo 
17438597f2e1SGabriel L. Somlo         type_info.name = info->name;
17448597f2e1SGabriel L. Somlo         type_info.parent = TYPE_E1000_BASE;
17458597f2e1SGabriel L. Somlo         type_info.class_data = (void *)info;
17468597f2e1SGabriel L. Somlo         type_info.class_init = e1000_class_init;
17475df3bf62SGonglei         type_info.instance_init = e1000_instance_init;
17488597f2e1SGabriel L. Somlo 
17498597f2e1SGabriel L. Somlo         type_register(&type_info);
17508597f2e1SGabriel L. Somlo     }
17519d07d757SPaul Brook }
17529d07d757SPaul Brook 
175383f7d43aSAndreas Färber type_init(e1000_register_types)
1754