xref: /qemu/hw/net/e1000.c (revision 4aeea330f022f45d0dabff6090ecbb98755c2116)
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 
145bc0f0674SLeonid Bloch #define chkflag(x)     (s->compat_flags & E1000_FLAG_##x)
146bc0f0674SLeonid 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),
17572ea771cSLeonid Bloch     defreg(ITR),     defreg(FCRUC),   defreg(TDFH),    defreg(TDFT),
17672ea771cSLeonid Bloch     defreg(TDFHS),   defreg(TDFTS),   defreg(TDFPC),   defreg(RDFH),
17772ea771cSLeonid Bloch     defreg(RDFT),    defreg(RDFHS),   defreg(RDFTS),   defreg(RDFPC),
17872ea771cSLeonid Bloch     defreg(IPAV),    defreg(WUC),     defreg(WUS),     defreg(AIT),
17972ea771cSLeonid Bloch     defreg(IP6AT),   defreg(IP4AT),   defreg(FFLT),    defreg(FFMT),
18072ea771cSLeonid Bloch     defreg(FFVT),    defreg(WUPM),    defreg(PBM),     defreg(SCC),
18172ea771cSLeonid Bloch     defreg(ECOL),    defreg(MCC),     defreg(LATECOL), defreg(COLC),
18272ea771cSLeonid Bloch     defreg(DC),      defreg(TNCRS),   defreg(SEC),     defreg(CEXTERR),
18372ea771cSLeonid Bloch     defreg(RLEC),    defreg(XONRXC),  defreg(XONTXC),  defreg(XOFFRXC),
18472ea771cSLeonid Bloch     defreg(XOFFTXC), defreg(RFC),     defreg(RJC),     defreg(RNBC),
18572ea771cSLeonid Bloch     defreg(TSCTFC),  defreg(MGTPRC),  defreg(MGTPDC),  defreg(MGTPTC)
1867c23b892Sbalrog };
1877c23b892Sbalrog 
18871aadd3cSJason Wang static void
18971aadd3cSJason Wang e1000_link_down(E1000State *s)
19071aadd3cSJason Wang {
19171aadd3cSJason Wang     s->mac_reg[STATUS] &= ~E1000_STATUS_LU;
19271aadd3cSJason Wang     s->phy_reg[PHY_STATUS] &= ~MII_SR_LINK_STATUS;
1936a2acedbSGabriel L. Somlo     s->phy_reg[PHY_STATUS] &= ~MII_SR_AUTONEG_COMPLETE;
1946883b591SGabriel L. Somlo     s->phy_reg[PHY_LP_ABILITY] &= ~MII_LPAR_LPACK;
19571aadd3cSJason Wang }
19671aadd3cSJason Wang 
19771aadd3cSJason Wang static void
19871aadd3cSJason Wang e1000_link_up(E1000State *s)
19971aadd3cSJason Wang {
20071aadd3cSJason Wang     s->mac_reg[STATUS] |= E1000_STATUS_LU;
20171aadd3cSJason Wang     s->phy_reg[PHY_STATUS] |= MII_SR_LINK_STATUS;
2025df6a185SStefan Hajnoczi 
2035df6a185SStefan Hajnoczi     /* E1000_STATUS_LU is tested by e1000_can_receive() */
2045df6a185SStefan Hajnoczi     qemu_flush_queued_packets(qemu_get_queue(s->nic));
20571aadd3cSJason Wang }
20671aadd3cSJason Wang 
2071195fed9SGabriel L. Somlo static bool
2081195fed9SGabriel L. Somlo have_autoneg(E1000State *s)
2091195fed9SGabriel L. Somlo {
210bc0f0674SLeonid Bloch     return chkflag(AUTONEG) && (s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN);
2111195fed9SGabriel L. Somlo }
2121195fed9SGabriel L. Somlo 
213b9d03e35SJason Wang static void
214b9d03e35SJason Wang set_phy_ctrl(E1000State *s, int index, uint16_t val)
215b9d03e35SJason Wang {
2161195fed9SGabriel L. Somlo     /* bits 0-5 reserved; MII_CR_[RESTART_AUTO_NEG,RESET] are self clearing */
2171195fed9SGabriel L. Somlo     s->phy_reg[PHY_CTRL] = val & ~(0x3f |
2181195fed9SGabriel L. Somlo                                    MII_CR_RESET |
2191195fed9SGabriel L. Somlo                                    MII_CR_RESTART_AUTO_NEG);
2201195fed9SGabriel L. Somlo 
2212af234e6SMichael S. Tsirkin     /*
2222af234e6SMichael S. Tsirkin      * QEMU 1.3 does not support link auto-negotiation emulation, so if we
2232af234e6SMichael S. Tsirkin      * migrate during auto negotiation, after migration the link will be
2242af234e6SMichael S. Tsirkin      * down.
2252af234e6SMichael S. Tsirkin      */
2261195fed9SGabriel L. Somlo     if (have_autoneg(s) && (val & MII_CR_RESTART_AUTO_NEG)) {
227b9d03e35SJason Wang         e1000_link_down(s);
228b9d03e35SJason Wang         DBGOUT(PHY, "Start link auto negotiation\n");
2291195fed9SGabriel L. Somlo         timer_mod(s->autoneg_timer,
2301195fed9SGabriel L. Somlo                   qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
231b9d03e35SJason Wang     }
232b9d03e35SJason Wang }
233b9d03e35SJason Wang 
234b9d03e35SJason Wang static void (*phyreg_writeops[])(E1000State *, int, uint16_t) = {
235b9d03e35SJason Wang     [PHY_CTRL] = set_phy_ctrl,
236b9d03e35SJason Wang };
237b9d03e35SJason Wang 
238b9d03e35SJason Wang enum { NPHYWRITEOPS = ARRAY_SIZE(phyreg_writeops) };
239b9d03e35SJason Wang 
2407c23b892Sbalrog enum { PHY_R = 1, PHY_W = 2, PHY_RW = PHY_R | PHY_W };
24188b4e9dbSblueswir1 static const char phy_regcap[0x20] = {
2427c23b892Sbalrog     [PHY_STATUS]      = PHY_R,     [M88E1000_EXT_PHY_SPEC_CTRL] = PHY_RW,
2437c23b892Sbalrog     [PHY_ID1]         = PHY_R,     [M88E1000_PHY_SPEC_CTRL]     = PHY_RW,
2447c23b892Sbalrog     [PHY_CTRL]        = PHY_RW,    [PHY_1000T_CTRL]             = PHY_RW,
2457c23b892Sbalrog     [PHY_LP_ABILITY]  = PHY_R,     [PHY_1000T_STATUS]           = PHY_R,
2467c23b892Sbalrog     [PHY_AUTONEG_ADV] = PHY_RW,    [M88E1000_RX_ERR_CNTR]       = PHY_R,
2476883b591SGabriel L. Somlo     [PHY_ID2]         = PHY_R,     [M88E1000_PHY_SPEC_STATUS]   = PHY_R,
2486883b591SGabriel L. Somlo     [PHY_AUTONEG_EXP] = PHY_R,
2497c23b892Sbalrog };
2507c23b892Sbalrog 
2518597f2e1SGabriel L. Somlo /* PHY_ID2 documented in 8254x_GBe_SDM.pdf, pp. 250 */
252814cd3acSMichael S. Tsirkin static const uint16_t phy_reg_init[] = {
2539616c290SGabriel L. Somlo     [PHY_CTRL]   = MII_CR_SPEED_SELECT_MSB |
2549616c290SGabriel L. Somlo                    MII_CR_FULL_DUPLEX |
2559616c290SGabriel L. Somlo                    MII_CR_AUTO_NEG_EN,
2569616c290SGabriel L. Somlo 
2579616c290SGabriel L. Somlo     [PHY_STATUS] = MII_SR_EXTENDED_CAPS |
2589616c290SGabriel L. Somlo                    MII_SR_LINK_STATUS |   /* link initially up */
2599616c290SGabriel L. Somlo                    MII_SR_AUTONEG_CAPS |
2609616c290SGabriel L. Somlo                    /* MII_SR_AUTONEG_COMPLETE: initially NOT completed */
2619616c290SGabriel L. Somlo                    MII_SR_PREAMBLE_SUPPRESS |
2629616c290SGabriel L. Somlo                    MII_SR_EXTENDED_STATUS |
2639616c290SGabriel L. Somlo                    MII_SR_10T_HD_CAPS |
2649616c290SGabriel L. Somlo                    MII_SR_10T_FD_CAPS |
2659616c290SGabriel L. Somlo                    MII_SR_100X_HD_CAPS |
2669616c290SGabriel L. Somlo                    MII_SR_100X_FD_CAPS,
2679616c290SGabriel L. Somlo 
2689616c290SGabriel L. Somlo     [PHY_ID1] = 0x141,
2699616c290SGabriel L. Somlo     /* [PHY_ID2] configured per DevId, from e1000_reset() */
2709616c290SGabriel L. Somlo     [PHY_AUTONEG_ADV] = 0xde1,
2719616c290SGabriel L. Somlo     [PHY_LP_ABILITY] = 0x1e0,
2729616c290SGabriel L. Somlo     [PHY_1000T_CTRL] = 0x0e00,
2739616c290SGabriel L. Somlo     [PHY_1000T_STATUS] = 0x3c00,
2749616c290SGabriel L. Somlo     [M88E1000_PHY_SPEC_CTRL] = 0x360,
275814cd3acSMichael S. Tsirkin     [M88E1000_PHY_SPEC_STATUS] = 0xac00,
2769616c290SGabriel L. Somlo     [M88E1000_EXT_PHY_SPEC_CTRL] = 0x0d60,
277814cd3acSMichael S. Tsirkin };
278814cd3acSMichael S. Tsirkin 
279814cd3acSMichael S. Tsirkin static const uint32_t mac_reg_init[] = {
280814cd3acSMichael S. Tsirkin     [PBA]     = 0x00100030,
281814cd3acSMichael S. Tsirkin     [LEDCTL]  = 0x602,
282814cd3acSMichael S. Tsirkin     [CTRL]    = E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN0 |
283814cd3acSMichael S. Tsirkin                 E1000_CTRL_SPD_1000 | E1000_CTRL_SLU,
284814cd3acSMichael S. Tsirkin     [STATUS]  = 0x80000000 | E1000_STATUS_GIO_MASTER_ENABLE |
285814cd3acSMichael S. Tsirkin                 E1000_STATUS_ASDV | E1000_STATUS_MTXCKOK |
286814cd3acSMichael S. Tsirkin                 E1000_STATUS_SPEED_1000 | E1000_STATUS_FD |
287814cd3acSMichael S. Tsirkin                 E1000_STATUS_LU,
288814cd3acSMichael S. Tsirkin     [MANC]    = E1000_MANC_EN_MNG2HOST | E1000_MANC_RCV_TCO_EN |
289814cd3acSMichael S. Tsirkin                 E1000_MANC_ARP_EN | E1000_MANC_0298_EN |
290814cd3acSMichael S. Tsirkin                 E1000_MANC_RMCP_EN,
291814cd3acSMichael S. Tsirkin };
292814cd3acSMichael S. Tsirkin 
293e9845f09SVincenzo Maffione /* Helper function, *curr == 0 means the value is not set */
294e9845f09SVincenzo Maffione static inline void
295e9845f09SVincenzo Maffione mit_update_delay(uint32_t *curr, uint32_t value)
296e9845f09SVincenzo Maffione {
297e9845f09SVincenzo Maffione     if (value && (*curr == 0 || value < *curr)) {
298e9845f09SVincenzo Maffione         *curr = value;
299e9845f09SVincenzo Maffione     }
300e9845f09SVincenzo Maffione }
301e9845f09SVincenzo Maffione 
3027c23b892Sbalrog static void
3037c23b892Sbalrog set_interrupt_cause(E1000State *s, int index, uint32_t val)
3047c23b892Sbalrog {
305b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
306e9845f09SVincenzo Maffione     uint32_t pending_ints;
307e9845f09SVincenzo Maffione     uint32_t mit_delay;
308b08340d5SAndreas Färber 
3097c23b892Sbalrog     s->mac_reg[ICR] = val;
310a52a8841SMichael S. Tsirkin 
311a52a8841SMichael S. Tsirkin     /*
312a52a8841SMichael S. Tsirkin      * Make sure ICR and ICS registers have the same value.
313a52a8841SMichael S. Tsirkin      * The spec says that the ICS register is write-only.  However in practice,
314a52a8841SMichael S. Tsirkin      * on real hardware ICS is readable, and for reads it has the same value as
315a52a8841SMichael S. Tsirkin      * ICR (except that ICS does not have the clear on read behaviour of ICR).
316a52a8841SMichael S. Tsirkin      *
317a52a8841SMichael S. Tsirkin      * The VxWorks PRO/1000 driver uses this behaviour.
318a52a8841SMichael S. Tsirkin      */
319b1332393SBill Paul     s->mac_reg[ICS] = val;
320a52a8841SMichael S. Tsirkin 
321e9845f09SVincenzo Maffione     pending_ints = (s->mac_reg[IMS] & s->mac_reg[ICR]);
322e9845f09SVincenzo Maffione     if (!s->mit_irq_level && pending_ints) {
323e9845f09SVincenzo Maffione         /*
324e9845f09SVincenzo Maffione          * Here we detect a potential raising edge. We postpone raising the
325e9845f09SVincenzo Maffione          * interrupt line if we are inside the mitigation delay window
326e9845f09SVincenzo Maffione          * (s->mit_timer_on == 1).
327e9845f09SVincenzo Maffione          * We provide a partial implementation of interrupt mitigation,
328e9845f09SVincenzo Maffione          * emulating only RADV, TADV and ITR (lower 16 bits, 1024ns units for
329e9845f09SVincenzo Maffione          * RADV and TADV, 256ns units for ITR). RDTR is only used to enable
330e9845f09SVincenzo Maffione          * RADV; relative timers based on TIDV and RDTR are not implemented.
331e9845f09SVincenzo Maffione          */
332e9845f09SVincenzo Maffione         if (s->mit_timer_on) {
333e9845f09SVincenzo Maffione             return;
334e9845f09SVincenzo Maffione         }
335bc0f0674SLeonid Bloch         if (chkflag(MIT)) {
336e9845f09SVincenzo Maffione             /* Compute the next mitigation delay according to pending
337e9845f09SVincenzo Maffione              * interrupts and the current values of RADV (provided
338e9845f09SVincenzo Maffione              * RDTR!=0), TADV and ITR.
339e9845f09SVincenzo Maffione              * Then rearm the timer.
340e9845f09SVincenzo Maffione              */
341e9845f09SVincenzo Maffione             mit_delay = 0;
342e9845f09SVincenzo Maffione             if (s->mit_ide &&
343e9845f09SVincenzo Maffione                     (pending_ints & (E1000_ICR_TXQE | E1000_ICR_TXDW))) {
344e9845f09SVincenzo Maffione                 mit_update_delay(&mit_delay, s->mac_reg[TADV] * 4);
345e9845f09SVincenzo Maffione             }
346e9845f09SVincenzo Maffione             if (s->mac_reg[RDTR] && (pending_ints & E1000_ICS_RXT0)) {
347e9845f09SVincenzo Maffione                 mit_update_delay(&mit_delay, s->mac_reg[RADV] * 4);
348e9845f09SVincenzo Maffione             }
349e9845f09SVincenzo Maffione             mit_update_delay(&mit_delay, s->mac_reg[ITR]);
350e9845f09SVincenzo Maffione 
351e9845f09SVincenzo Maffione             if (mit_delay) {
352e9845f09SVincenzo Maffione                 s->mit_timer_on = 1;
353e9845f09SVincenzo Maffione                 timer_mod(s->mit_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
354e9845f09SVincenzo Maffione                           mit_delay * 256);
355e9845f09SVincenzo Maffione             }
356e9845f09SVincenzo Maffione             s->mit_ide = 0;
357e9845f09SVincenzo Maffione         }
358e9845f09SVincenzo Maffione     }
359e9845f09SVincenzo Maffione 
360e9845f09SVincenzo Maffione     s->mit_irq_level = (pending_ints != 0);
3619e64f8a3SMarcel Apfelbaum     pci_set_irq(d, s->mit_irq_level);
362e9845f09SVincenzo Maffione }
363e9845f09SVincenzo Maffione 
364e9845f09SVincenzo Maffione static void
365e9845f09SVincenzo Maffione e1000_mit_timer(void *opaque)
366e9845f09SVincenzo Maffione {
367e9845f09SVincenzo Maffione     E1000State *s = opaque;
368e9845f09SVincenzo Maffione 
369e9845f09SVincenzo Maffione     s->mit_timer_on = 0;
370e9845f09SVincenzo Maffione     /* Call set_interrupt_cause to update the irq level (if necessary). */
371e9845f09SVincenzo Maffione     set_interrupt_cause(s, 0, s->mac_reg[ICR]);
3727c23b892Sbalrog }
3737c23b892Sbalrog 
3747c23b892Sbalrog static void
3757c23b892Sbalrog set_ics(E1000State *s, int index, uint32_t val)
3767c23b892Sbalrog {
3777c23b892Sbalrog     DBGOUT(INTERRUPT, "set_ics %x, ICR %x, IMR %x\n", val, s->mac_reg[ICR],
3787c23b892Sbalrog         s->mac_reg[IMS]);
3797c23b892Sbalrog     set_interrupt_cause(s, 0, val | s->mac_reg[ICR]);
3807c23b892Sbalrog }
3817c23b892Sbalrog 
382d52aec95SGabriel L. Somlo static void
383d52aec95SGabriel L. Somlo e1000_autoneg_timer(void *opaque)
384d52aec95SGabriel L. Somlo {
385d52aec95SGabriel L. Somlo     E1000State *s = opaque;
386d52aec95SGabriel L. Somlo     if (!qemu_get_queue(s->nic)->link_down) {
387d52aec95SGabriel L. Somlo         e1000_link_up(s);
388d52aec95SGabriel L. Somlo         s->phy_reg[PHY_LP_ABILITY] |= MII_LPAR_LPACK;
389d52aec95SGabriel L. Somlo         s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
390d52aec95SGabriel L. Somlo         DBGOUT(PHY, "Auto negotiation is completed\n");
391d52aec95SGabriel L. Somlo         set_ics(s, 0, E1000_ICS_LSC); /* signal link status change to guest */
392d52aec95SGabriel L. Somlo     }
393d52aec95SGabriel L. Somlo }
394d52aec95SGabriel L. Somlo 
3957c23b892Sbalrog static int
3967c23b892Sbalrog rxbufsize(uint32_t v)
3977c23b892Sbalrog {
3987c23b892Sbalrog     v &= E1000_RCTL_BSEX | E1000_RCTL_SZ_16384 | E1000_RCTL_SZ_8192 |
3997c23b892Sbalrog          E1000_RCTL_SZ_4096 | E1000_RCTL_SZ_2048 | E1000_RCTL_SZ_1024 |
4007c23b892Sbalrog          E1000_RCTL_SZ_512 | E1000_RCTL_SZ_256;
4017c23b892Sbalrog     switch (v) {
4027c23b892Sbalrog     case E1000_RCTL_BSEX | E1000_RCTL_SZ_16384:
4037c23b892Sbalrog         return 16384;
4047c23b892Sbalrog     case E1000_RCTL_BSEX | E1000_RCTL_SZ_8192:
4057c23b892Sbalrog         return 8192;
4067c23b892Sbalrog     case E1000_RCTL_BSEX | E1000_RCTL_SZ_4096:
4077c23b892Sbalrog         return 4096;
4087c23b892Sbalrog     case E1000_RCTL_SZ_1024:
4097c23b892Sbalrog         return 1024;
4107c23b892Sbalrog     case E1000_RCTL_SZ_512:
4117c23b892Sbalrog         return 512;
4127c23b892Sbalrog     case E1000_RCTL_SZ_256:
4137c23b892Sbalrog         return 256;
4147c23b892Sbalrog     }
4157c23b892Sbalrog     return 2048;
4167c23b892Sbalrog }
4177c23b892Sbalrog 
418814cd3acSMichael S. Tsirkin static void e1000_reset(void *opaque)
419814cd3acSMichael S. Tsirkin {
420814cd3acSMichael S. Tsirkin     E1000State *d = opaque;
4218597f2e1SGabriel L. Somlo     E1000BaseClass *edc = E1000_DEVICE_GET_CLASS(d);
422372254c6SGabriel L. Somlo     uint8_t *macaddr = d->conf.macaddr.a;
423372254c6SGabriel L. Somlo     int i;
424814cd3acSMichael S. Tsirkin 
425bc72ad67SAlex Bligh     timer_del(d->autoneg_timer);
426e9845f09SVincenzo Maffione     timer_del(d->mit_timer);
427e9845f09SVincenzo Maffione     d->mit_timer_on = 0;
428e9845f09SVincenzo Maffione     d->mit_irq_level = 0;
429e9845f09SVincenzo Maffione     d->mit_ide = 0;
430814cd3acSMichael S. Tsirkin     memset(d->phy_reg, 0, sizeof d->phy_reg);
431814cd3acSMichael S. Tsirkin     memmove(d->phy_reg, phy_reg_init, sizeof phy_reg_init);
4328597f2e1SGabriel L. Somlo     d->phy_reg[PHY_ID2] = edc->phy_id2;
433814cd3acSMichael S. Tsirkin     memset(d->mac_reg, 0, sizeof d->mac_reg);
434814cd3acSMichael S. Tsirkin     memmove(d->mac_reg, mac_reg_init, sizeof mac_reg_init);
435814cd3acSMichael S. Tsirkin     d->rxbuf_min_shift = 1;
436814cd3acSMichael S. Tsirkin     memset(&d->tx, 0, sizeof d->tx);
437814cd3acSMichael S. Tsirkin 
438b356f76dSJason Wang     if (qemu_get_queue(d->nic)->link_down) {
43971aadd3cSJason Wang         e1000_link_down(d);
440814cd3acSMichael S. Tsirkin     }
441372254c6SGabriel L. Somlo 
442372254c6SGabriel L. Somlo     /* Some guests expect pre-initialized RAH/RAL (AddrValid flag + MACaddr) */
443372254c6SGabriel L. Somlo     d->mac_reg[RA] = 0;
444372254c6SGabriel L. Somlo     d->mac_reg[RA + 1] = E1000_RAH_AV;
445372254c6SGabriel L. Somlo     for (i = 0; i < 4; i++) {
446372254c6SGabriel L. Somlo         d->mac_reg[RA] |= macaddr[i] << (8 * i);
447372254c6SGabriel L. Somlo         d->mac_reg[RA + 1] |= (i < 2) ? macaddr[i + 4] << (8 * i) : 0;
448372254c6SGabriel L. Somlo     }
449655d3b63SAmos Kong     qemu_format_nic_info_str(qemu_get_queue(d->nic), macaddr);
450814cd3acSMichael S. Tsirkin }
451814cd3acSMichael S. Tsirkin 
4527c23b892Sbalrog static void
453cab3c825SKevin Wolf set_ctrl(E1000State *s, int index, uint32_t val)
454cab3c825SKevin Wolf {
455cab3c825SKevin Wolf     /* RST is self clearing */
456cab3c825SKevin Wolf     s->mac_reg[CTRL] = val & ~E1000_CTRL_RST;
457cab3c825SKevin Wolf }
458cab3c825SKevin Wolf 
459cab3c825SKevin Wolf static void
4607c23b892Sbalrog set_rx_control(E1000State *s, int index, uint32_t val)
4617c23b892Sbalrog {
4627c23b892Sbalrog     s->mac_reg[RCTL] = val;
4637c23b892Sbalrog     s->rxbuf_size = rxbufsize(val);
4647c23b892Sbalrog     s->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1;
4657c23b892Sbalrog     DBGOUT(RX, "RCTL: %d, mac_reg[RCTL] = 0x%x\n", s->mac_reg[RDT],
4667c23b892Sbalrog            s->mac_reg[RCTL]);
467b356f76dSJason Wang     qemu_flush_queued_packets(qemu_get_queue(s->nic));
4687c23b892Sbalrog }
4697c23b892Sbalrog 
4707c23b892Sbalrog static void
4717c23b892Sbalrog set_mdic(E1000State *s, int index, uint32_t val)
4727c23b892Sbalrog {
4737c23b892Sbalrog     uint32_t data = val & E1000_MDIC_DATA_MASK;
4747c23b892Sbalrog     uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT);
4757c23b892Sbalrog 
4767c23b892Sbalrog     if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) // phy #
4777c23b892Sbalrog         val = s->mac_reg[MDIC] | E1000_MDIC_ERROR;
4787c23b892Sbalrog     else if (val & E1000_MDIC_OP_READ) {
4797c23b892Sbalrog         DBGOUT(MDIC, "MDIC read reg 0x%x\n", addr);
4807c23b892Sbalrog         if (!(phy_regcap[addr] & PHY_R)) {
4817c23b892Sbalrog             DBGOUT(MDIC, "MDIC read reg %x unhandled\n", addr);
4827c23b892Sbalrog             val |= E1000_MDIC_ERROR;
4837c23b892Sbalrog         } else
4847c23b892Sbalrog             val = (val ^ data) | s->phy_reg[addr];
4857c23b892Sbalrog     } else if (val & E1000_MDIC_OP_WRITE) {
4867c23b892Sbalrog         DBGOUT(MDIC, "MDIC write reg 0x%x, value 0x%x\n", addr, data);
4877c23b892Sbalrog         if (!(phy_regcap[addr] & PHY_W)) {
4887c23b892Sbalrog             DBGOUT(MDIC, "MDIC write reg %x unhandled\n", addr);
4897c23b892Sbalrog             val |= E1000_MDIC_ERROR;
490b9d03e35SJason Wang         } else {
491b9d03e35SJason Wang             if (addr < NPHYWRITEOPS && phyreg_writeops[addr]) {
492b9d03e35SJason Wang                 phyreg_writeops[addr](s, index, data);
4931195fed9SGabriel L. Somlo             } else {
4947c23b892Sbalrog                 s->phy_reg[addr] = data;
4957c23b892Sbalrog             }
496b9d03e35SJason Wang         }
4971195fed9SGabriel L. Somlo     }
4987c23b892Sbalrog     s->mac_reg[MDIC] = val | E1000_MDIC_READY;
49917fbbb0bSJason Wang 
50017fbbb0bSJason Wang     if (val & E1000_MDIC_INT_EN) {
5017c23b892Sbalrog         set_ics(s, 0, E1000_ICR_MDAC);
5027c23b892Sbalrog     }
50317fbbb0bSJason Wang }
5047c23b892Sbalrog 
5057c23b892Sbalrog static uint32_t
5067c23b892Sbalrog get_eecd(E1000State *s, int index)
5077c23b892Sbalrog {
5087c23b892Sbalrog     uint32_t ret = E1000_EECD_PRES|E1000_EECD_GNT | s->eecd_state.old_eecd;
5097c23b892Sbalrog 
5107c23b892Sbalrog     DBGOUT(EEPROM, "reading eeprom bit %d (reading %d)\n",
5117c23b892Sbalrog            s->eecd_state.bitnum_out, s->eecd_state.reading);
5127c23b892Sbalrog     if (!s->eecd_state.reading ||
5137c23b892Sbalrog         ((s->eeprom_data[(s->eecd_state.bitnum_out >> 4) & 0x3f] >>
5147c23b892Sbalrog           ((s->eecd_state.bitnum_out & 0xf) ^ 0xf))) & 1)
5157c23b892Sbalrog         ret |= E1000_EECD_DO;
5167c23b892Sbalrog     return ret;
5177c23b892Sbalrog }
5187c23b892Sbalrog 
5197c23b892Sbalrog static void
5207c23b892Sbalrog set_eecd(E1000State *s, int index, uint32_t val)
5217c23b892Sbalrog {
5227c23b892Sbalrog     uint32_t oldval = s->eecd_state.old_eecd;
5237c23b892Sbalrog 
5247c23b892Sbalrog     s->eecd_state.old_eecd = val & (E1000_EECD_SK | E1000_EECD_CS |
5257c23b892Sbalrog             E1000_EECD_DI|E1000_EECD_FWE_MASK|E1000_EECD_REQ);
52620f3e863SLeonid Bloch     if (!(E1000_EECD_CS & val)) {            /* CS inactive; nothing to do */
5279651ac55SIzumi Tsutsui         return;
52820f3e863SLeonid Bloch     }
52920f3e863SLeonid Bloch     if (E1000_EECD_CS & (val ^ oldval)) {    /* CS rise edge; reset state */
5309651ac55SIzumi Tsutsui         s->eecd_state.val_in = 0;
5319651ac55SIzumi Tsutsui         s->eecd_state.bitnum_in = 0;
5329651ac55SIzumi Tsutsui         s->eecd_state.bitnum_out = 0;
5339651ac55SIzumi Tsutsui         s->eecd_state.reading = 0;
5349651ac55SIzumi Tsutsui     }
53520f3e863SLeonid Bloch     if (!(E1000_EECD_SK & (val ^ oldval))) {    /* no clock edge */
5367c23b892Sbalrog         return;
53720f3e863SLeonid Bloch     }
53820f3e863SLeonid Bloch     if (!(E1000_EECD_SK & val)) {               /* falling edge */
5397c23b892Sbalrog         s->eecd_state.bitnum_out++;
5407c23b892Sbalrog         return;
5417c23b892Sbalrog     }
5427c23b892Sbalrog     s->eecd_state.val_in <<= 1;
5437c23b892Sbalrog     if (val & E1000_EECD_DI)
5447c23b892Sbalrog         s->eecd_state.val_in |= 1;
5457c23b892Sbalrog     if (++s->eecd_state.bitnum_in == 9 && !s->eecd_state.reading) {
5467c23b892Sbalrog         s->eecd_state.bitnum_out = ((s->eecd_state.val_in & 0x3f)<<4)-1;
5477c23b892Sbalrog         s->eecd_state.reading = (((s->eecd_state.val_in >> 6) & 7) ==
5487c23b892Sbalrog             EEPROM_READ_OPCODE_MICROWIRE);
5497c23b892Sbalrog     }
5507c23b892Sbalrog     DBGOUT(EEPROM, "eeprom bitnum in %d out %d, reading %d\n",
5517c23b892Sbalrog            s->eecd_state.bitnum_in, s->eecd_state.bitnum_out,
5527c23b892Sbalrog            s->eecd_state.reading);
5537c23b892Sbalrog }
5547c23b892Sbalrog 
5557c23b892Sbalrog static uint32_t
5567c23b892Sbalrog flash_eerd_read(E1000State *s, int x)
5577c23b892Sbalrog {
5587c23b892Sbalrog     unsigned int index, r = s->mac_reg[EERD] & ~E1000_EEPROM_RW_REG_START;
5597c23b892Sbalrog 
560b1332393SBill Paul     if ((s->mac_reg[EERD] & E1000_EEPROM_RW_REG_START) == 0)
561b1332393SBill Paul         return (s->mac_reg[EERD]);
562b1332393SBill Paul 
5637c23b892Sbalrog     if ((index = r >> E1000_EEPROM_RW_ADDR_SHIFT) > EEPROM_CHECKSUM_REG)
564b1332393SBill Paul         return (E1000_EEPROM_RW_REG_DONE | r);
565b1332393SBill Paul 
566b1332393SBill Paul     return ((s->eeprom_data[index] << E1000_EEPROM_RW_REG_DATA) |
567b1332393SBill Paul            E1000_EEPROM_RW_REG_DONE | r);
5687c23b892Sbalrog }
5697c23b892Sbalrog 
5707c23b892Sbalrog static void
5717c23b892Sbalrog putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t css, uint32_t cse)
5727c23b892Sbalrog {
573c6a6a5e3Saliguori     uint32_t sum;
574c6a6a5e3Saliguori 
5757c23b892Sbalrog     if (cse && cse < n)
5767c23b892Sbalrog         n = cse + 1;
577c6a6a5e3Saliguori     if (sloc < n-1) {
578c6a6a5e3Saliguori         sum = net_checksum_add(n-css, data+css);
579d8ee2591SPeter Maydell         stw_be_p(data + sloc, net_checksum_finish(sum));
580c6a6a5e3Saliguori     }
5817c23b892Sbalrog }
5827c23b892Sbalrog 
5831f67f92cSLeonid Bloch static inline void
5841f67f92cSLeonid Bloch inc_reg_if_not_full(E1000State *s, int index)
5851f67f92cSLeonid Bloch {
5861f67f92cSLeonid Bloch     if (s->mac_reg[index] != 0xffffffff) {
5871f67f92cSLeonid Bloch         s->mac_reg[index]++;
5881f67f92cSLeonid Bloch     }
5891f67f92cSLeonid Bloch }
5901f67f92cSLeonid Bloch 
59145e93764SLeonid Bloch static void
59245e93764SLeonid Bloch grow_8reg_if_not_full(E1000State *s, int index, int size)
59345e93764SLeonid Bloch {
59445e93764SLeonid Bloch     uint64_t sum = s->mac_reg[index] | (uint64_t)s->mac_reg[index+1] << 32;
59545e93764SLeonid Bloch 
59645e93764SLeonid Bloch     if (sum + size < sum) {
59745e93764SLeonid Bloch         sum = ~0ULL;
59845e93764SLeonid Bloch     } else {
59945e93764SLeonid Bloch         sum += size;
60045e93764SLeonid Bloch     }
60145e93764SLeonid Bloch     s->mac_reg[index] = sum;
60245e93764SLeonid Bloch     s->mac_reg[index+1] = sum >> 32;
60345e93764SLeonid Bloch }
60445e93764SLeonid Bloch 
6058f2e8d1fSaliguori static inline int
6068f2e8d1fSaliguori vlan_enabled(E1000State *s)
6078f2e8d1fSaliguori {
6088f2e8d1fSaliguori     return ((s->mac_reg[CTRL] & E1000_CTRL_VME) != 0);
6098f2e8d1fSaliguori }
6108f2e8d1fSaliguori 
6118f2e8d1fSaliguori static inline int
6128f2e8d1fSaliguori vlan_rx_filter_enabled(E1000State *s)
6138f2e8d1fSaliguori {
6148f2e8d1fSaliguori     return ((s->mac_reg[RCTL] & E1000_RCTL_VFE) != 0);
6158f2e8d1fSaliguori }
6168f2e8d1fSaliguori 
6178f2e8d1fSaliguori static inline int
6188f2e8d1fSaliguori is_vlan_packet(E1000State *s, const uint8_t *buf)
6198f2e8d1fSaliguori {
6208f2e8d1fSaliguori     return (be16_to_cpup((uint16_t *)(buf + 12)) ==
6214e60a250SShannon Zhao                 le16_to_cpu(s->mac_reg[VET]));
6228f2e8d1fSaliguori }
6238f2e8d1fSaliguori 
6248f2e8d1fSaliguori static inline int
6258f2e8d1fSaliguori is_vlan_txd(uint32_t txd_lower)
6268f2e8d1fSaliguori {
6278f2e8d1fSaliguori     return ((txd_lower & E1000_TXD_CMD_VLE) != 0);
6288f2e8d1fSaliguori }
6298f2e8d1fSaliguori 
63055e8d1ceSMichael S. Tsirkin /* FCS aka Ethernet CRC-32. We don't get it from backends and can't
63155e8d1ceSMichael S. Tsirkin  * fill it in, just pad descriptor length by 4 bytes unless guest
632a05e8a6eSMichael S. Tsirkin  * told us to strip it off the packet. */
63355e8d1ceSMichael S. Tsirkin static inline int
63455e8d1ceSMichael S. Tsirkin fcs_len(E1000State *s)
63555e8d1ceSMichael S. Tsirkin {
63655e8d1ceSMichael S. Tsirkin     return (s->mac_reg[RCTL] & E1000_RCTL_SECRC) ? 0 : 4;
63755e8d1ceSMichael S. Tsirkin }
63855e8d1ceSMichael S. Tsirkin 
6397c23b892Sbalrog static void
64093e37d76SJason Wang e1000_send_packet(E1000State *s, const uint8_t *buf, int size)
64193e37d76SJason Wang {
642b356f76dSJason Wang     NetClientState *nc = qemu_get_queue(s->nic);
64393e37d76SJason Wang     if (s->phy_reg[PHY_CTRL] & MII_CR_LOOPBACK) {
644b356f76dSJason Wang         nc->info->receive(nc, buf, size);
64593e37d76SJason Wang     } else {
646b356f76dSJason Wang         qemu_send_packet(nc, buf, size);
64793e37d76SJason Wang     }
64893e37d76SJason Wang }
64993e37d76SJason Wang 
65093e37d76SJason Wang static void
6517c23b892Sbalrog xmit_seg(E1000State *s)
6527c23b892Sbalrog {
6537c23b892Sbalrog     uint16_t len, *sp;
65445e93764SLeonid Bloch     unsigned int frames = s->tx.tso_frames, css, sofar;
6557c23b892Sbalrog     struct e1000_tx *tp = &s->tx;
6567c23b892Sbalrog 
6571b0009dbSbalrog     if (tp->tse && tp->cptse) {
6587c23b892Sbalrog         css = tp->ipcss;
6597c23b892Sbalrog         DBGOUT(TXSUM, "frames %d size %d ipcss %d\n",
6607c23b892Sbalrog                frames, tp->size, css);
66120f3e863SLeonid Bloch         if (tp->ip) {    /* IPv4 */
662d8ee2591SPeter Maydell             stw_be_p(tp->data+css+2, tp->size - css);
663d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4,
6647c23b892Sbalrog                      be16_to_cpup((uint16_t *)(tp->data+css+4))+frames);
66520f3e863SLeonid Bloch         } else {         /* IPv6 */
666d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4, tp->size - css);
66720f3e863SLeonid Bloch         }
6687c23b892Sbalrog         css = tp->tucss;
6697c23b892Sbalrog         len = tp->size - css;
6707c23b892Sbalrog         DBGOUT(TXSUM, "tcp %d tucss %d len %d\n", tp->tcp, css, len);
6717c23b892Sbalrog         if (tp->tcp) {
6727c23b892Sbalrog             sofar = frames * tp->mss;
6736bd194abSPeter Maydell             stl_be_p(tp->data+css+4, ldl_be_p(tp->data+css+4)+sofar); /* seq */
6747c23b892Sbalrog             if (tp->paylen - sofar > tp->mss)
67520f3e863SLeonid Bloch                 tp->data[css + 13] &= ~9;    /* PSH, FIN */
67620f3e863SLeonid Bloch         } else    /* UDP */
677d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4, len);
6787c23b892Sbalrog         if (tp->sum_needed & E1000_TXD_POPTS_TXSM) {
679e685b4ebSAlex Williamson             unsigned int phsum;
6807c23b892Sbalrog             // add pseudo-header length before checksum calculation
6817c23b892Sbalrog             sp = (uint16_t *)(tp->data + tp->tucso);
682e685b4ebSAlex Williamson             phsum = be16_to_cpup(sp) + len;
683e685b4ebSAlex Williamson             phsum = (phsum >> 16) + (phsum & 0xffff);
684d8ee2591SPeter Maydell             stw_be_p(sp, phsum);
6857c23b892Sbalrog         }
6867c23b892Sbalrog         tp->tso_frames++;
6877c23b892Sbalrog     }
6887c23b892Sbalrog 
6897c23b892Sbalrog     if (tp->sum_needed & E1000_TXD_POPTS_TXSM)
6907c23b892Sbalrog         putsum(tp->data, tp->size, tp->tucso, tp->tucss, tp->tucse);
6917c23b892Sbalrog     if (tp->sum_needed & E1000_TXD_POPTS_IXSM)
6927c23b892Sbalrog         putsum(tp->data, tp->size, tp->ipcso, tp->ipcss, tp->ipcse);
6938f2e8d1fSaliguori     if (tp->vlan_needed) {
694b10fec9bSStefan Weil         memmove(tp->vlan, tp->data, 4);
695b10fec9bSStefan Weil         memmove(tp->data, tp->data + 4, 8);
6968f2e8d1fSaliguori         memcpy(tp->data + 8, tp->vlan_header, 4);
69793e37d76SJason Wang         e1000_send_packet(s, tp->vlan, tp->size + 4);
69820f3e863SLeonid Bloch     } else {
69993e37d76SJason Wang         e1000_send_packet(s, tp->data, tp->size);
70020f3e863SLeonid Bloch     }
70120f3e863SLeonid Bloch 
7021f67f92cSLeonid Bloch     inc_reg_if_not_full(s, TPT);
70345e93764SLeonid Bloch     grow_8reg_if_not_full(s, TOTL, s->tx.size);
7041f67f92cSLeonid Bloch     s->mac_reg[GPTC] = s->mac_reg[TPT];
7057c23b892Sbalrog }
7067c23b892Sbalrog 
7077c23b892Sbalrog static void
7087c23b892Sbalrog process_tx_desc(E1000State *s, struct e1000_tx_desc *dp)
7097c23b892Sbalrog {
710b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
7117c23b892Sbalrog     uint32_t txd_lower = le32_to_cpu(dp->lower.data);
7127c23b892Sbalrog     uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D);
7137c23b892Sbalrog     unsigned int split_size = txd_lower & 0xffff, bytes, sz, op;
714a0ae17a6SAndrew Jones     unsigned int msh = 0xfffff;
7157c23b892Sbalrog     uint64_t addr;
7167c23b892Sbalrog     struct e1000_context_desc *xp = (struct e1000_context_desc *)dp;
7177c23b892Sbalrog     struct e1000_tx *tp = &s->tx;
7187c23b892Sbalrog 
719e9845f09SVincenzo Maffione     s->mit_ide |= (txd_lower & E1000_TXD_CMD_IDE);
72020f3e863SLeonid Bloch     if (dtype == E1000_TXD_CMD_DEXT) {    /* context descriptor */
7217c23b892Sbalrog         op = le32_to_cpu(xp->cmd_and_length);
7227c23b892Sbalrog         tp->ipcss = xp->lower_setup.ip_fields.ipcss;
7237c23b892Sbalrog         tp->ipcso = xp->lower_setup.ip_fields.ipcso;
7247c23b892Sbalrog         tp->ipcse = le16_to_cpu(xp->lower_setup.ip_fields.ipcse);
7257c23b892Sbalrog         tp->tucss = xp->upper_setup.tcp_fields.tucss;
7267c23b892Sbalrog         tp->tucso = xp->upper_setup.tcp_fields.tucso;
7277c23b892Sbalrog         tp->tucse = le16_to_cpu(xp->upper_setup.tcp_fields.tucse);
7287c23b892Sbalrog         tp->paylen = op & 0xfffff;
7297c23b892Sbalrog         tp->hdr_len = xp->tcp_seg_setup.fields.hdr_len;
7307c23b892Sbalrog         tp->mss = le16_to_cpu(xp->tcp_seg_setup.fields.mss);
7317c23b892Sbalrog         tp->ip = (op & E1000_TXD_CMD_IP) ? 1 : 0;
7327c23b892Sbalrog         tp->tcp = (op & E1000_TXD_CMD_TCP) ? 1 : 0;
7337c23b892Sbalrog         tp->tse = (op & E1000_TXD_CMD_TSE) ? 1 : 0;
7347c23b892Sbalrog         tp->tso_frames = 0;
73520f3e863SLeonid Bloch         if (tp->tucso == 0) {    /* this is probably wrong */
7367c23b892Sbalrog             DBGOUT(TXSUM, "TCP/UDP: cso 0!\n");
7377c23b892Sbalrog             tp->tucso = tp->tucss + (tp->tcp ? 16 : 6);
7387c23b892Sbalrog         }
7397c23b892Sbalrog         return;
7401b0009dbSbalrog     } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) {
7411b0009dbSbalrog         // data descriptor
742735e77ecSStefan Hajnoczi         if (tp->size == 0) {
7437c23b892Sbalrog             tp->sum_needed = le32_to_cpu(dp->upper.data) >> 8;
744735e77ecSStefan Hajnoczi         }
7451b0009dbSbalrog         tp->cptse = ( txd_lower & E1000_TXD_CMD_TSE ) ? 1 : 0;
74643ad7e3eSJes Sorensen     } else {
7471b0009dbSbalrog         // legacy descriptor
7481b0009dbSbalrog         tp->cptse = 0;
74943ad7e3eSJes Sorensen     }
7507c23b892Sbalrog 
7518f2e8d1fSaliguori     if (vlan_enabled(s) && is_vlan_txd(txd_lower) &&
7528f2e8d1fSaliguori         (tp->cptse || txd_lower & E1000_TXD_CMD_EOP)) {
7538f2e8d1fSaliguori         tp->vlan_needed = 1;
754d8ee2591SPeter Maydell         stw_be_p(tp->vlan_header,
7554e60a250SShannon Zhao                       le16_to_cpu(s->mac_reg[VET]));
756d8ee2591SPeter Maydell         stw_be_p(tp->vlan_header + 2,
7578f2e8d1fSaliguori                       le16_to_cpu(dp->upper.fields.special));
7588f2e8d1fSaliguori     }
7598f2e8d1fSaliguori 
7607c23b892Sbalrog     addr = le64_to_cpu(dp->buffer_addr);
7611b0009dbSbalrog     if (tp->tse && tp->cptse) {
762a0ae17a6SAndrew Jones         msh = tp->hdr_len + tp->mss;
7637c23b892Sbalrog         do {
7647c23b892Sbalrog             bytes = split_size;
7657c23b892Sbalrog             if (tp->size + bytes > msh)
7667c23b892Sbalrog                 bytes = msh - tp->size;
76765f82df0SAnthony Liguori 
76865f82df0SAnthony Liguori             bytes = MIN(sizeof(tp->data) - tp->size, bytes);
769b08340d5SAndreas Färber             pci_dma_read(d, addr, tp->data + tp->size, bytes);
770a0ae17a6SAndrew Jones             sz = tp->size + bytes;
771a0ae17a6SAndrew Jones             if (sz >= tp->hdr_len && tp->size < tp->hdr_len) {
772a0ae17a6SAndrew Jones                 memmove(tp->header, tp->data, tp->hdr_len);
773a0ae17a6SAndrew Jones             }
7747c23b892Sbalrog             tp->size = sz;
7757c23b892Sbalrog             addr += bytes;
7767c23b892Sbalrog             if (sz == msh) {
7777c23b892Sbalrog                 xmit_seg(s);
778a0ae17a6SAndrew Jones                 memmove(tp->data, tp->header, tp->hdr_len);
779a0ae17a6SAndrew Jones                 tp->size = tp->hdr_len;
7807c23b892Sbalrog             }
781b947ac2bSP J P             split_size -= bytes;
782b947ac2bSP J P         } while (bytes && split_size);
7831b0009dbSbalrog     } else if (!tp->tse && tp->cptse) {
7841b0009dbSbalrog         // context descriptor TSE is not set, while data descriptor TSE is set
785362f5fb5SStefan Weil         DBGOUT(TXERR, "TCP segmentation error\n");
7861b0009dbSbalrog     } else {
78765f82df0SAnthony Liguori         split_size = MIN(sizeof(tp->data) - tp->size, split_size);
788b08340d5SAndreas Färber         pci_dma_read(d, addr, tp->data + tp->size, split_size);
7891b0009dbSbalrog         tp->size += split_size;
7901b0009dbSbalrog     }
7917c23b892Sbalrog 
7927c23b892Sbalrog     if (!(txd_lower & E1000_TXD_CMD_EOP))
7937c23b892Sbalrog         return;
794a0ae17a6SAndrew Jones     if (!(tp->tse && tp->cptse && tp->size < tp->hdr_len)) {
7957c23b892Sbalrog         xmit_seg(s);
796a0ae17a6SAndrew Jones     }
7977c23b892Sbalrog     tp->tso_frames = 0;
7987c23b892Sbalrog     tp->sum_needed = 0;
7998f2e8d1fSaliguori     tp->vlan_needed = 0;
8007c23b892Sbalrog     tp->size = 0;
8011b0009dbSbalrog     tp->cptse = 0;
8027c23b892Sbalrog }
8037c23b892Sbalrog 
8047c23b892Sbalrog static uint32_t
80562ecbd35SEduard - Gabriel Munteanu txdesc_writeback(E1000State *s, dma_addr_t base, struct e1000_tx_desc *dp)
8067c23b892Sbalrog {
807b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
8087c23b892Sbalrog     uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data);
8097c23b892Sbalrog 
8107c23b892Sbalrog     if (!(txd_lower & (E1000_TXD_CMD_RS|E1000_TXD_CMD_RPS)))
8117c23b892Sbalrog         return 0;
8127c23b892Sbalrog     txd_upper = (le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD) &
8137c23b892Sbalrog                 ~(E1000_TXD_STAT_EC | E1000_TXD_STAT_LC | E1000_TXD_STAT_TU);
8147c23b892Sbalrog     dp->upper.data = cpu_to_le32(txd_upper);
815b08340d5SAndreas Färber     pci_dma_write(d, base + ((char *)&dp->upper - (char *)dp),
81600c3a05bSDavid Gibson                   &dp->upper, sizeof(dp->upper));
8177c23b892Sbalrog     return E1000_ICR_TXDW;
8187c23b892Sbalrog }
8197c23b892Sbalrog 
820d17161f6SKevin Wolf static uint64_t tx_desc_base(E1000State *s)
821d17161f6SKevin Wolf {
822d17161f6SKevin Wolf     uint64_t bah = s->mac_reg[TDBAH];
823d17161f6SKevin Wolf     uint64_t bal = s->mac_reg[TDBAL] & ~0xf;
824d17161f6SKevin Wolf 
825d17161f6SKevin Wolf     return (bah << 32) + bal;
826d17161f6SKevin Wolf }
827d17161f6SKevin Wolf 
8287c23b892Sbalrog static void
8297c23b892Sbalrog start_xmit(E1000State *s)
8307c23b892Sbalrog {
831b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
83262ecbd35SEduard - Gabriel Munteanu     dma_addr_t base;
8337c23b892Sbalrog     struct e1000_tx_desc desc;
8347c23b892Sbalrog     uint32_t tdh_start = s->mac_reg[TDH], cause = E1000_ICS_TXQE;
8357c23b892Sbalrog 
8367c23b892Sbalrog     if (!(s->mac_reg[TCTL] & E1000_TCTL_EN)) {
8377c23b892Sbalrog         DBGOUT(TX, "tx disabled\n");
8387c23b892Sbalrog         return;
8397c23b892Sbalrog     }
8407c23b892Sbalrog 
8417c23b892Sbalrog     while (s->mac_reg[TDH] != s->mac_reg[TDT]) {
842d17161f6SKevin Wolf         base = tx_desc_base(s) +
8437c23b892Sbalrog                sizeof(struct e1000_tx_desc) * s->mac_reg[TDH];
844b08340d5SAndreas Färber         pci_dma_read(d, base, &desc, sizeof(desc));
8457c23b892Sbalrog 
8467c23b892Sbalrog         DBGOUT(TX, "index %d: %p : %x %x\n", s->mac_reg[TDH],
8476106075bSths                (void *)(intptr_t)desc.buffer_addr, desc.lower.data,
8487c23b892Sbalrog                desc.upper.data);
8497c23b892Sbalrog 
8507c23b892Sbalrog         process_tx_desc(s, &desc);
85162ecbd35SEduard - Gabriel Munteanu         cause |= txdesc_writeback(s, base, &desc);
8527c23b892Sbalrog 
8537c23b892Sbalrog         if (++s->mac_reg[TDH] * sizeof(desc) >= s->mac_reg[TDLEN])
8547c23b892Sbalrog             s->mac_reg[TDH] = 0;
8557c23b892Sbalrog         /*
8567c23b892Sbalrog          * the following could happen only if guest sw assigns
8577c23b892Sbalrog          * bogus values to TDT/TDLEN.
8587c23b892Sbalrog          * there's nothing too intelligent we could do about this.
8597c23b892Sbalrog          */
8607c23b892Sbalrog         if (s->mac_reg[TDH] == tdh_start) {
8617c23b892Sbalrog             DBGOUT(TXERR, "TDH wraparound @%x, TDT %x, TDLEN %x\n",
8627c23b892Sbalrog                    tdh_start, s->mac_reg[TDT], s->mac_reg[TDLEN]);
8637c23b892Sbalrog             break;
8647c23b892Sbalrog         }
8657c23b892Sbalrog     }
8667c23b892Sbalrog     set_ics(s, 0, cause);
8677c23b892Sbalrog }
8687c23b892Sbalrog 
8697c23b892Sbalrog static int
8707c23b892Sbalrog receive_filter(E1000State *s, const uint8_t *buf, int size)
8717c23b892Sbalrog {
872af2960f9SBlue Swirl     static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
873af2960f9SBlue Swirl     static const int mta_shift[] = {4, 3, 2, 0};
8747c23b892Sbalrog     uint32_t f, rctl = s->mac_reg[RCTL], ra[2], *rp;
875*4aeea330SLeonid Bloch     int isbcast = !memcmp(buf, bcast, sizeof bcast), ismcast = (buf[0] & 1);
8767c23b892Sbalrog 
8778f2e8d1fSaliguori     if (is_vlan_packet(s, buf) && vlan_rx_filter_enabled(s)) {
8788f2e8d1fSaliguori         uint16_t vid = be16_to_cpup((uint16_t *)(buf + 14));
8798f2e8d1fSaliguori         uint32_t vfta = le32_to_cpup((uint32_t *)(s->mac_reg + VFTA) +
8808f2e8d1fSaliguori                                      ((vid >> 5) & 0x7f));
8818f2e8d1fSaliguori         if ((vfta & (1 << (vid & 0x1f))) == 0)
8828f2e8d1fSaliguori             return 0;
8838f2e8d1fSaliguori     }
8848f2e8d1fSaliguori 
885*4aeea330SLeonid Bloch     if (!isbcast && !ismcast && (rctl & E1000_RCTL_UPE)) { /* promiscuous ucast */
8867c23b892Sbalrog         return 1;
887*4aeea330SLeonid Bloch     }
8887c23b892Sbalrog 
889*4aeea330SLeonid Bloch     if (ismcast && (rctl & E1000_RCTL_MPE)) {          /* promiscuous mcast */
8907c23b892Sbalrog         return 1;
891*4aeea330SLeonid Bloch     }
8927c23b892Sbalrog 
893*4aeea330SLeonid Bloch     if (isbcast && (rctl & E1000_RCTL_BAM)) {          /* broadcast enabled */
8947c23b892Sbalrog         return 1;
895*4aeea330SLeonid Bloch     }
8967c23b892Sbalrog 
8977c23b892Sbalrog     for (rp = s->mac_reg + RA; rp < s->mac_reg + RA + 32; rp += 2) {
8987c23b892Sbalrog         if (!(rp[1] & E1000_RAH_AV))
8997c23b892Sbalrog             continue;
9007c23b892Sbalrog         ra[0] = cpu_to_le32(rp[0]);
9017c23b892Sbalrog         ra[1] = cpu_to_le32(rp[1]);
9027c23b892Sbalrog         if (!memcmp(buf, (uint8_t *)ra, 6)) {
9037c23b892Sbalrog             DBGOUT(RXFILTER,
9047c23b892Sbalrog                    "unicast match[%d]: %02x:%02x:%02x:%02x:%02x:%02x\n",
9057c23b892Sbalrog                    (int)(rp - s->mac_reg - RA)/2,
9067c23b892Sbalrog                    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
9077c23b892Sbalrog             return 1;
9087c23b892Sbalrog         }
9097c23b892Sbalrog     }
9107c23b892Sbalrog     DBGOUT(RXFILTER, "unicast mismatch: %02x:%02x:%02x:%02x:%02x:%02x\n",
9117c23b892Sbalrog            buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
9127c23b892Sbalrog 
9137c23b892Sbalrog     f = mta_shift[(rctl >> E1000_RCTL_MO_SHIFT) & 3];
9147c23b892Sbalrog     f = (((buf[5] << 8) | buf[4]) >> f) & 0xfff;
9157c23b892Sbalrog     if (s->mac_reg[MTA + (f >> 5)] & (1 << (f & 0x1f)))
9167c23b892Sbalrog         return 1;
9177c23b892Sbalrog     DBGOUT(RXFILTER,
9187c23b892Sbalrog            "dropping, inexact filter mismatch: %02x:%02x:%02x:%02x:%02x:%02x MO %d MTA[%d] %x\n",
9197c23b892Sbalrog            buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
9207c23b892Sbalrog            (rctl >> E1000_RCTL_MO_SHIFT) & 3, f >> 5,
9217c23b892Sbalrog            s->mac_reg[MTA + (f >> 5)]);
9227c23b892Sbalrog 
9237c23b892Sbalrog     return 0;
9247c23b892Sbalrog }
9257c23b892Sbalrog 
92699ed7e30Saliguori static void
9274e68f7a0SStefan Hajnoczi e1000_set_link_status(NetClientState *nc)
92899ed7e30Saliguori {
929cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
93099ed7e30Saliguori     uint32_t old_status = s->mac_reg[STATUS];
93199ed7e30Saliguori 
932d4044c2aSBjørn Mork     if (nc->link_down) {
93371aadd3cSJason Wang         e1000_link_down(s);
934d4044c2aSBjørn Mork     } else {
935d7a41552SGabriel L. Somlo         if (have_autoneg(s) &&
9366a2acedbSGabriel L. Somlo             !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
9376a2acedbSGabriel L. Somlo             /* emulate auto-negotiation if supported */
9386a2acedbSGabriel L. Somlo             timer_mod(s->autoneg_timer,
9396a2acedbSGabriel L. Somlo                       qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
9406a2acedbSGabriel L. Somlo         } else {
94171aadd3cSJason Wang             e1000_link_up(s);
942d4044c2aSBjørn Mork         }
9436a2acedbSGabriel L. Somlo     }
94499ed7e30Saliguori 
94599ed7e30Saliguori     if (s->mac_reg[STATUS] != old_status)
94699ed7e30Saliguori         set_ics(s, 0, E1000_ICR_LSC);
94799ed7e30Saliguori }
94899ed7e30Saliguori 
949322fd48aSMichael S. Tsirkin static bool e1000_has_rxbufs(E1000State *s, size_t total_size)
950322fd48aSMichael S. Tsirkin {
951322fd48aSMichael S. Tsirkin     int bufs;
952322fd48aSMichael S. Tsirkin     /* Fast-path short packets */
953322fd48aSMichael S. Tsirkin     if (total_size <= s->rxbuf_size) {
954e5b8b0d4SDmitry Fleytman         return s->mac_reg[RDH] != s->mac_reg[RDT];
955322fd48aSMichael S. Tsirkin     }
956322fd48aSMichael S. Tsirkin     if (s->mac_reg[RDH] < s->mac_reg[RDT]) {
957322fd48aSMichael S. Tsirkin         bufs = s->mac_reg[RDT] - s->mac_reg[RDH];
958e5b8b0d4SDmitry Fleytman     } else if (s->mac_reg[RDH] > s->mac_reg[RDT]) {
959322fd48aSMichael S. Tsirkin         bufs = s->mac_reg[RDLEN] /  sizeof(struct e1000_rx_desc) +
960322fd48aSMichael S. Tsirkin             s->mac_reg[RDT] - s->mac_reg[RDH];
961322fd48aSMichael S. Tsirkin     } else {
962322fd48aSMichael S. Tsirkin         return false;
963322fd48aSMichael S. Tsirkin     }
964322fd48aSMichael S. Tsirkin     return total_size <= bufs * s->rxbuf_size;
965322fd48aSMichael S. Tsirkin }
966322fd48aSMichael S. Tsirkin 
9676cdfab28SMichael S. Tsirkin static int
9684e68f7a0SStefan Hajnoczi e1000_can_receive(NetClientState *nc)
9696cdfab28SMichael S. Tsirkin {
970cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
9716cdfab28SMichael S. Tsirkin 
972ddcb73b7SMichael S. Tsirkin     return (s->mac_reg[STATUS] & E1000_STATUS_LU) &&
97320302e71SMichael S. Tsirkin         (s->mac_reg[RCTL] & E1000_RCTL_EN) &&
97420302e71SMichael S. Tsirkin         (s->parent_obj.config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
97520302e71SMichael S. Tsirkin         e1000_has_rxbufs(s, 1);
9766cdfab28SMichael S. Tsirkin }
9776cdfab28SMichael S. Tsirkin 
978d17161f6SKevin Wolf static uint64_t rx_desc_base(E1000State *s)
979d17161f6SKevin Wolf {
980d17161f6SKevin Wolf     uint64_t bah = s->mac_reg[RDBAH];
981d17161f6SKevin Wolf     uint64_t bal = s->mac_reg[RDBAL] & ~0xf;
982d17161f6SKevin Wolf 
983d17161f6SKevin Wolf     return (bah << 32) + bal;
984d17161f6SKevin Wolf }
985d17161f6SKevin Wolf 
9864f1c942bSMark McLoughlin static ssize_t
98797410ddeSVincenzo Maffione e1000_receive_iov(NetClientState *nc, const struct iovec *iov, int iovcnt)
9887c23b892Sbalrog {
989cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
990b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
9917c23b892Sbalrog     struct e1000_rx_desc desc;
99262ecbd35SEduard - Gabriel Munteanu     dma_addr_t base;
9937c23b892Sbalrog     unsigned int n, rdt;
9947c23b892Sbalrog     uint32_t rdh_start;
9958f2e8d1fSaliguori     uint16_t vlan_special = 0;
99697410ddeSVincenzo Maffione     uint8_t vlan_status = 0;
99778aeb23eSStefan Hajnoczi     uint8_t min_buf[MIN_BUF_SIZE];
99897410ddeSVincenzo Maffione     struct iovec min_iov;
99997410ddeSVincenzo Maffione     uint8_t *filter_buf = iov->iov_base;
100097410ddeSVincenzo Maffione     size_t size = iov_size(iov, iovcnt);
100197410ddeSVincenzo Maffione     size_t iov_ofs = 0;
1002b19487e2SMichael S. Tsirkin     size_t desc_offset;
1003b19487e2SMichael S. Tsirkin     size_t desc_size;
1004b19487e2SMichael S. Tsirkin     size_t total_size;
10057c23b892Sbalrog 
1006ddcb73b7SMichael S. Tsirkin     if (!(s->mac_reg[STATUS] & E1000_STATUS_LU)) {
10074f1c942bSMark McLoughlin         return -1;
1008ddcb73b7SMichael S. Tsirkin     }
1009ddcb73b7SMichael S. Tsirkin 
1010ddcb73b7SMichael S. Tsirkin     if (!(s->mac_reg[RCTL] & E1000_RCTL_EN)) {
1011ddcb73b7SMichael S. Tsirkin         return -1;
1012ddcb73b7SMichael S. Tsirkin     }
10137c23b892Sbalrog 
101478aeb23eSStefan Hajnoczi     /* Pad to minimum Ethernet frame length */
101578aeb23eSStefan Hajnoczi     if (size < sizeof(min_buf)) {
101697410ddeSVincenzo Maffione         iov_to_buf(iov, iovcnt, 0, min_buf, size);
101778aeb23eSStefan Hajnoczi         memset(&min_buf[size], 0, sizeof(min_buf) - size);
101897410ddeSVincenzo Maffione         min_iov.iov_base = filter_buf = min_buf;
101997410ddeSVincenzo Maffione         min_iov.iov_len = size = sizeof(min_buf);
102097410ddeSVincenzo Maffione         iovcnt = 1;
102197410ddeSVincenzo Maffione         iov = &min_iov;
102297410ddeSVincenzo Maffione     } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) {
102397410ddeSVincenzo Maffione         /* This is very unlikely, but may happen. */
102497410ddeSVincenzo Maffione         iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN);
102597410ddeSVincenzo Maffione         filter_buf = min_buf;
102678aeb23eSStefan Hajnoczi     }
102778aeb23eSStefan Hajnoczi 
1028b0d9ffcdSMichael Contreras     /* Discard oversized packets if !LPE and !SBP. */
10292c0331f4SMichael Contreras     if ((size > MAXIMUM_ETHERNET_LPE_SIZE ||
10302c0331f4SMichael Contreras         (size > MAXIMUM_ETHERNET_VLAN_SIZE
10312c0331f4SMichael Contreras         && !(s->mac_reg[RCTL] & E1000_RCTL_LPE)))
1032b0d9ffcdSMichael Contreras         && !(s->mac_reg[RCTL] & E1000_RCTL_SBP)) {
1033b0d9ffcdSMichael Contreras         return size;
1034b0d9ffcdSMichael Contreras     }
1035b0d9ffcdSMichael Contreras 
103697410ddeSVincenzo Maffione     if (!receive_filter(s, filter_buf, size)) {
10374f1c942bSMark McLoughlin         return size;
103897410ddeSVincenzo Maffione     }
10397c23b892Sbalrog 
104097410ddeSVincenzo Maffione     if (vlan_enabled(s) && is_vlan_packet(s, filter_buf)) {
104197410ddeSVincenzo Maffione         vlan_special = cpu_to_le16(be16_to_cpup((uint16_t *)(filter_buf
104297410ddeSVincenzo Maffione                                                                 + 14)));
104397410ddeSVincenzo Maffione         iov_ofs = 4;
104497410ddeSVincenzo Maffione         if (filter_buf == iov->iov_base) {
104597410ddeSVincenzo Maffione             memmove(filter_buf + 4, filter_buf, 12);
104697410ddeSVincenzo Maffione         } else {
104797410ddeSVincenzo Maffione             iov_from_buf(iov, iovcnt, 4, filter_buf, 12);
104897410ddeSVincenzo Maffione             while (iov->iov_len <= iov_ofs) {
104997410ddeSVincenzo Maffione                 iov_ofs -= iov->iov_len;
105097410ddeSVincenzo Maffione                 iov++;
105197410ddeSVincenzo Maffione             }
105297410ddeSVincenzo Maffione         }
10538f2e8d1fSaliguori         vlan_status = E1000_RXD_STAT_VP;
10548f2e8d1fSaliguori         size -= 4;
10558f2e8d1fSaliguori     }
10568f2e8d1fSaliguori 
10577c23b892Sbalrog     rdh_start = s->mac_reg[RDH];
1058b19487e2SMichael S. Tsirkin     desc_offset = 0;
1059b19487e2SMichael S. Tsirkin     total_size = size + fcs_len(s);
1060322fd48aSMichael S. Tsirkin     if (!e1000_has_rxbufs(s, total_size)) {
1061322fd48aSMichael S. Tsirkin             set_ics(s, 0, E1000_ICS_RXO);
1062322fd48aSMichael S. Tsirkin             return -1;
1063322fd48aSMichael S. Tsirkin     }
10647c23b892Sbalrog     do {
1065b19487e2SMichael S. Tsirkin         desc_size = total_size - desc_offset;
1066b19487e2SMichael S. Tsirkin         if (desc_size > s->rxbuf_size) {
1067b19487e2SMichael S. Tsirkin             desc_size = s->rxbuf_size;
1068b19487e2SMichael S. Tsirkin         }
1069d17161f6SKevin Wolf         base = rx_desc_base(s) + sizeof(desc) * s->mac_reg[RDH];
1070b08340d5SAndreas Färber         pci_dma_read(d, base, &desc, sizeof(desc));
10718f2e8d1fSaliguori         desc.special = vlan_special;
10728f2e8d1fSaliguori         desc.status |= (vlan_status | E1000_RXD_STAT_DD);
10737c23b892Sbalrog         if (desc.buffer_addr) {
1074b19487e2SMichael S. Tsirkin             if (desc_offset < size) {
107597410ddeSVincenzo Maffione                 size_t iov_copy;
107697410ddeSVincenzo Maffione                 hwaddr ba = le64_to_cpu(desc.buffer_addr);
1077b19487e2SMichael S. Tsirkin                 size_t copy_size = size - desc_offset;
1078b19487e2SMichael S. Tsirkin                 if (copy_size > s->rxbuf_size) {
1079b19487e2SMichael S. Tsirkin                     copy_size = s->rxbuf_size;
1080b19487e2SMichael S. Tsirkin                 }
108197410ddeSVincenzo Maffione                 do {
108297410ddeSVincenzo Maffione                     iov_copy = MIN(copy_size, iov->iov_len - iov_ofs);
108397410ddeSVincenzo Maffione                     pci_dma_write(d, ba, iov->iov_base + iov_ofs, iov_copy);
108497410ddeSVincenzo Maffione                     copy_size -= iov_copy;
108597410ddeSVincenzo Maffione                     ba += iov_copy;
108697410ddeSVincenzo Maffione                     iov_ofs += iov_copy;
108797410ddeSVincenzo Maffione                     if (iov_ofs == iov->iov_len) {
108897410ddeSVincenzo Maffione                         iov++;
108997410ddeSVincenzo Maffione                         iov_ofs = 0;
109097410ddeSVincenzo Maffione                     }
109197410ddeSVincenzo Maffione                 } while (copy_size);
1092b19487e2SMichael S. Tsirkin             }
1093b19487e2SMichael S. Tsirkin             desc_offset += desc_size;
1094b19487e2SMichael S. Tsirkin             desc.length = cpu_to_le16(desc_size);
1095ee912ccfSMichael S. Tsirkin             if (desc_offset >= total_size) {
10967c23b892Sbalrog                 desc.status |= E1000_RXD_STAT_EOP | E1000_RXD_STAT_IXSM;
1097b19487e2SMichael S. Tsirkin             } else {
1098ee912ccfSMichael S. Tsirkin                 /* Guest zeroing out status is not a hardware requirement.
1099ee912ccfSMichael S. Tsirkin                    Clear EOP in case guest didn't do it. */
1100ee912ccfSMichael S. Tsirkin                 desc.status &= ~E1000_RXD_STAT_EOP;
1101b19487e2SMichael S. Tsirkin             }
110243ad7e3eSJes Sorensen         } else { // as per intel docs; skip descriptors with null buf addr
11037c23b892Sbalrog             DBGOUT(RX, "Null RX descriptor!!\n");
110443ad7e3eSJes Sorensen         }
1105b08340d5SAndreas Färber         pci_dma_write(d, base, &desc, sizeof(desc));
11067c23b892Sbalrog 
11077c23b892Sbalrog         if (++s->mac_reg[RDH] * sizeof(desc) >= s->mac_reg[RDLEN])
11087c23b892Sbalrog             s->mac_reg[RDH] = 0;
11097c23b892Sbalrog         /* see comment in start_xmit; same here */
11107c23b892Sbalrog         if (s->mac_reg[RDH] == rdh_start) {
11117c23b892Sbalrog             DBGOUT(RXERR, "RDH wraparound @%x, RDT %x, RDLEN %x\n",
11127c23b892Sbalrog                    rdh_start, s->mac_reg[RDT], s->mac_reg[RDLEN]);
11137c23b892Sbalrog             set_ics(s, 0, E1000_ICS_RXO);
11144f1c942bSMark McLoughlin             return -1;
11157c23b892Sbalrog         }
1116b19487e2SMichael S. Tsirkin     } while (desc_offset < total_size);
11177c23b892Sbalrog 
11181f67f92cSLeonid Bloch     inc_reg_if_not_full(s, TPR);
11191f67f92cSLeonid Bloch     s->mac_reg[GPRC] = s->mac_reg[TPR];
1120a05e8a6eSMichael S. Tsirkin     /* TOR - Total Octets Received:
1121a05e8a6eSMichael S. Tsirkin      * This register includes bytes received in a packet from the <Destination
1122a05e8a6eSMichael S. Tsirkin      * Address> field through the <CRC> field, inclusively.
112345e93764SLeonid Bloch      * Always include FCS length (4) in size.
1124a05e8a6eSMichael S. Tsirkin      */
112545e93764SLeonid Bloch     grow_8reg_if_not_full(s, TORL, size+4);
11267c23b892Sbalrog 
11277c23b892Sbalrog     n = E1000_ICS_RXT0;
11287c23b892Sbalrog     if ((rdt = s->mac_reg[RDT]) < s->mac_reg[RDH])
11297c23b892Sbalrog         rdt += s->mac_reg[RDLEN] / sizeof(desc);
1130bf16cc8fSaliguori     if (((rdt - s->mac_reg[RDH]) * sizeof(desc)) <= s->mac_reg[RDLEN] >>
1131bf16cc8fSaliguori         s->rxbuf_min_shift)
11327c23b892Sbalrog         n |= E1000_ICS_RXDMT0;
11337c23b892Sbalrog 
11347c23b892Sbalrog     set_ics(s, 0, n);
11354f1c942bSMark McLoughlin 
11364f1c942bSMark McLoughlin     return size;
11377c23b892Sbalrog }
11387c23b892Sbalrog 
113997410ddeSVincenzo Maffione static ssize_t
114097410ddeSVincenzo Maffione e1000_receive(NetClientState *nc, const uint8_t *buf, size_t size)
114197410ddeSVincenzo Maffione {
114297410ddeSVincenzo Maffione     const struct iovec iov = {
114397410ddeSVincenzo Maffione         .iov_base = (uint8_t *)buf,
114497410ddeSVincenzo Maffione         .iov_len = size
114597410ddeSVincenzo Maffione     };
114697410ddeSVincenzo Maffione 
114797410ddeSVincenzo Maffione     return e1000_receive_iov(nc, &iov, 1);
114897410ddeSVincenzo Maffione }
114997410ddeSVincenzo Maffione 
11507c23b892Sbalrog static uint32_t
11517c23b892Sbalrog mac_readreg(E1000State *s, int index)
11527c23b892Sbalrog {
11537c23b892Sbalrog     return s->mac_reg[index];
11547c23b892Sbalrog }
11557c23b892Sbalrog 
11567c23b892Sbalrog static uint32_t
115772ea771cSLeonid Bloch mac_low4_read(E1000State *s, int index)
115872ea771cSLeonid Bloch {
115972ea771cSLeonid Bloch     return s->mac_reg[index] & 0xf;
116072ea771cSLeonid Bloch }
116172ea771cSLeonid Bloch 
116272ea771cSLeonid Bloch static uint32_t
116372ea771cSLeonid Bloch mac_low11_read(E1000State *s, int index)
116472ea771cSLeonid Bloch {
116572ea771cSLeonid Bloch     return s->mac_reg[index] & 0x7ff;
116672ea771cSLeonid Bloch }
116772ea771cSLeonid Bloch 
116872ea771cSLeonid Bloch static uint32_t
116972ea771cSLeonid Bloch mac_low13_read(E1000State *s, int index)
117072ea771cSLeonid Bloch {
117172ea771cSLeonid Bloch     return s->mac_reg[index] & 0x1fff;
117272ea771cSLeonid Bloch }
117372ea771cSLeonid Bloch 
117472ea771cSLeonid Bloch static uint32_t
117572ea771cSLeonid Bloch mac_low16_read(E1000State *s, int index)
117672ea771cSLeonid Bloch {
117772ea771cSLeonid Bloch     return s->mac_reg[index] & 0xffff;
117872ea771cSLeonid Bloch }
117972ea771cSLeonid Bloch 
118072ea771cSLeonid Bloch static uint32_t
11817c23b892Sbalrog mac_icr_read(E1000State *s, int index)
11827c23b892Sbalrog {
11837c23b892Sbalrog     uint32_t ret = s->mac_reg[ICR];
11847c23b892Sbalrog 
11857c23b892Sbalrog     DBGOUT(INTERRUPT, "ICR read: %x\n", ret);
11867c23b892Sbalrog     set_interrupt_cause(s, 0, 0);
11877c23b892Sbalrog     return ret;
11887c23b892Sbalrog }
11897c23b892Sbalrog 
11907c23b892Sbalrog static uint32_t
11917c23b892Sbalrog mac_read_clr4(E1000State *s, int index)
11927c23b892Sbalrog {
11937c23b892Sbalrog     uint32_t ret = s->mac_reg[index];
11947c23b892Sbalrog 
11957c23b892Sbalrog     s->mac_reg[index] = 0;
11967c23b892Sbalrog     return ret;
11977c23b892Sbalrog }
11987c23b892Sbalrog 
11997c23b892Sbalrog static uint32_t
12007c23b892Sbalrog mac_read_clr8(E1000State *s, int index)
12017c23b892Sbalrog {
12027c23b892Sbalrog     uint32_t ret = s->mac_reg[index];
12037c23b892Sbalrog 
12047c23b892Sbalrog     s->mac_reg[index] = 0;
12057c23b892Sbalrog     s->mac_reg[index-1] = 0;
12067c23b892Sbalrog     return ret;
12077c23b892Sbalrog }
12087c23b892Sbalrog 
12097c23b892Sbalrog static void
12107c23b892Sbalrog mac_writereg(E1000State *s, int index, uint32_t val)
12117c23b892Sbalrog {
12127c36507cSAmos Kong     uint32_t macaddr[2];
12137c36507cSAmos Kong 
12147c23b892Sbalrog     s->mac_reg[index] = val;
12157c36507cSAmos Kong 
121690d131fbSMichael S. Tsirkin     if (index == RA + 1) {
12177c36507cSAmos Kong         macaddr[0] = cpu_to_le32(s->mac_reg[RA]);
12187c36507cSAmos Kong         macaddr[1] = cpu_to_le32(s->mac_reg[RA + 1]);
12197c36507cSAmos Kong         qemu_format_nic_info_str(qemu_get_queue(s->nic), (uint8_t *)macaddr);
12207c36507cSAmos Kong     }
12217c23b892Sbalrog }
12227c23b892Sbalrog 
12237c23b892Sbalrog static void
12247c23b892Sbalrog set_rdt(E1000State *s, int index, uint32_t val)
12257c23b892Sbalrog {
12267c23b892Sbalrog     s->mac_reg[index] = val & 0xffff;
1227e8b4c680SPaolo Bonzini     if (e1000_has_rxbufs(s, 1)) {
1228b356f76dSJason Wang         qemu_flush_queued_packets(qemu_get_queue(s->nic));
1229e8b4c680SPaolo Bonzini     }
12307c23b892Sbalrog }
12317c23b892Sbalrog 
12327c23b892Sbalrog static void
12337c23b892Sbalrog set_16bit(E1000State *s, int index, uint32_t val)
12347c23b892Sbalrog {
12357c23b892Sbalrog     s->mac_reg[index] = val & 0xffff;
12367c23b892Sbalrog }
12377c23b892Sbalrog 
12387c23b892Sbalrog static void
12397c23b892Sbalrog set_dlen(E1000State *s, int index, uint32_t val)
12407c23b892Sbalrog {
12417c23b892Sbalrog     s->mac_reg[index] = val & 0xfff80;
12427c23b892Sbalrog }
12437c23b892Sbalrog 
12447c23b892Sbalrog static void
12457c23b892Sbalrog set_tctl(E1000State *s, int index, uint32_t val)
12467c23b892Sbalrog {
12477c23b892Sbalrog     s->mac_reg[index] = val;
12487c23b892Sbalrog     s->mac_reg[TDT] &= 0xffff;
12497c23b892Sbalrog     start_xmit(s);
12507c23b892Sbalrog }
12517c23b892Sbalrog 
12527c23b892Sbalrog static void
12537c23b892Sbalrog set_icr(E1000State *s, int index, uint32_t val)
12547c23b892Sbalrog {
12557c23b892Sbalrog     DBGOUT(INTERRUPT, "set_icr %x\n", val);
12567c23b892Sbalrog     set_interrupt_cause(s, 0, s->mac_reg[ICR] & ~val);
12577c23b892Sbalrog }
12587c23b892Sbalrog 
12597c23b892Sbalrog static void
12607c23b892Sbalrog set_imc(E1000State *s, int index, uint32_t val)
12617c23b892Sbalrog {
12627c23b892Sbalrog     s->mac_reg[IMS] &= ~val;
12637c23b892Sbalrog     set_ics(s, 0, 0);
12647c23b892Sbalrog }
12657c23b892Sbalrog 
12667c23b892Sbalrog static void
12677c23b892Sbalrog set_ims(E1000State *s, int index, uint32_t val)
12687c23b892Sbalrog {
12697c23b892Sbalrog     s->mac_reg[IMS] |= val;
12707c23b892Sbalrog     set_ics(s, 0, 0);
12717c23b892Sbalrog }
12727c23b892Sbalrog 
12737c23b892Sbalrog #define getreg(x)    [x] = mac_readreg
12747c23b892Sbalrog static uint32_t (*macreg_readops[])(E1000State *, int) = {
12757c23b892Sbalrog     getreg(PBA),      getreg(RCTL),     getreg(TDH),      getreg(TXDCTL),
12767c23b892Sbalrog     getreg(WUFC),     getreg(TDT),      getreg(CTRL),     getreg(LEDCTL),
12777c23b892Sbalrog     getreg(MANC),     getreg(MDIC),     getreg(SWSM),     getreg(STATUS),
12787c23b892Sbalrog     getreg(TORL),     getreg(TOTL),     getreg(IMS),      getreg(TCTL),
1279b1332393SBill Paul     getreg(RDH),      getreg(RDT),      getreg(VET),      getreg(ICS),
1280a00b2335SKay Ackermann     getreg(TDBAL),    getreg(TDBAH),    getreg(RDBAH),    getreg(RDBAL),
1281e9845f09SVincenzo Maffione     getreg(TDLEN),    getreg(RDLEN),    getreg(RDTR),     getreg(RADV),
128272ea771cSLeonid Bloch     getreg(TADV),     getreg(ITR),      getreg(FCRUC),    getreg(IPAV),
128372ea771cSLeonid Bloch     getreg(WUC),      getreg(WUS),      getreg(SCC),      getreg(ECOL),
128472ea771cSLeonid Bloch     getreg(MCC),      getreg(LATECOL),  getreg(COLC),     getreg(DC),
128572ea771cSLeonid Bloch     getreg(TNCRS),    getreg(SEC),      getreg(CEXTERR),  getreg(RLEC),
128672ea771cSLeonid Bloch     getreg(XONRXC),   getreg(XONTXC),   getreg(XOFFRXC),  getreg(XOFFTXC),
128772ea771cSLeonid Bloch     getreg(RFC),      getreg(RJC),      getreg(RNBC),     getreg(TSCTFC),
128872ea771cSLeonid Bloch     getreg(MGTPRC),   getreg(MGTPDC),   getreg(MGTPTC),
12897c23b892Sbalrog 
129020f3e863SLeonid Bloch     [TOTH]    = mac_read_clr8,      [TORH]    = mac_read_clr8,
129120f3e863SLeonid Bloch     [GPRC]    = mac_read_clr4,      [GPTC]    = mac_read_clr4,
129220f3e863SLeonid Bloch     [TPT]     = mac_read_clr4,      [TPR]     = mac_read_clr4,
129320f3e863SLeonid Bloch     [ICR]     = mac_icr_read,       [EECD]    = get_eecd,
129420f3e863SLeonid Bloch     [EERD]    = flash_eerd_read,
129572ea771cSLeonid Bloch     [RDFH]    = mac_low13_read,     [RDFT]    = mac_low13_read,
129672ea771cSLeonid Bloch     [RDFHS]   = mac_low13_read,     [RDFTS]   = mac_low13_read,
129772ea771cSLeonid Bloch     [RDFPC]   = mac_low13_read,
129872ea771cSLeonid Bloch     [TDFH]    = mac_low11_read,     [TDFT]    = mac_low11_read,
129972ea771cSLeonid Bloch     [TDFHS]   = mac_low13_read,     [TDFTS]   = mac_low13_read,
130072ea771cSLeonid Bloch     [TDFPC]   = mac_low13_read,
130172ea771cSLeonid Bloch     [AIT]     = mac_low16_read,
130220f3e863SLeonid Bloch 
13037c23b892Sbalrog     [CRCERRS ... MPC]   = &mac_readreg,
130472ea771cSLeonid Bloch     [IP6AT ... IP6AT+3] = &mac_readreg,    [IP4AT ... IP4AT+6] = &mac_readreg,
130572ea771cSLeonid Bloch     [FFLT ... FFLT+6]   = &mac_low11_read,
13067c23b892Sbalrog     [RA ... RA+31]      = &mac_readreg,
130772ea771cSLeonid Bloch     [WUPM ... WUPM+31]  = &mac_readreg,
13087c23b892Sbalrog     [MTA ... MTA+127]   = &mac_readreg,
13098f2e8d1fSaliguori     [VFTA ... VFTA+127] = &mac_readreg,
131072ea771cSLeonid Bloch     [FFMT ... FFMT+254] = &mac_low4_read,
131172ea771cSLeonid Bloch     [FFVT ... FFVT+254] = &mac_readreg,
131272ea771cSLeonid Bloch     [PBM ... PBM+16383] = &mac_readreg,
13137c23b892Sbalrog };
1314b1503cdaSmalc enum { NREADOPS = ARRAY_SIZE(macreg_readops) };
13157c23b892Sbalrog 
13167c23b892Sbalrog #define putreg(x)    [x] = mac_writereg
13177c23b892Sbalrog static void (*macreg_writeops[])(E1000State *, int, uint32_t) = {
13187c23b892Sbalrog     putreg(PBA),      putreg(EERD),     putreg(SWSM),     putreg(WUFC),
13197c23b892Sbalrog     putreg(TDBAL),    putreg(TDBAH),    putreg(TXDCTL),   putreg(RDBAH),
132072ea771cSLeonid Bloch     putreg(RDBAL),    putreg(LEDCTL),   putreg(VET),      putreg(FCRUC),
132172ea771cSLeonid Bloch     putreg(TDFH),     putreg(TDFT),     putreg(TDFHS),    putreg(TDFTS),
132272ea771cSLeonid Bloch     putreg(TDFPC),    putreg(RDFH),     putreg(RDFT),     putreg(RDFHS),
132372ea771cSLeonid Bloch     putreg(RDFTS),    putreg(RDFPC),    putreg(IPAV),     putreg(WUC),
132472ea771cSLeonid Bloch     putreg(WUS),      putreg(AIT),
132520f3e863SLeonid Bloch 
13267c23b892Sbalrog     [TDLEN]  = set_dlen,   [RDLEN]  = set_dlen,       [TCTL] = set_tctl,
13277c23b892Sbalrog     [TDT]    = set_tctl,   [MDIC]   = set_mdic,       [ICS]  = set_ics,
13287c23b892Sbalrog     [TDH]    = set_16bit,  [RDH]    = set_16bit,      [RDT]  = set_rdt,
13297c23b892Sbalrog     [IMC]    = set_imc,    [IMS]    = set_ims,        [ICR]  = set_icr,
1330cab3c825SKevin Wolf     [EECD]   = set_eecd,   [RCTL]   = set_rx_control, [CTRL] = set_ctrl,
1331e9845f09SVincenzo Maffione     [RDTR]   = set_16bit,  [RADV]   = set_16bit,      [TADV] = set_16bit,
1332e9845f09SVincenzo Maffione     [ITR]    = set_16bit,
133320f3e863SLeonid Bloch 
133472ea771cSLeonid Bloch     [IP6AT ... IP6AT+3] = &mac_writereg, [IP4AT ... IP4AT+6] = &mac_writereg,
133572ea771cSLeonid Bloch     [FFLT ... FFLT+6]   = &mac_writereg,
13367c23b892Sbalrog     [RA ... RA+31]      = &mac_writereg,
133772ea771cSLeonid Bloch     [WUPM ... WUPM+31]  = &mac_writereg,
13387c23b892Sbalrog     [MTA ... MTA+127]   = &mac_writereg,
13398f2e8d1fSaliguori     [VFTA ... VFTA+127] = &mac_writereg,
134072ea771cSLeonid Bloch     [FFMT ... FFMT+254] = &mac_writereg, [FFVT ... FFVT+254] = &mac_writereg,
134172ea771cSLeonid Bloch     [PBM ... PBM+16383] = &mac_writereg,
13427c23b892Sbalrog };
1343b9d03e35SJason Wang 
1344b1503cdaSmalc enum { NWRITEOPS = ARRAY_SIZE(macreg_writeops) };
13457c23b892Sbalrog 
1346bc0f0674SLeonid Bloch enum { MAC_ACCESS_PARTIAL = 1, MAC_ACCESS_FLAG_NEEDED = 2 };
1347bc0f0674SLeonid Bloch 
1348bc0f0674SLeonid Bloch #define markflag(x)    ((E1000_FLAG_##x << 2) | MAC_ACCESS_FLAG_NEEDED)
1349bc0f0674SLeonid Bloch /* In the array below the meaning of the bits is: [f|f|f|f|f|f|n|p]
1350bc0f0674SLeonid Bloch  * f - flag bits (up to 6 possible flags)
1351bc0f0674SLeonid Bloch  * n - flag needed
1352bc0f0674SLeonid Bloch  * p - partially implenented */
1353bc0f0674SLeonid Bloch static const uint8_t mac_reg_access[0x8000] = {
1354bc0f0674SLeonid Bloch     [RDTR]    = markflag(MIT),    [TADV]    = markflag(MIT),
1355bc0f0674SLeonid Bloch     [RADV]    = markflag(MIT),    [ITR]     = markflag(MIT),
135672ea771cSLeonid Bloch 
135772ea771cSLeonid Bloch     [IPAV]    = markflag(MAC),    [WUC]     = markflag(MAC),
135872ea771cSLeonid Bloch     [IP6AT]   = markflag(MAC),    [IP4AT]   = markflag(MAC),
135972ea771cSLeonid Bloch     [FFVT]    = markflag(MAC),    [WUPM]    = markflag(MAC),
136072ea771cSLeonid Bloch     [ECOL]    = markflag(MAC),    [MCC]     = markflag(MAC),
136172ea771cSLeonid Bloch     [DC]      = markflag(MAC),    [TNCRS]   = markflag(MAC),
136272ea771cSLeonid Bloch     [RLEC]    = markflag(MAC),    [XONRXC]  = markflag(MAC),
136372ea771cSLeonid Bloch     [XOFFTXC] = markflag(MAC),    [RFC]     = markflag(MAC),
136472ea771cSLeonid Bloch     [TSCTFC]  = markflag(MAC),    [MGTPRC]  = markflag(MAC),
136572ea771cSLeonid Bloch     [WUS]     = markflag(MAC),    [AIT]     = markflag(MAC),
136672ea771cSLeonid Bloch     [FFLT]    = markflag(MAC),    [FFMT]    = markflag(MAC),
136772ea771cSLeonid Bloch     [SCC]     = markflag(MAC),    [FCRUC]   = markflag(MAC),
136872ea771cSLeonid Bloch     [LATECOL] = markflag(MAC),    [COLC]    = markflag(MAC),
136972ea771cSLeonid Bloch     [SEC]     = markflag(MAC),    [CEXTERR] = markflag(MAC),
137072ea771cSLeonid Bloch     [XONTXC]  = markflag(MAC),    [XOFFRXC] = markflag(MAC),
137172ea771cSLeonid Bloch     [RJC]     = markflag(MAC),    [RNBC]    = markflag(MAC),
137272ea771cSLeonid Bloch     [MGTPDC]  = markflag(MAC),    [MGTPTC]  = markflag(MAC),
137372ea771cSLeonid Bloch 
137472ea771cSLeonid Bloch     [TDFH]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
137572ea771cSLeonid Bloch     [TDFT]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
137672ea771cSLeonid Bloch     [TDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
137772ea771cSLeonid Bloch     [TDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
137872ea771cSLeonid Bloch     [TDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL,
137972ea771cSLeonid Bloch     [RDFH]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
138072ea771cSLeonid Bloch     [RDFT]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
138172ea771cSLeonid Bloch     [RDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
138272ea771cSLeonid Bloch     [RDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
138372ea771cSLeonid Bloch     [RDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL,
138472ea771cSLeonid Bloch     [PBM]   = markflag(MAC) | MAC_ACCESS_PARTIAL,
1385bc0f0674SLeonid Bloch };
1386bc0f0674SLeonid Bloch 
13877c23b892Sbalrog static void
1388a8170e5eSAvi Kivity e1000_mmio_write(void *opaque, hwaddr addr, uint64_t val,
1389ad00a9b9SAvi Kivity                  unsigned size)
13907c23b892Sbalrog {
13917c23b892Sbalrog     E1000State *s = opaque;
13928da3ff18Spbrook     unsigned int index = (addr & 0x1ffff) >> 2;
13937c23b892Sbalrog 
139443ad7e3eSJes Sorensen     if (index < NWRITEOPS && macreg_writeops[index]) {
1395bc0f0674SLeonid Bloch         if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED)
1396bc0f0674SLeonid Bloch             || (s->compat_flags & (mac_reg_access[index] >> 2))) {
1397bc0f0674SLeonid Bloch             if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
1398bc0f0674SLeonid Bloch                 DBGOUT(GENERAL, "Writing to register at offset: 0x%08x. "
1399bc0f0674SLeonid Bloch                        "It is not fully implemented.\n", index<<2);
1400bc0f0674SLeonid Bloch             }
14016b59fc74Saurel32             macreg_writeops[index](s, index, val);
1402bc0f0674SLeonid Bloch         } else {    /* "flag needed" bit is set, but the flag is not active */
1403bc0f0674SLeonid Bloch             DBGOUT(MMIO, "MMIO write attempt to disabled reg. addr=0x%08x\n",
1404bc0f0674SLeonid Bloch                    index<<2);
1405bc0f0674SLeonid Bloch         }
140643ad7e3eSJes Sorensen     } else if (index < NREADOPS && macreg_readops[index]) {
1407bc0f0674SLeonid Bloch         DBGOUT(MMIO, "e1000_mmio_writel RO %x: 0x%04"PRIx64"\n",
1408bc0f0674SLeonid Bloch                index<<2, val);
140943ad7e3eSJes Sorensen     } else {
1410ad00a9b9SAvi Kivity         DBGOUT(UNKNOWN, "MMIO unknown write addr=0x%08x,val=0x%08"PRIx64"\n",
14117c23b892Sbalrog                index<<2, val);
14127c23b892Sbalrog     }
141343ad7e3eSJes Sorensen }
14147c23b892Sbalrog 
1415ad00a9b9SAvi Kivity static uint64_t
1416a8170e5eSAvi Kivity e1000_mmio_read(void *opaque, hwaddr addr, unsigned size)
14177c23b892Sbalrog {
14187c23b892Sbalrog     E1000State *s = opaque;
14198da3ff18Spbrook     unsigned int index = (addr & 0x1ffff) >> 2;
14207c23b892Sbalrog 
1421bc0f0674SLeonid Bloch     if (index < NREADOPS && macreg_readops[index]) {
1422bc0f0674SLeonid Bloch         if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED)
1423bc0f0674SLeonid Bloch             || (s->compat_flags & (mac_reg_access[index] >> 2))) {
1424bc0f0674SLeonid Bloch             if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
1425bc0f0674SLeonid Bloch                 DBGOUT(GENERAL, "Reading register at offset: 0x%08x. "
1426bc0f0674SLeonid Bloch                        "It is not fully implemented.\n", index<<2);
14276b59fc74Saurel32             }
1428bc0f0674SLeonid Bloch             return macreg_readops[index](s, index);
1429bc0f0674SLeonid Bloch         } else {    /* "flag needed" bit is set, but the flag is not active */
1430bc0f0674SLeonid Bloch             DBGOUT(MMIO, "MMIO read attempt of disabled reg. addr=0x%08x\n",
1431bc0f0674SLeonid Bloch                    index<<2);
1432bc0f0674SLeonid Bloch         }
1433bc0f0674SLeonid Bloch     } else {
14347c23b892Sbalrog         DBGOUT(UNKNOWN, "MMIO unknown read addr=0x%08x\n", index<<2);
1435bc0f0674SLeonid Bloch     }
14367c23b892Sbalrog     return 0;
14377c23b892Sbalrog }
14387c23b892Sbalrog 
1439ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_mmio_ops = {
1440ad00a9b9SAvi Kivity     .read = e1000_mmio_read,
1441ad00a9b9SAvi Kivity     .write = e1000_mmio_write,
1442ad00a9b9SAvi Kivity     .endianness = DEVICE_LITTLE_ENDIAN,
1443ad00a9b9SAvi Kivity     .impl = {
1444ad00a9b9SAvi Kivity         .min_access_size = 4,
1445ad00a9b9SAvi Kivity         .max_access_size = 4,
1446ad00a9b9SAvi Kivity     },
1447ad00a9b9SAvi Kivity };
1448ad00a9b9SAvi Kivity 
1449a8170e5eSAvi Kivity static uint64_t e1000_io_read(void *opaque, hwaddr addr,
1450ad00a9b9SAvi Kivity                               unsigned size)
14517c23b892Sbalrog {
1452ad00a9b9SAvi Kivity     E1000State *s = opaque;
1453ad00a9b9SAvi Kivity 
1454ad00a9b9SAvi Kivity     (void)s;
1455ad00a9b9SAvi Kivity     return 0;
14567c23b892Sbalrog }
14577c23b892Sbalrog 
1458a8170e5eSAvi Kivity static void e1000_io_write(void *opaque, hwaddr addr,
1459ad00a9b9SAvi Kivity                            uint64_t val, unsigned size)
14607c23b892Sbalrog {
1461ad00a9b9SAvi Kivity     E1000State *s = opaque;
1462ad00a9b9SAvi Kivity 
1463ad00a9b9SAvi Kivity     (void)s;
14647c23b892Sbalrog }
14657c23b892Sbalrog 
1466ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_io_ops = {
1467ad00a9b9SAvi Kivity     .read = e1000_io_read,
1468ad00a9b9SAvi Kivity     .write = e1000_io_write,
1469ad00a9b9SAvi Kivity     .endianness = DEVICE_LITTLE_ENDIAN,
1470ad00a9b9SAvi Kivity };
1471ad00a9b9SAvi Kivity 
1472e482dc3eSJuan Quintela static bool is_version_1(void *opaque, int version_id)
14737c23b892Sbalrog {
1474e482dc3eSJuan Quintela     return version_id == 1;
14757c23b892Sbalrog }
14767c23b892Sbalrog 
1477ddcb73b7SMichael S. Tsirkin static void e1000_pre_save(void *opaque)
1478ddcb73b7SMichael S. Tsirkin {
1479ddcb73b7SMichael S. Tsirkin     E1000State *s = opaque;
1480ddcb73b7SMichael S. Tsirkin     NetClientState *nc = qemu_get_queue(s->nic);
14812af234e6SMichael S. Tsirkin 
1482e9845f09SVincenzo Maffione     /* If the mitigation timer is active, emulate a timeout now. */
1483e9845f09SVincenzo Maffione     if (s->mit_timer_on) {
1484e9845f09SVincenzo Maffione         e1000_mit_timer(s);
1485e9845f09SVincenzo Maffione     }
1486e9845f09SVincenzo Maffione 
1487ddcb73b7SMichael S. Tsirkin     /*
14886a2acedbSGabriel L. Somlo      * If link is down and auto-negotiation is supported and ongoing,
14896a2acedbSGabriel L. Somlo      * complete auto-negotiation immediately. This allows us to look
14906a2acedbSGabriel L. Somlo      * at MII_SR_AUTONEG_COMPLETE to infer link status on load.
1491ddcb73b7SMichael S. Tsirkin      */
1492d7a41552SGabriel L. Somlo     if (nc->link_down && have_autoneg(s)) {
1493ddcb73b7SMichael S. Tsirkin         s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
1494ddcb73b7SMichael S. Tsirkin     }
1495ddcb73b7SMichael S. Tsirkin }
1496ddcb73b7SMichael S. Tsirkin 
1497e4b82364SAmos Kong static int e1000_post_load(void *opaque, int version_id)
1498e4b82364SAmos Kong {
1499e4b82364SAmos Kong     E1000State *s = opaque;
1500b356f76dSJason Wang     NetClientState *nc = qemu_get_queue(s->nic);
1501e4b82364SAmos Kong 
1502bc0f0674SLeonid Bloch     if (!chkflag(MIT)) {
1503e9845f09SVincenzo Maffione         s->mac_reg[ITR] = s->mac_reg[RDTR] = s->mac_reg[RADV] =
1504e9845f09SVincenzo Maffione             s->mac_reg[TADV] = 0;
1505e9845f09SVincenzo Maffione         s->mit_irq_level = false;
1506e9845f09SVincenzo Maffione     }
1507e9845f09SVincenzo Maffione     s->mit_ide = 0;
1508e9845f09SVincenzo Maffione     s->mit_timer_on = false;
1509e9845f09SVincenzo Maffione 
1510e4b82364SAmos Kong     /* nc.link_down can't be migrated, so infer link_down according
1511ddcb73b7SMichael S. Tsirkin      * to link status bit in mac_reg[STATUS].
1512ddcb73b7SMichael S. Tsirkin      * Alternatively, restart link negotiation if it was in progress. */
1513b356f76dSJason Wang     nc->link_down = (s->mac_reg[STATUS] & E1000_STATUS_LU) == 0;
15142af234e6SMichael S. Tsirkin 
1515d7a41552SGabriel L. Somlo     if (have_autoneg(s) &&
1516ddcb73b7SMichael S. Tsirkin         !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
1517ddcb73b7SMichael S. Tsirkin         nc->link_down = false;
1518d7a41552SGabriel L. Somlo         timer_mod(s->autoneg_timer,
1519d7a41552SGabriel L. Somlo                   qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
1520ddcb73b7SMichael S. Tsirkin     }
1521e4b82364SAmos Kong 
1522e4b82364SAmos Kong     return 0;
1523e4b82364SAmos Kong }
1524e4b82364SAmos Kong 
1525e9845f09SVincenzo Maffione static bool e1000_mit_state_needed(void *opaque)
1526e9845f09SVincenzo Maffione {
1527e9845f09SVincenzo Maffione     E1000State *s = opaque;
1528e9845f09SVincenzo Maffione 
1529bc0f0674SLeonid Bloch     return chkflag(MIT);
1530e9845f09SVincenzo Maffione }
1531e9845f09SVincenzo Maffione 
15329e117734SLeonid Bloch static bool e1000_full_mac_needed(void *opaque)
15339e117734SLeonid Bloch {
15349e117734SLeonid Bloch     E1000State *s = opaque;
15359e117734SLeonid Bloch 
1536bc0f0674SLeonid Bloch     return chkflag(MAC);
15379e117734SLeonid Bloch }
15389e117734SLeonid Bloch 
1539e9845f09SVincenzo Maffione static const VMStateDescription vmstate_e1000_mit_state = {
1540e9845f09SVincenzo Maffione     .name = "e1000/mit_state",
1541e9845f09SVincenzo Maffione     .version_id = 1,
1542e9845f09SVincenzo Maffione     .minimum_version_id = 1,
15435cd8cadaSJuan Quintela     .needed = e1000_mit_state_needed,
1544e9845f09SVincenzo Maffione     .fields = (VMStateField[]) {
1545e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[RDTR], E1000State),
1546e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[RADV], E1000State),
1547e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[TADV], E1000State),
1548e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[ITR], E1000State),
1549e9845f09SVincenzo Maffione         VMSTATE_BOOL(mit_irq_level, E1000State),
1550e9845f09SVincenzo Maffione         VMSTATE_END_OF_LIST()
1551e9845f09SVincenzo Maffione     }
1552e9845f09SVincenzo Maffione };
1553e9845f09SVincenzo Maffione 
15549e117734SLeonid Bloch static const VMStateDescription vmstate_e1000_full_mac_state = {
15559e117734SLeonid Bloch     .name = "e1000/full_mac_state",
15569e117734SLeonid Bloch     .version_id = 1,
15579e117734SLeonid Bloch     .minimum_version_id = 1,
15589e117734SLeonid Bloch     .needed = e1000_full_mac_needed,
15599e117734SLeonid Bloch     .fields = (VMStateField[]) {
15609e117734SLeonid Bloch         VMSTATE_UINT32_ARRAY(mac_reg, E1000State, 0x8000),
15619e117734SLeonid Bloch         VMSTATE_END_OF_LIST()
15629e117734SLeonid Bloch     }
15639e117734SLeonid Bloch };
15649e117734SLeonid Bloch 
1565e482dc3eSJuan Quintela static const VMStateDescription vmstate_e1000 = {
1566e482dc3eSJuan Quintela     .name = "e1000",
1567e482dc3eSJuan Quintela     .version_id = 2,
1568e482dc3eSJuan Quintela     .minimum_version_id = 1,
1569ddcb73b7SMichael S. Tsirkin     .pre_save = e1000_pre_save,
1570e4b82364SAmos Kong     .post_load = e1000_post_load,
1571e482dc3eSJuan Quintela     .fields = (VMStateField[]) {
1572b08340d5SAndreas Färber         VMSTATE_PCI_DEVICE(parent_obj, E1000State),
1573e482dc3eSJuan Quintela         VMSTATE_UNUSED_TEST(is_version_1, 4), /* was instance id */
1574e482dc3eSJuan Quintela         VMSTATE_UNUSED(4), /* Was mmio_base.  */
1575e482dc3eSJuan Quintela         VMSTATE_UINT32(rxbuf_size, E1000State),
1576e482dc3eSJuan Quintela         VMSTATE_UINT32(rxbuf_min_shift, E1000State),
1577e482dc3eSJuan Quintela         VMSTATE_UINT32(eecd_state.val_in, E1000State),
1578e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.bitnum_in, E1000State),
1579e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.bitnum_out, E1000State),
1580e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.reading, E1000State),
1581e482dc3eSJuan Quintela         VMSTATE_UINT32(eecd_state.old_eecd, E1000State),
1582e482dc3eSJuan Quintela         VMSTATE_UINT8(tx.ipcss, E1000State),
1583e482dc3eSJuan Quintela         VMSTATE_UINT8(tx.ipcso, E1000State),
1584e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.ipcse, E1000State),
1585e482dc3eSJuan Quintela         VMSTATE_UINT8(tx.tucss, E1000State),
1586e482dc3eSJuan Quintela         VMSTATE_UINT8(tx.tucso, E1000State),
1587e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.tucse, E1000State),
1588e482dc3eSJuan Quintela         VMSTATE_UINT32(tx.paylen, E1000State),
1589e482dc3eSJuan Quintela         VMSTATE_UINT8(tx.hdr_len, E1000State),
1590e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.mss, E1000State),
1591e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.size, E1000State),
1592e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.tso_frames, E1000State),
1593e482dc3eSJuan Quintela         VMSTATE_UINT8(tx.sum_needed, E1000State),
1594e482dc3eSJuan Quintela         VMSTATE_INT8(tx.ip, E1000State),
1595e482dc3eSJuan Quintela         VMSTATE_INT8(tx.tcp, E1000State),
1596e482dc3eSJuan Quintela         VMSTATE_BUFFER(tx.header, E1000State),
1597e482dc3eSJuan Quintela         VMSTATE_BUFFER(tx.data, E1000State),
1598e482dc3eSJuan Quintela         VMSTATE_UINT16_ARRAY(eeprom_data, E1000State, 64),
1599e482dc3eSJuan Quintela         VMSTATE_UINT16_ARRAY(phy_reg, E1000State, 0x20),
1600e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[CTRL], E1000State),
1601e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[EECD], E1000State),
1602e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[EERD], E1000State),
1603e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[GPRC], E1000State),
1604e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[GPTC], E1000State),
1605e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[ICR], E1000State),
1606e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[ICS], E1000State),
1607e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[IMC], E1000State),
1608e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[IMS], E1000State),
1609e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[LEDCTL], E1000State),
1610e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MANC], E1000State),
1611e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MDIC], E1000State),
1612e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MPC], E1000State),
1613e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[PBA], E1000State),
1614e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RCTL], E1000State),
1615e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDBAH], E1000State),
1616e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDBAL], E1000State),
1617e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDH], E1000State),
1618e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDLEN], E1000State),
1619e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDT], E1000State),
1620e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[STATUS], E1000State),
1621e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[SWSM], E1000State),
1622e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TCTL], E1000State),
1623e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDBAH], E1000State),
1624e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDBAL], E1000State),
1625e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDH], E1000State),
1626e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDLEN], E1000State),
1627e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDT], E1000State),
1628e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TORH], E1000State),
1629e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TORL], E1000State),
1630e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TOTH], E1000State),
1631e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TOTL], E1000State),
1632e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TPR], E1000State),
1633e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TPT], E1000State),
1634e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TXDCTL], E1000State),
1635e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[WUFC], E1000State),
1636e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[VET], E1000State),
1637e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, RA, 32),
1638e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, MTA, 128),
1639e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, VFTA, 128),
1640e482dc3eSJuan Quintela         VMSTATE_END_OF_LIST()
1641e9845f09SVincenzo Maffione     },
16425cd8cadaSJuan Quintela     .subsections = (const VMStateDescription*[]) {
16435cd8cadaSJuan Quintela         &vmstate_e1000_mit_state,
16449e117734SLeonid Bloch         &vmstate_e1000_full_mac_state,
16455cd8cadaSJuan Quintela         NULL
16467c23b892Sbalrog     }
1647e482dc3eSJuan Quintela };
16487c23b892Sbalrog 
16498597f2e1SGabriel L. Somlo /*
16508597f2e1SGabriel L. Somlo  * EEPROM contents documented in Tables 5-2 and 5-3, pp. 98-102.
16518597f2e1SGabriel L. Somlo  * Note: A valid DevId will be inserted during pci_e1000_init().
16528597f2e1SGabriel L. Somlo  */
165388b4e9dbSblueswir1 static const uint16_t e1000_eeprom_template[64] = {
16547c23b892Sbalrog     0x0000, 0x0000, 0x0000, 0x0000,      0xffff, 0x0000,      0x0000, 0x0000,
16558597f2e1SGabriel L. Somlo     0x3000, 0x1000, 0x6403, 0 /*DevId*/, 0x8086, 0 /*DevId*/, 0x8086, 0x3040,
16567c23b892Sbalrog     0x0008, 0x2000, 0x7e14, 0x0048,      0x1000, 0x00d8,      0x0000, 0x2700,
16577c23b892Sbalrog     0x6cc9, 0x3150, 0x0722, 0x040b,      0x0984, 0x0000,      0xc000, 0x0706,
16587c23b892Sbalrog     0x1008, 0x0000, 0x0f04, 0x7fff,      0x4d01, 0xffff,      0xffff, 0xffff,
16597c23b892Sbalrog     0xffff, 0xffff, 0xffff, 0xffff,      0xffff, 0xffff,      0xffff, 0xffff,
16607c23b892Sbalrog     0x0100, 0x4000, 0x121c, 0xffff,      0xffff, 0xffff,      0xffff, 0xffff,
16617c23b892Sbalrog     0xffff, 0xffff, 0xffff, 0xffff,      0xffff, 0xffff,      0xffff, 0x0000,
16627c23b892Sbalrog };
16637c23b892Sbalrog 
16647c23b892Sbalrog /* PCI interface */
16657c23b892Sbalrog 
16667c23b892Sbalrog static void
1667ad00a9b9SAvi Kivity e1000_mmio_setup(E1000State *d)
16687c23b892Sbalrog {
1669f65ed4c1Saliguori     int i;
1670f65ed4c1Saliguori     const uint32_t excluded_regs[] = {
1671f65ed4c1Saliguori         E1000_MDIC, E1000_ICR, E1000_ICS, E1000_IMS,
1672f65ed4c1Saliguori         E1000_IMC, E1000_TCTL, E1000_TDT, PNPMMIO_SIZE
1673f65ed4c1Saliguori     };
1674f65ed4c1Saliguori 
1675eedfac6fSPaolo Bonzini     memory_region_init_io(&d->mmio, OBJECT(d), &e1000_mmio_ops, d,
1676eedfac6fSPaolo Bonzini                           "e1000-mmio", PNPMMIO_SIZE);
1677ad00a9b9SAvi Kivity     memory_region_add_coalescing(&d->mmio, 0, excluded_regs[0]);
1678f65ed4c1Saliguori     for (i = 0; excluded_regs[i] != PNPMMIO_SIZE; i++)
1679ad00a9b9SAvi Kivity         memory_region_add_coalescing(&d->mmio, excluded_regs[i] + 4,
1680ad00a9b9SAvi Kivity                                      excluded_regs[i+1] - excluded_regs[i] - 4);
1681eedfac6fSPaolo Bonzini     memory_region_init_io(&d->io, OBJECT(d), &e1000_io_ops, d, "e1000-io", IOPORT_SIZE);
16827c23b892Sbalrog }
16837c23b892Sbalrog 
1684b946a153Saliguori static void
16854b09be85Saliguori pci_e1000_uninit(PCIDevice *dev)
16864b09be85Saliguori {
1687567a3c9eSPeter Crosthwaite     E1000State *d = E1000(dev);
16884b09be85Saliguori 
1689bc72ad67SAlex Bligh     timer_del(d->autoneg_timer);
1690bc72ad67SAlex Bligh     timer_free(d->autoneg_timer);
1691e9845f09SVincenzo Maffione     timer_del(d->mit_timer);
1692e9845f09SVincenzo Maffione     timer_free(d->mit_timer);
1693948ecf21SJason Wang     qemu_del_nic(d->nic);
16944b09be85Saliguori }
16954b09be85Saliguori 
1696a03e2aecSMark McLoughlin static NetClientInfo net_e1000_info = {
16972be64a68SLaszlo Ersek     .type = NET_CLIENT_OPTIONS_KIND_NIC,
1698a03e2aecSMark McLoughlin     .size = sizeof(NICState),
1699a03e2aecSMark McLoughlin     .can_receive = e1000_can_receive,
1700a03e2aecSMark McLoughlin     .receive = e1000_receive,
170197410ddeSVincenzo Maffione     .receive_iov = e1000_receive_iov,
1702a03e2aecSMark McLoughlin     .link_status_changed = e1000_set_link_status,
1703a03e2aecSMark McLoughlin };
1704a03e2aecSMark McLoughlin 
170520302e71SMichael S. Tsirkin static void e1000_write_config(PCIDevice *pci_dev, uint32_t address,
170620302e71SMichael S. Tsirkin                                 uint32_t val, int len)
170720302e71SMichael S. Tsirkin {
170820302e71SMichael S. Tsirkin     E1000State *s = E1000(pci_dev);
170920302e71SMichael S. Tsirkin 
171020302e71SMichael S. Tsirkin     pci_default_write_config(pci_dev, address, val, len);
171120302e71SMichael S. Tsirkin 
171220302e71SMichael S. Tsirkin     if (range_covers_byte(address, len, PCI_COMMAND) &&
171320302e71SMichael S. Tsirkin         (pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
171420302e71SMichael S. Tsirkin         qemu_flush_queued_packets(qemu_get_queue(s->nic));
171520302e71SMichael S. Tsirkin     }
171620302e71SMichael S. Tsirkin }
171720302e71SMichael S. Tsirkin 
171820302e71SMichael S. Tsirkin 
17199af21dbeSMarkus Armbruster static void pci_e1000_realize(PCIDevice *pci_dev, Error **errp)
17207c23b892Sbalrog {
1721567a3c9eSPeter Crosthwaite     DeviceState *dev = DEVICE(pci_dev);
1722567a3c9eSPeter Crosthwaite     E1000State *d = E1000(pci_dev);
17238597f2e1SGabriel L. Somlo     PCIDeviceClass *pdc = PCI_DEVICE_GET_CLASS(pci_dev);
17247c23b892Sbalrog     uint8_t *pci_conf;
17257c23b892Sbalrog     uint16_t checksum = 0;
17267c23b892Sbalrog     int i;
1727fbdaa002SGerd Hoffmann     uint8_t *macaddr;
1728aff427a1SChris Wright 
172920302e71SMichael S. Tsirkin     pci_dev->config_write = e1000_write_config;
173020302e71SMichael S. Tsirkin 
1731b08340d5SAndreas Färber     pci_conf = pci_dev->config;
17327c23b892Sbalrog 
1733a9cbacb0SMichael S. Tsirkin     /* TODO: RST# value should be 0, PCI spec 6.2.4 */
1734a9cbacb0SMichael S. Tsirkin     pci_conf[PCI_CACHE_LINE_SIZE] = 0x10;
17357c23b892Sbalrog 
1736817e0b6fSMichael S. Tsirkin     pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
17377c23b892Sbalrog 
1738ad00a9b9SAvi Kivity     e1000_mmio_setup(d);
17397c23b892Sbalrog 
1740b08340d5SAndreas Färber     pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
17417c23b892Sbalrog 
1742b08340d5SAndreas Färber     pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->io);
17437c23b892Sbalrog 
17447c23b892Sbalrog     memmove(d->eeprom_data, e1000_eeprom_template,
17457c23b892Sbalrog         sizeof e1000_eeprom_template);
1746fbdaa002SGerd Hoffmann     qemu_macaddr_default_if_unset(&d->conf.macaddr);
1747fbdaa002SGerd Hoffmann     macaddr = d->conf.macaddr.a;
17487c23b892Sbalrog     for (i = 0; i < 3; i++)
17499d07d757SPaul Brook         d->eeprom_data[i] = (macaddr[2*i+1]<<8) | macaddr[2*i];
17508597f2e1SGabriel L. Somlo     d->eeprom_data[11] = d->eeprom_data[13] = pdc->device_id;
17517c23b892Sbalrog     for (i = 0; i < EEPROM_CHECKSUM_REG; i++)
17527c23b892Sbalrog         checksum += d->eeprom_data[i];
17537c23b892Sbalrog     checksum = (uint16_t) EEPROM_SUM - checksum;
17547c23b892Sbalrog     d->eeprom_data[EEPROM_CHECKSUM_REG] = checksum;
17557c23b892Sbalrog 
1756a03e2aecSMark McLoughlin     d->nic = qemu_new_nic(&net_e1000_info, &d->conf,
1757567a3c9eSPeter Crosthwaite                           object_get_typename(OBJECT(d)), dev->id, d);
17587c23b892Sbalrog 
1759b356f76dSJason Wang     qemu_format_nic_info_str(qemu_get_queue(d->nic), macaddr);
17601ca4d09aSGleb Natapov 
1761bc72ad67SAlex Bligh     d->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, e1000_autoneg_timer, d);
1762e9845f09SVincenzo Maffione     d->mit_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000_mit_timer, d);
17637c23b892Sbalrog }
17649d07d757SPaul Brook 
1765fbdaa002SGerd Hoffmann static void qdev_e1000_reset(DeviceState *dev)
1766fbdaa002SGerd Hoffmann {
1767567a3c9eSPeter Crosthwaite     E1000State *d = E1000(dev);
1768fbdaa002SGerd Hoffmann     e1000_reset(d);
1769fbdaa002SGerd Hoffmann }
1770fbdaa002SGerd Hoffmann 
177140021f08SAnthony Liguori static Property e1000_properties[] = {
1772fbdaa002SGerd Hoffmann     DEFINE_NIC_PROPERTIES(E1000State, conf),
17732af234e6SMichael S. Tsirkin     DEFINE_PROP_BIT("autonegotiation", E1000State,
17742af234e6SMichael S. Tsirkin                     compat_flags, E1000_FLAG_AUTONEG_BIT, true),
1775e9845f09SVincenzo Maffione     DEFINE_PROP_BIT("mitigation", E1000State,
1776e9845f09SVincenzo Maffione                     compat_flags, E1000_FLAG_MIT_BIT, true),
1777fbdaa002SGerd Hoffmann     DEFINE_PROP_END_OF_LIST(),
177840021f08SAnthony Liguori };
177940021f08SAnthony Liguori 
17808597f2e1SGabriel L. Somlo typedef struct E1000Info {
17818597f2e1SGabriel L. Somlo     const char *name;
17828597f2e1SGabriel L. Somlo     uint16_t   device_id;
17838597f2e1SGabriel L. Somlo     uint8_t    revision;
17848597f2e1SGabriel L. Somlo     uint16_t   phy_id2;
17858597f2e1SGabriel L. Somlo } E1000Info;
17868597f2e1SGabriel L. Somlo 
178740021f08SAnthony Liguori static void e1000_class_init(ObjectClass *klass, void *data)
178840021f08SAnthony Liguori {
178939bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
179040021f08SAnthony Liguori     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
17918597f2e1SGabriel L. Somlo     E1000BaseClass *e = E1000_DEVICE_CLASS(klass);
17928597f2e1SGabriel L. Somlo     const E1000Info *info = data;
179340021f08SAnthony Liguori 
17949af21dbeSMarkus Armbruster     k->realize = pci_e1000_realize;
179540021f08SAnthony Liguori     k->exit = pci_e1000_uninit;
1796c45e5b5bSGerd Hoffmann     k->romfile = "efi-e1000.rom";
179740021f08SAnthony Liguori     k->vendor_id = PCI_VENDOR_ID_INTEL;
17988597f2e1SGabriel L. Somlo     k->device_id = info->device_id;
17998597f2e1SGabriel L. Somlo     k->revision = info->revision;
18008597f2e1SGabriel L. Somlo     e->phy_id2 = info->phy_id2;
180140021f08SAnthony Liguori     k->class_id = PCI_CLASS_NETWORK_ETHERNET;
1802125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
180339bffca2SAnthony Liguori     dc->desc = "Intel Gigabit Ethernet";
180439bffca2SAnthony Liguori     dc->reset = qdev_e1000_reset;
180539bffca2SAnthony Liguori     dc->vmsd = &vmstate_e1000;
180639bffca2SAnthony Liguori     dc->props = e1000_properties;
1807fbdaa002SGerd Hoffmann }
180840021f08SAnthony Liguori 
18095df3bf62SGonglei static void e1000_instance_init(Object *obj)
18105df3bf62SGonglei {
18115df3bf62SGonglei     E1000State *n = E1000(obj);
18125df3bf62SGonglei     device_add_bootindex_property(obj, &n->conf.bootindex,
18135df3bf62SGonglei                                   "bootindex", "/ethernet-phy@0",
18145df3bf62SGonglei                                   DEVICE(n), NULL);
18155df3bf62SGonglei }
18165df3bf62SGonglei 
18178597f2e1SGabriel L. Somlo static const TypeInfo e1000_base_info = {
18188597f2e1SGabriel L. Somlo     .name          = TYPE_E1000_BASE,
181939bffca2SAnthony Liguori     .parent        = TYPE_PCI_DEVICE,
182039bffca2SAnthony Liguori     .instance_size = sizeof(E1000State),
18215df3bf62SGonglei     .instance_init = e1000_instance_init,
18228597f2e1SGabriel L. Somlo     .class_size    = sizeof(E1000BaseClass),
18238597f2e1SGabriel L. Somlo     .abstract      = true,
18248597f2e1SGabriel L. Somlo };
18258597f2e1SGabriel L. Somlo 
18268597f2e1SGabriel L. Somlo static const E1000Info e1000_devices[] = {
18278597f2e1SGabriel L. Somlo     {
182883044020SJason Wang         .name      = "e1000",
18298597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82540EM,
18308597f2e1SGabriel L. Somlo         .revision  = 0x03,
18318597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_8254xx_DEFAULT,
18328597f2e1SGabriel L. Somlo     },
18338597f2e1SGabriel L. Somlo     {
18348597f2e1SGabriel L. Somlo         .name      = "e1000-82544gc",
18358597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82544GC_COPPER,
18368597f2e1SGabriel L. Somlo         .revision  = 0x03,
18378597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_82544x,
18388597f2e1SGabriel L. Somlo     },
18398597f2e1SGabriel L. Somlo     {
18408597f2e1SGabriel L. Somlo         .name      = "e1000-82545em",
18418597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82545EM_COPPER,
18428597f2e1SGabriel L. Somlo         .revision  = 0x03,
18438597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_8254xx_DEFAULT,
18448597f2e1SGabriel L. Somlo     },
18458597f2e1SGabriel L. Somlo };
18468597f2e1SGabriel L. Somlo 
184783f7d43aSAndreas Färber static void e1000_register_types(void)
18489d07d757SPaul Brook {
18498597f2e1SGabriel L. Somlo     int i;
18508597f2e1SGabriel L. Somlo 
18518597f2e1SGabriel L. Somlo     type_register_static(&e1000_base_info);
18528597f2e1SGabriel L. Somlo     for (i = 0; i < ARRAY_SIZE(e1000_devices); i++) {
18538597f2e1SGabriel L. Somlo         const E1000Info *info = &e1000_devices[i];
18548597f2e1SGabriel L. Somlo         TypeInfo type_info = {};
18558597f2e1SGabriel L. Somlo 
18568597f2e1SGabriel L. Somlo         type_info.name = info->name;
18578597f2e1SGabriel L. Somlo         type_info.parent = TYPE_E1000_BASE;
18588597f2e1SGabriel L. Somlo         type_info.class_data = (void *)info;
18598597f2e1SGabriel L. Somlo         type_info.class_init = e1000_class_init;
18605df3bf62SGonglei         type_info.instance_init = e1000_instance_init;
18618597f2e1SGabriel L. Somlo 
18628597f2e1SGabriel L. Somlo         type_register(&type_info);
18638597f2e1SGabriel L. Somlo     }
18649d07d757SPaul Brook }
18659d07d757SPaul Brook 
186683f7d43aSAndreas Färber type_init(e1000_register_types)
1867