xref: /qemu/hw/net/e1000.c (revision 7d08c73e7bdc39b10e5f2f5acdce700f17ffe962)
17c23b892Sbalrog /*
27c23b892Sbalrog  * QEMU e1000 emulation
37c23b892Sbalrog  *
42758aa52SMichael S. Tsirkin  * Software developer's manual:
52758aa52SMichael S. Tsirkin  * http://download.intel.com/design/network/manuals/8254x_GBe_SDM.pdf
62758aa52SMichael S. Tsirkin  *
77c23b892Sbalrog  * Nir Peleg, Tutis Systems Ltd. for Qumranet Inc.
87c23b892Sbalrog  * Copyright (c) 2008 Qumranet
97c23b892Sbalrog  * Based on work done by:
107c23b892Sbalrog  * Copyright (c) 2007 Dan Aloni
117c23b892Sbalrog  * Copyright (c) 2004 Antony T Curtis
127c23b892Sbalrog  *
137c23b892Sbalrog  * This library is free software; you can redistribute it and/or
147c23b892Sbalrog  * modify it under the terms of the GNU Lesser General Public
157c23b892Sbalrog  * License as published by the Free Software Foundation; either
167c23b892Sbalrog  * version 2 of the License, or (at your option) any later version.
177c23b892Sbalrog  *
187c23b892Sbalrog  * This library is distributed in the hope that it will be useful,
197c23b892Sbalrog  * but WITHOUT ANY WARRANTY; without even the implied warranty of
207c23b892Sbalrog  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
217c23b892Sbalrog  * Lesser General Public License for more details.
227c23b892Sbalrog  *
237c23b892Sbalrog  * You should have received a copy of the GNU Lesser General Public
248167ee88SBlue Swirl  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
257c23b892Sbalrog  */
267c23b892Sbalrog 
277c23b892Sbalrog 
28e8d40465SPeter Maydell #include "qemu/osdep.h"
2983c9f4caSPaolo Bonzini #include "hw/hw.h"
3083c9f4caSPaolo Bonzini #include "hw/pci/pci.h"
311422e32dSPaolo Bonzini #include "net/net.h"
327200ac3cSMark McLoughlin #include "net/checksum.h"
3383c9f4caSPaolo Bonzini #include "hw/loader.h"
349c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
359c17d615SPaolo Bonzini #include "sysemu/dma.h"
3697410ddeSVincenzo Maffione #include "qemu/iov.h"
3720302e71SMichael S. Tsirkin #include "qemu/range.h"
387c23b892Sbalrog 
39093454e2SDmitry Fleytman #include "e1000x_common.h"
407c23b892Sbalrog 
413b274301SLeonid Bloch static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
423b274301SLeonid Bloch 
43b4053c64SJason Wang /* #define E1000_DEBUG */
447c23b892Sbalrog 
4527124888SJes Sorensen #ifdef E1000_DEBUG
467c23b892Sbalrog enum {
477c23b892Sbalrog     DEBUG_GENERAL,      DEBUG_IO,       DEBUG_MMIO,     DEBUG_INTERRUPT,
487c23b892Sbalrog     DEBUG_RX,           DEBUG_TX,       DEBUG_MDIC,     DEBUG_EEPROM,
497c23b892Sbalrog     DEBUG_UNKNOWN,      DEBUG_TXSUM,    DEBUG_TXERR,    DEBUG_RXERR,
50f9c1cdf4SJason Wang     DEBUG_RXFILTER,     DEBUG_PHY,      DEBUG_NOTYET,
517c23b892Sbalrog };
527c23b892Sbalrog #define DBGBIT(x)    (1<<DEBUG_##x)
537c23b892Sbalrog static int debugflags = DBGBIT(TXERR) | DBGBIT(GENERAL);
547c23b892Sbalrog 
556c7f4b47SBlue Swirl #define DBGOUT(what, fmt, ...) do { \
567c23b892Sbalrog     if (debugflags & DBGBIT(what)) \
576c7f4b47SBlue Swirl         fprintf(stderr, "e1000: " fmt, ## __VA_ARGS__); \
587c23b892Sbalrog     } while (0)
597c23b892Sbalrog #else
606c7f4b47SBlue Swirl #define DBGOUT(what, fmt, ...) do {} while (0)
617c23b892Sbalrog #endif
627c23b892Sbalrog 
637c23b892Sbalrog #define IOPORT_SIZE       0x40
64e94bbefeSaurel32 #define PNPMMIO_SIZE      0x20000
6578aeb23eSStefan Hajnoczi #define MIN_BUF_SIZE      60 /* Min. octets in an ethernet frame sans FCS */
667c23b892Sbalrog 
6797410ddeSVincenzo Maffione #define MAXIMUM_ETHERNET_HDR_LEN (14+4)
6897410ddeSVincenzo Maffione 
697c23b892Sbalrog /*
707c23b892Sbalrog  * HW models:
718597f2e1SGabriel L. Somlo  *  E1000_DEV_ID_82540EM works with Windows, Linux, and OS X <= 10.8
727c23b892Sbalrog  *  E1000_DEV_ID_82544GC_COPPER appears to work; not well tested
738597f2e1SGabriel L. Somlo  *  E1000_DEV_ID_82545EM_COPPER works with Linux and OS X >= 10.6
747c23b892Sbalrog  *  Others never tested
757c23b892Sbalrog  */
767c23b892Sbalrog 
777c23b892Sbalrog typedef struct E1000State_st {
78b08340d5SAndreas Färber     /*< private >*/
79b08340d5SAndreas Färber     PCIDevice parent_obj;
80b08340d5SAndreas Färber     /*< public >*/
81b08340d5SAndreas Färber 
82a03e2aecSMark McLoughlin     NICState *nic;
83fbdaa002SGerd Hoffmann     NICConf conf;
84ad00a9b9SAvi Kivity     MemoryRegion mmio;
85ad00a9b9SAvi Kivity     MemoryRegion io;
867c23b892Sbalrog 
877c23b892Sbalrog     uint32_t mac_reg[0x8000];
887c23b892Sbalrog     uint16_t phy_reg[0x20];
897c23b892Sbalrog     uint16_t eeprom_data[64];
907c23b892Sbalrog 
917c23b892Sbalrog     uint32_t rxbuf_size;
927c23b892Sbalrog     uint32_t rxbuf_min_shift;
937c23b892Sbalrog     struct e1000_tx {
947c23b892Sbalrog         unsigned char header[256];
958f2e8d1fSaliguori         unsigned char vlan_header[4];
96b10fec9bSStefan Weil         /* Fields vlan and data must not be reordered or separated. */
978f2e8d1fSaliguori         unsigned char vlan[4];
987c23b892Sbalrog         unsigned char data[0x10000];
997c23b892Sbalrog         uint16_t size;
1008f2e8d1fSaliguori         unsigned char vlan_needed;
101*7d08c73eSEd Swierk via Qemu-devel         unsigned char sum_needed;
102*7d08c73eSEd Swierk via Qemu-devel         bool cptse;
103093454e2SDmitry Fleytman         e1000x_txd_props props;
1047c23b892Sbalrog         uint16_t tso_frames;
1057c23b892Sbalrog     } tx;
1067c23b892Sbalrog 
1077c23b892Sbalrog     struct {
10820f3e863SLeonid Bloch         uint32_t val_in;    /* shifted in from guest driver */
1097c23b892Sbalrog         uint16_t bitnum_in;
1107c23b892Sbalrog         uint16_t bitnum_out;
1117c23b892Sbalrog         uint16_t reading;
1127c23b892Sbalrog         uint32_t old_eecd;
1137c23b892Sbalrog     } eecd_state;
114b9d03e35SJason Wang 
115b9d03e35SJason Wang     QEMUTimer *autoneg_timer;
1162af234e6SMichael S. Tsirkin 
117e9845f09SVincenzo Maffione     QEMUTimer *mit_timer;      /* Mitigation timer. */
118e9845f09SVincenzo Maffione     bool mit_timer_on;         /* Mitigation timer is running. */
119e9845f09SVincenzo Maffione     bool mit_irq_level;        /* Tracks interrupt pin level. */
120e9845f09SVincenzo Maffione     uint32_t mit_ide;          /* Tracks E1000_TXD_CMD_IDE bit. */
121e9845f09SVincenzo Maffione 
1222af234e6SMichael S. Tsirkin /* Compatibility flags for migration to/from qemu 1.3.0 and older */
1232af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG_BIT 0
124e9845f09SVincenzo Maffione #define E1000_FLAG_MIT_BIT 1
1259e117734SLeonid Bloch #define E1000_FLAG_MAC_BIT 2
1262af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG (1 << E1000_FLAG_AUTONEG_BIT)
127e9845f09SVincenzo Maffione #define E1000_FLAG_MIT (1 << E1000_FLAG_MIT_BIT)
1289e117734SLeonid Bloch #define E1000_FLAG_MAC (1 << E1000_FLAG_MAC_BIT)
1292af234e6SMichael S. Tsirkin     uint32_t compat_flags;
1307c23b892Sbalrog } E1000State;
1317c23b892Sbalrog 
132bc0f0674SLeonid Bloch #define chkflag(x)     (s->compat_flags & E1000_FLAG_##x)
133bc0f0674SLeonid Bloch 
1348597f2e1SGabriel L. Somlo typedef struct E1000BaseClass {
1358597f2e1SGabriel L. Somlo     PCIDeviceClass parent_class;
1368597f2e1SGabriel L. Somlo     uint16_t phy_id2;
1378597f2e1SGabriel L. Somlo } E1000BaseClass;
1388597f2e1SGabriel L. Somlo 
1398597f2e1SGabriel L. Somlo #define TYPE_E1000_BASE "e1000-base"
140567a3c9eSPeter Crosthwaite 
141567a3c9eSPeter Crosthwaite #define E1000(obj) \
1428597f2e1SGabriel L. Somlo     OBJECT_CHECK(E1000State, (obj), TYPE_E1000_BASE)
1438597f2e1SGabriel L. Somlo 
1448597f2e1SGabriel L. Somlo #define E1000_DEVICE_CLASS(klass) \
1458597f2e1SGabriel L. Somlo      OBJECT_CLASS_CHECK(E1000BaseClass, (klass), TYPE_E1000_BASE)
1468597f2e1SGabriel L. Somlo #define E1000_DEVICE_GET_CLASS(obj) \
1478597f2e1SGabriel L. Somlo     OBJECT_GET_CLASS(E1000BaseClass, (obj), TYPE_E1000_BASE)
148567a3c9eSPeter Crosthwaite 
14971aadd3cSJason Wang static void
15071aadd3cSJason Wang e1000_link_up(E1000State *s)
15171aadd3cSJason Wang {
152093454e2SDmitry Fleytman     e1000x_update_regs_on_link_up(s->mac_reg, s->phy_reg);
153093454e2SDmitry Fleytman 
154093454e2SDmitry Fleytman     /* E1000_STATUS_LU is tested by e1000_can_receive() */
155093454e2SDmitry Fleytman     qemu_flush_queued_packets(qemu_get_queue(s->nic));
156093454e2SDmitry Fleytman }
157093454e2SDmitry Fleytman 
158093454e2SDmitry Fleytman static void
159093454e2SDmitry Fleytman e1000_autoneg_done(E1000State *s)
160093454e2SDmitry Fleytman {
161093454e2SDmitry Fleytman     e1000x_update_regs_on_autoneg_done(s->mac_reg, s->phy_reg);
1625df6a185SStefan Hajnoczi 
1635df6a185SStefan Hajnoczi     /* E1000_STATUS_LU is tested by e1000_can_receive() */
1645df6a185SStefan Hajnoczi     qemu_flush_queued_packets(qemu_get_queue(s->nic));
16571aadd3cSJason Wang }
16671aadd3cSJason Wang 
1671195fed9SGabriel L. Somlo static bool
1681195fed9SGabriel L. Somlo have_autoneg(E1000State *s)
1691195fed9SGabriel L. Somlo {
170bc0f0674SLeonid Bloch     return chkflag(AUTONEG) && (s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN);
1711195fed9SGabriel L. Somlo }
1721195fed9SGabriel L. Somlo 
173b9d03e35SJason Wang static void
174b9d03e35SJason Wang set_phy_ctrl(E1000State *s, int index, uint16_t val)
175b9d03e35SJason Wang {
1761195fed9SGabriel L. Somlo     /* bits 0-5 reserved; MII_CR_[RESTART_AUTO_NEG,RESET] are self clearing */
1771195fed9SGabriel L. Somlo     s->phy_reg[PHY_CTRL] = val & ~(0x3f |
1781195fed9SGabriel L. Somlo                                    MII_CR_RESET |
1791195fed9SGabriel L. Somlo                                    MII_CR_RESTART_AUTO_NEG);
1801195fed9SGabriel L. Somlo 
1812af234e6SMichael S. Tsirkin     /*
1822af234e6SMichael S. Tsirkin      * QEMU 1.3 does not support link auto-negotiation emulation, so if we
1832af234e6SMichael S. Tsirkin      * migrate during auto negotiation, after migration the link will be
1842af234e6SMichael S. Tsirkin      * down.
1852af234e6SMichael S. Tsirkin      */
1861195fed9SGabriel L. Somlo     if (have_autoneg(s) && (val & MII_CR_RESTART_AUTO_NEG)) {
187093454e2SDmitry Fleytman         e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer);
188b9d03e35SJason Wang     }
189b9d03e35SJason Wang }
190b9d03e35SJason Wang 
191b9d03e35SJason Wang static void (*phyreg_writeops[])(E1000State *, int, uint16_t) = {
192b9d03e35SJason Wang     [PHY_CTRL] = set_phy_ctrl,
193b9d03e35SJason Wang };
194b9d03e35SJason Wang 
195b9d03e35SJason Wang enum { NPHYWRITEOPS = ARRAY_SIZE(phyreg_writeops) };
196b9d03e35SJason Wang 
1977c23b892Sbalrog enum { PHY_R = 1, PHY_W = 2, PHY_RW = PHY_R | PHY_W };
19888b4e9dbSblueswir1 static const char phy_regcap[0x20] = {
1997c23b892Sbalrog     [PHY_STATUS]      = PHY_R,     [M88E1000_EXT_PHY_SPEC_CTRL] = PHY_RW,
2007c23b892Sbalrog     [PHY_ID1]         = PHY_R,     [M88E1000_PHY_SPEC_CTRL]     = PHY_RW,
2017c23b892Sbalrog     [PHY_CTRL]        = PHY_RW,    [PHY_1000T_CTRL]             = PHY_RW,
2027c23b892Sbalrog     [PHY_LP_ABILITY]  = PHY_R,     [PHY_1000T_STATUS]           = PHY_R,
2037c23b892Sbalrog     [PHY_AUTONEG_ADV] = PHY_RW,    [M88E1000_RX_ERR_CNTR]       = PHY_R,
2046883b591SGabriel L. Somlo     [PHY_ID2]         = PHY_R,     [M88E1000_PHY_SPEC_STATUS]   = PHY_R,
2056883b591SGabriel L. Somlo     [PHY_AUTONEG_EXP] = PHY_R,
2067c23b892Sbalrog };
2077c23b892Sbalrog 
2088597f2e1SGabriel L. Somlo /* PHY_ID2 documented in 8254x_GBe_SDM.pdf, pp. 250 */
209814cd3acSMichael S. Tsirkin static const uint16_t phy_reg_init[] = {
2109616c290SGabriel L. Somlo     [PHY_CTRL]   = MII_CR_SPEED_SELECT_MSB |
2119616c290SGabriel L. Somlo                    MII_CR_FULL_DUPLEX |
2129616c290SGabriel L. Somlo                    MII_CR_AUTO_NEG_EN,
2139616c290SGabriel L. Somlo 
2149616c290SGabriel L. Somlo     [PHY_STATUS] = MII_SR_EXTENDED_CAPS |
2159616c290SGabriel L. Somlo                    MII_SR_LINK_STATUS |   /* link initially up */
2169616c290SGabriel L. Somlo                    MII_SR_AUTONEG_CAPS |
2179616c290SGabriel L. Somlo                    /* MII_SR_AUTONEG_COMPLETE: initially NOT completed */
2189616c290SGabriel L. Somlo                    MII_SR_PREAMBLE_SUPPRESS |
2199616c290SGabriel L. Somlo                    MII_SR_EXTENDED_STATUS |
2209616c290SGabriel L. Somlo                    MII_SR_10T_HD_CAPS |
2219616c290SGabriel L. Somlo                    MII_SR_10T_FD_CAPS |
2229616c290SGabriel L. Somlo                    MII_SR_100X_HD_CAPS |
2239616c290SGabriel L. Somlo                    MII_SR_100X_FD_CAPS,
2249616c290SGabriel L. Somlo 
2259616c290SGabriel L. Somlo     [PHY_ID1] = 0x141,
2269616c290SGabriel L. Somlo     /* [PHY_ID2] configured per DevId, from e1000_reset() */
2279616c290SGabriel L. Somlo     [PHY_AUTONEG_ADV] = 0xde1,
2289616c290SGabriel L. Somlo     [PHY_LP_ABILITY] = 0x1e0,
2299616c290SGabriel L. Somlo     [PHY_1000T_CTRL] = 0x0e00,
2309616c290SGabriel L. Somlo     [PHY_1000T_STATUS] = 0x3c00,
2319616c290SGabriel L. Somlo     [M88E1000_PHY_SPEC_CTRL] = 0x360,
232814cd3acSMichael S. Tsirkin     [M88E1000_PHY_SPEC_STATUS] = 0xac00,
2339616c290SGabriel L. Somlo     [M88E1000_EXT_PHY_SPEC_CTRL] = 0x0d60,
234814cd3acSMichael S. Tsirkin };
235814cd3acSMichael S. Tsirkin 
236814cd3acSMichael S. Tsirkin static const uint32_t mac_reg_init[] = {
237814cd3acSMichael S. Tsirkin     [PBA]     = 0x00100030,
238814cd3acSMichael S. Tsirkin     [LEDCTL]  = 0x602,
239814cd3acSMichael S. Tsirkin     [CTRL]    = E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN0 |
240814cd3acSMichael S. Tsirkin                 E1000_CTRL_SPD_1000 | E1000_CTRL_SLU,
241814cd3acSMichael S. Tsirkin     [STATUS]  = 0x80000000 | E1000_STATUS_GIO_MASTER_ENABLE |
242814cd3acSMichael S. Tsirkin                 E1000_STATUS_ASDV | E1000_STATUS_MTXCKOK |
243814cd3acSMichael S. Tsirkin                 E1000_STATUS_SPEED_1000 | E1000_STATUS_FD |
244814cd3acSMichael S. Tsirkin                 E1000_STATUS_LU,
245814cd3acSMichael S. Tsirkin     [MANC]    = E1000_MANC_EN_MNG2HOST | E1000_MANC_RCV_TCO_EN |
246814cd3acSMichael S. Tsirkin                 E1000_MANC_ARP_EN | E1000_MANC_0298_EN |
247814cd3acSMichael S. Tsirkin                 E1000_MANC_RMCP_EN,
248814cd3acSMichael S. Tsirkin };
249814cd3acSMichael S. Tsirkin 
250e9845f09SVincenzo Maffione /* Helper function, *curr == 0 means the value is not set */
251e9845f09SVincenzo Maffione static inline void
252e9845f09SVincenzo Maffione mit_update_delay(uint32_t *curr, uint32_t value)
253e9845f09SVincenzo Maffione {
254e9845f09SVincenzo Maffione     if (value && (*curr == 0 || value < *curr)) {
255e9845f09SVincenzo Maffione         *curr = value;
256e9845f09SVincenzo Maffione     }
257e9845f09SVincenzo Maffione }
258e9845f09SVincenzo Maffione 
2597c23b892Sbalrog static void
2607c23b892Sbalrog set_interrupt_cause(E1000State *s, int index, uint32_t val)
2617c23b892Sbalrog {
262b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
263e9845f09SVincenzo Maffione     uint32_t pending_ints;
264e9845f09SVincenzo Maffione     uint32_t mit_delay;
265b08340d5SAndreas Färber 
2667c23b892Sbalrog     s->mac_reg[ICR] = val;
267a52a8841SMichael S. Tsirkin 
268a52a8841SMichael S. Tsirkin     /*
269a52a8841SMichael S. Tsirkin      * Make sure ICR and ICS registers have the same value.
270a52a8841SMichael S. Tsirkin      * The spec says that the ICS register is write-only.  However in practice,
271a52a8841SMichael S. Tsirkin      * on real hardware ICS is readable, and for reads it has the same value as
272a52a8841SMichael S. Tsirkin      * ICR (except that ICS does not have the clear on read behaviour of ICR).
273a52a8841SMichael S. Tsirkin      *
274a52a8841SMichael S. Tsirkin      * The VxWorks PRO/1000 driver uses this behaviour.
275a52a8841SMichael S. Tsirkin      */
276b1332393SBill Paul     s->mac_reg[ICS] = val;
277a52a8841SMichael S. Tsirkin 
278e9845f09SVincenzo Maffione     pending_ints = (s->mac_reg[IMS] & s->mac_reg[ICR]);
279e9845f09SVincenzo Maffione     if (!s->mit_irq_level && pending_ints) {
280e9845f09SVincenzo Maffione         /*
281e9845f09SVincenzo Maffione          * Here we detect a potential raising edge. We postpone raising the
282e9845f09SVincenzo Maffione          * interrupt line if we are inside the mitigation delay window
283e9845f09SVincenzo Maffione          * (s->mit_timer_on == 1).
284e9845f09SVincenzo Maffione          * We provide a partial implementation of interrupt mitigation,
285e9845f09SVincenzo Maffione          * emulating only RADV, TADV and ITR (lower 16 bits, 1024ns units for
286e9845f09SVincenzo Maffione          * RADV and TADV, 256ns units for ITR). RDTR is only used to enable
287e9845f09SVincenzo Maffione          * RADV; relative timers based on TIDV and RDTR are not implemented.
288e9845f09SVincenzo Maffione          */
289e9845f09SVincenzo Maffione         if (s->mit_timer_on) {
290e9845f09SVincenzo Maffione             return;
291e9845f09SVincenzo Maffione         }
292bc0f0674SLeonid Bloch         if (chkflag(MIT)) {
293e9845f09SVincenzo Maffione             /* Compute the next mitigation delay according to pending
294e9845f09SVincenzo Maffione              * interrupts and the current values of RADV (provided
295e9845f09SVincenzo Maffione              * RDTR!=0), TADV and ITR.
296e9845f09SVincenzo Maffione              * Then rearm the timer.
297e9845f09SVincenzo Maffione              */
298e9845f09SVincenzo Maffione             mit_delay = 0;
299e9845f09SVincenzo Maffione             if (s->mit_ide &&
300e9845f09SVincenzo Maffione                     (pending_ints & (E1000_ICR_TXQE | E1000_ICR_TXDW))) {
301e9845f09SVincenzo Maffione                 mit_update_delay(&mit_delay, s->mac_reg[TADV] * 4);
302e9845f09SVincenzo Maffione             }
303e9845f09SVincenzo Maffione             if (s->mac_reg[RDTR] && (pending_ints & E1000_ICS_RXT0)) {
304e9845f09SVincenzo Maffione                 mit_update_delay(&mit_delay, s->mac_reg[RADV] * 4);
305e9845f09SVincenzo Maffione             }
306e9845f09SVincenzo Maffione             mit_update_delay(&mit_delay, s->mac_reg[ITR]);
307e9845f09SVincenzo Maffione 
30874004e8cSSameeh Jubran             /*
30974004e8cSSameeh Jubran              * According to e1000 SPEC, the Ethernet controller guarantees
31074004e8cSSameeh Jubran              * a maximum observable interrupt rate of 7813 interrupts/sec.
31174004e8cSSameeh Jubran              * Thus if mit_delay < 500 then the delay should be set to the
31274004e8cSSameeh Jubran              * minimum delay possible which is 500.
31374004e8cSSameeh Jubran              */
31474004e8cSSameeh Jubran             mit_delay = (mit_delay < 500) ? 500 : mit_delay;
31574004e8cSSameeh Jubran 
316e9845f09SVincenzo Maffione             s->mit_timer_on = 1;
317e9845f09SVincenzo Maffione             timer_mod(s->mit_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
318e9845f09SVincenzo Maffione                       mit_delay * 256);
319e9845f09SVincenzo Maffione             s->mit_ide = 0;
320e9845f09SVincenzo Maffione         }
321e9845f09SVincenzo Maffione     }
322e9845f09SVincenzo Maffione 
323e9845f09SVincenzo Maffione     s->mit_irq_level = (pending_ints != 0);
3249e64f8a3SMarcel Apfelbaum     pci_set_irq(d, s->mit_irq_level);
325e9845f09SVincenzo Maffione }
326e9845f09SVincenzo Maffione 
327e9845f09SVincenzo Maffione static void
328e9845f09SVincenzo Maffione e1000_mit_timer(void *opaque)
329e9845f09SVincenzo Maffione {
330e9845f09SVincenzo Maffione     E1000State *s = opaque;
331e9845f09SVincenzo Maffione 
332e9845f09SVincenzo Maffione     s->mit_timer_on = 0;
333e9845f09SVincenzo Maffione     /* Call set_interrupt_cause to update the irq level (if necessary). */
334e9845f09SVincenzo Maffione     set_interrupt_cause(s, 0, s->mac_reg[ICR]);
3357c23b892Sbalrog }
3367c23b892Sbalrog 
3377c23b892Sbalrog static void
3387c23b892Sbalrog set_ics(E1000State *s, int index, uint32_t val)
3397c23b892Sbalrog {
3407c23b892Sbalrog     DBGOUT(INTERRUPT, "set_ics %x, ICR %x, IMR %x\n", val, s->mac_reg[ICR],
3417c23b892Sbalrog         s->mac_reg[IMS]);
3427c23b892Sbalrog     set_interrupt_cause(s, 0, val | s->mac_reg[ICR]);
3437c23b892Sbalrog }
3447c23b892Sbalrog 
345d52aec95SGabriel L. Somlo static void
346d52aec95SGabriel L. Somlo e1000_autoneg_timer(void *opaque)
347d52aec95SGabriel L. Somlo {
348d52aec95SGabriel L. Somlo     E1000State *s = opaque;
349d52aec95SGabriel L. Somlo     if (!qemu_get_queue(s->nic)->link_down) {
350093454e2SDmitry Fleytman         e1000_autoneg_done(s);
351d52aec95SGabriel L. Somlo         set_ics(s, 0, E1000_ICS_LSC); /* signal link status change to guest */
352d52aec95SGabriel L. Somlo     }
353d52aec95SGabriel L. Somlo }
354d52aec95SGabriel L. Somlo 
355814cd3acSMichael S. Tsirkin static void e1000_reset(void *opaque)
356814cd3acSMichael S. Tsirkin {
357814cd3acSMichael S. Tsirkin     E1000State *d = opaque;
3588597f2e1SGabriel L. Somlo     E1000BaseClass *edc = E1000_DEVICE_GET_CLASS(d);
359372254c6SGabriel L. Somlo     uint8_t *macaddr = d->conf.macaddr.a;
360814cd3acSMichael S. Tsirkin 
361bc72ad67SAlex Bligh     timer_del(d->autoneg_timer);
362e9845f09SVincenzo Maffione     timer_del(d->mit_timer);
363e9845f09SVincenzo Maffione     d->mit_timer_on = 0;
364e9845f09SVincenzo Maffione     d->mit_irq_level = 0;
365e9845f09SVincenzo Maffione     d->mit_ide = 0;
366814cd3acSMichael S. Tsirkin     memset(d->phy_reg, 0, sizeof d->phy_reg);
367814cd3acSMichael S. Tsirkin     memmove(d->phy_reg, phy_reg_init, sizeof phy_reg_init);
3688597f2e1SGabriel L. Somlo     d->phy_reg[PHY_ID2] = edc->phy_id2;
369814cd3acSMichael S. Tsirkin     memset(d->mac_reg, 0, sizeof d->mac_reg);
370814cd3acSMichael S. Tsirkin     memmove(d->mac_reg, mac_reg_init, sizeof mac_reg_init);
371814cd3acSMichael S. Tsirkin     d->rxbuf_min_shift = 1;
372814cd3acSMichael S. Tsirkin     memset(&d->tx, 0, sizeof d->tx);
373814cd3acSMichael S. Tsirkin 
374b356f76dSJason Wang     if (qemu_get_queue(d->nic)->link_down) {
375093454e2SDmitry Fleytman         e1000x_update_regs_on_link_down(d->mac_reg, d->phy_reg);
376814cd3acSMichael S. Tsirkin     }
377372254c6SGabriel L. Somlo 
378093454e2SDmitry Fleytman     e1000x_reset_mac_addr(d->nic, d->mac_reg, macaddr);
379814cd3acSMichael S. Tsirkin }
380814cd3acSMichael S. Tsirkin 
3817c23b892Sbalrog static void
382cab3c825SKevin Wolf set_ctrl(E1000State *s, int index, uint32_t val)
383cab3c825SKevin Wolf {
384cab3c825SKevin Wolf     /* RST is self clearing */
385cab3c825SKevin Wolf     s->mac_reg[CTRL] = val & ~E1000_CTRL_RST;
386cab3c825SKevin Wolf }
387cab3c825SKevin Wolf 
388cab3c825SKevin Wolf static void
3897c23b892Sbalrog set_rx_control(E1000State *s, int index, uint32_t val)
3907c23b892Sbalrog {
3917c23b892Sbalrog     s->mac_reg[RCTL] = val;
392093454e2SDmitry Fleytman     s->rxbuf_size = e1000x_rxbufsize(val);
3937c23b892Sbalrog     s->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1;
3947c23b892Sbalrog     DBGOUT(RX, "RCTL: %d, mac_reg[RCTL] = 0x%x\n", s->mac_reg[RDT],
3957c23b892Sbalrog            s->mac_reg[RCTL]);
396b356f76dSJason Wang     qemu_flush_queued_packets(qemu_get_queue(s->nic));
3977c23b892Sbalrog }
3987c23b892Sbalrog 
3997c23b892Sbalrog static void
4007c23b892Sbalrog set_mdic(E1000State *s, int index, uint32_t val)
4017c23b892Sbalrog {
4027c23b892Sbalrog     uint32_t data = val & E1000_MDIC_DATA_MASK;
4037c23b892Sbalrog     uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT);
4047c23b892Sbalrog 
4057c23b892Sbalrog     if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) // phy #
4067c23b892Sbalrog         val = s->mac_reg[MDIC] | E1000_MDIC_ERROR;
4077c23b892Sbalrog     else if (val & E1000_MDIC_OP_READ) {
4087c23b892Sbalrog         DBGOUT(MDIC, "MDIC read reg 0x%x\n", addr);
4097c23b892Sbalrog         if (!(phy_regcap[addr] & PHY_R)) {
4107c23b892Sbalrog             DBGOUT(MDIC, "MDIC read reg %x unhandled\n", addr);
4117c23b892Sbalrog             val |= E1000_MDIC_ERROR;
4127c23b892Sbalrog         } else
4137c23b892Sbalrog             val = (val ^ data) | s->phy_reg[addr];
4147c23b892Sbalrog     } else if (val & E1000_MDIC_OP_WRITE) {
4157c23b892Sbalrog         DBGOUT(MDIC, "MDIC write reg 0x%x, value 0x%x\n", addr, data);
4167c23b892Sbalrog         if (!(phy_regcap[addr] & PHY_W)) {
4177c23b892Sbalrog             DBGOUT(MDIC, "MDIC write reg %x unhandled\n", addr);
4187c23b892Sbalrog             val |= E1000_MDIC_ERROR;
419b9d03e35SJason Wang         } else {
420b9d03e35SJason Wang             if (addr < NPHYWRITEOPS && phyreg_writeops[addr]) {
421b9d03e35SJason Wang                 phyreg_writeops[addr](s, index, data);
4221195fed9SGabriel L. Somlo             } else {
4237c23b892Sbalrog                 s->phy_reg[addr] = data;
4247c23b892Sbalrog             }
425b9d03e35SJason Wang         }
4261195fed9SGabriel L. Somlo     }
4277c23b892Sbalrog     s->mac_reg[MDIC] = val | E1000_MDIC_READY;
42817fbbb0bSJason Wang 
42917fbbb0bSJason Wang     if (val & E1000_MDIC_INT_EN) {
4307c23b892Sbalrog         set_ics(s, 0, E1000_ICR_MDAC);
4317c23b892Sbalrog     }
43217fbbb0bSJason Wang }
4337c23b892Sbalrog 
4347c23b892Sbalrog static uint32_t
4357c23b892Sbalrog get_eecd(E1000State *s, int index)
4367c23b892Sbalrog {
4377c23b892Sbalrog     uint32_t ret = E1000_EECD_PRES|E1000_EECD_GNT | s->eecd_state.old_eecd;
4387c23b892Sbalrog 
4397c23b892Sbalrog     DBGOUT(EEPROM, "reading eeprom bit %d (reading %d)\n",
4407c23b892Sbalrog            s->eecd_state.bitnum_out, s->eecd_state.reading);
4417c23b892Sbalrog     if (!s->eecd_state.reading ||
4427c23b892Sbalrog         ((s->eeprom_data[(s->eecd_state.bitnum_out >> 4) & 0x3f] >>
4437c23b892Sbalrog           ((s->eecd_state.bitnum_out & 0xf) ^ 0xf))) & 1)
4447c23b892Sbalrog         ret |= E1000_EECD_DO;
4457c23b892Sbalrog     return ret;
4467c23b892Sbalrog }
4477c23b892Sbalrog 
4487c23b892Sbalrog static void
4497c23b892Sbalrog set_eecd(E1000State *s, int index, uint32_t val)
4507c23b892Sbalrog {
4517c23b892Sbalrog     uint32_t oldval = s->eecd_state.old_eecd;
4527c23b892Sbalrog 
4537c23b892Sbalrog     s->eecd_state.old_eecd = val & (E1000_EECD_SK | E1000_EECD_CS |
4547c23b892Sbalrog             E1000_EECD_DI|E1000_EECD_FWE_MASK|E1000_EECD_REQ);
45520f3e863SLeonid Bloch     if (!(E1000_EECD_CS & val)) {            /* CS inactive; nothing to do */
4569651ac55SIzumi Tsutsui         return;
45720f3e863SLeonid Bloch     }
45820f3e863SLeonid Bloch     if (E1000_EECD_CS & (val ^ oldval)) {    /* CS rise edge; reset state */
4599651ac55SIzumi Tsutsui         s->eecd_state.val_in = 0;
4609651ac55SIzumi Tsutsui         s->eecd_state.bitnum_in = 0;
4619651ac55SIzumi Tsutsui         s->eecd_state.bitnum_out = 0;
4629651ac55SIzumi Tsutsui         s->eecd_state.reading = 0;
4639651ac55SIzumi Tsutsui     }
46420f3e863SLeonid Bloch     if (!(E1000_EECD_SK & (val ^ oldval))) {    /* no clock edge */
4657c23b892Sbalrog         return;
46620f3e863SLeonid Bloch     }
46720f3e863SLeonid Bloch     if (!(E1000_EECD_SK & val)) {               /* falling edge */
4687c23b892Sbalrog         s->eecd_state.bitnum_out++;
4697c23b892Sbalrog         return;
4707c23b892Sbalrog     }
4717c23b892Sbalrog     s->eecd_state.val_in <<= 1;
4727c23b892Sbalrog     if (val & E1000_EECD_DI)
4737c23b892Sbalrog         s->eecd_state.val_in |= 1;
4747c23b892Sbalrog     if (++s->eecd_state.bitnum_in == 9 && !s->eecd_state.reading) {
4757c23b892Sbalrog         s->eecd_state.bitnum_out = ((s->eecd_state.val_in & 0x3f)<<4)-1;
4767c23b892Sbalrog         s->eecd_state.reading = (((s->eecd_state.val_in >> 6) & 7) ==
4777c23b892Sbalrog             EEPROM_READ_OPCODE_MICROWIRE);
4787c23b892Sbalrog     }
4797c23b892Sbalrog     DBGOUT(EEPROM, "eeprom bitnum in %d out %d, reading %d\n",
4807c23b892Sbalrog            s->eecd_state.bitnum_in, s->eecd_state.bitnum_out,
4817c23b892Sbalrog            s->eecd_state.reading);
4827c23b892Sbalrog }
4837c23b892Sbalrog 
4847c23b892Sbalrog static uint32_t
4857c23b892Sbalrog flash_eerd_read(E1000State *s, int x)
4867c23b892Sbalrog {
4877c23b892Sbalrog     unsigned int index, r = s->mac_reg[EERD] & ~E1000_EEPROM_RW_REG_START;
4887c23b892Sbalrog 
489b1332393SBill Paul     if ((s->mac_reg[EERD] & E1000_EEPROM_RW_REG_START) == 0)
490b1332393SBill Paul         return (s->mac_reg[EERD]);
491b1332393SBill Paul 
4927c23b892Sbalrog     if ((index = r >> E1000_EEPROM_RW_ADDR_SHIFT) > EEPROM_CHECKSUM_REG)
493b1332393SBill Paul         return (E1000_EEPROM_RW_REG_DONE | r);
494b1332393SBill Paul 
495b1332393SBill Paul     return ((s->eeprom_data[index] << E1000_EEPROM_RW_REG_DATA) |
496b1332393SBill Paul            E1000_EEPROM_RW_REG_DONE | r);
4977c23b892Sbalrog }
4987c23b892Sbalrog 
4997c23b892Sbalrog static void
5007c23b892Sbalrog putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t css, uint32_t cse)
5017c23b892Sbalrog {
502c6a6a5e3Saliguori     uint32_t sum;
503c6a6a5e3Saliguori 
5047c23b892Sbalrog     if (cse && cse < n)
5057c23b892Sbalrog         n = cse + 1;
506c6a6a5e3Saliguori     if (sloc < n-1) {
507c6a6a5e3Saliguori         sum = net_checksum_add(n-css, data+css);
5080dacea92SEd Swierk         stw_be_p(data + sloc, net_checksum_finish_nozero(sum));
509c6a6a5e3Saliguori     }
5107c23b892Sbalrog }
5117c23b892Sbalrog 
5121f67f92cSLeonid Bloch static inline void
5133b274301SLeonid Bloch inc_tx_bcast_or_mcast_count(E1000State *s, const unsigned char *arr)
5143b274301SLeonid Bloch {
5153b274301SLeonid Bloch     if (!memcmp(arr, bcast, sizeof bcast)) {
516093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, BPTC);
5173b274301SLeonid Bloch     } else if (arr[0] & 1) {
518093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, MPTC);
5193b274301SLeonid Bloch     }
5203b274301SLeonid Bloch }
5213b274301SLeonid Bloch 
52245e93764SLeonid Bloch static void
52393e37d76SJason Wang e1000_send_packet(E1000State *s, const uint8_t *buf, int size)
52493e37d76SJason Wang {
5253b274301SLeonid Bloch     static const int PTCregs[6] = { PTC64, PTC127, PTC255, PTC511,
5263b274301SLeonid Bloch                                     PTC1023, PTC1522 };
5273b274301SLeonid Bloch 
528b356f76dSJason Wang     NetClientState *nc = qemu_get_queue(s->nic);
52993e37d76SJason Wang     if (s->phy_reg[PHY_CTRL] & MII_CR_LOOPBACK) {
530b356f76dSJason Wang         nc->info->receive(nc, buf, size);
53193e37d76SJason Wang     } else {
532b356f76dSJason Wang         qemu_send_packet(nc, buf, size);
53393e37d76SJason Wang     }
5343b274301SLeonid Bloch     inc_tx_bcast_or_mcast_count(s, buf);
535093454e2SDmitry Fleytman     e1000x_increase_size_stats(s->mac_reg, PTCregs, size);
53693e37d76SJason Wang }
53793e37d76SJason Wang 
53893e37d76SJason Wang static void
5397c23b892Sbalrog xmit_seg(E1000State *s)
5407c23b892Sbalrog {
54114e60aaeSPeter Maydell     uint16_t len;
54245e93764SLeonid Bloch     unsigned int frames = s->tx.tso_frames, css, sofar;
5437c23b892Sbalrog     struct e1000_tx *tp = &s->tx;
5447c23b892Sbalrog 
545*7d08c73eSEd Swierk via Qemu-devel     if (tp->props.tse && tp->cptse) {
546093454e2SDmitry Fleytman         css = tp->props.ipcss;
5477c23b892Sbalrog         DBGOUT(TXSUM, "frames %d size %d ipcss %d\n",
5487c23b892Sbalrog                frames, tp->size, css);
549093454e2SDmitry Fleytman         if (tp->props.ip) {    /* IPv4 */
550d8ee2591SPeter Maydell             stw_be_p(tp->data+css+2, tp->size - css);
551d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4,
55214e60aaeSPeter Maydell                      lduw_be_p(tp->data + css + 4) + frames);
55320f3e863SLeonid Bloch         } else {         /* IPv6 */
554d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4, tp->size - css);
55520f3e863SLeonid Bloch         }
556093454e2SDmitry Fleytman         css = tp->props.tucss;
5577c23b892Sbalrog         len = tp->size - css;
558093454e2SDmitry Fleytman         DBGOUT(TXSUM, "tcp %d tucss %d len %d\n", tp->props.tcp, css, len);
559093454e2SDmitry Fleytman         if (tp->props.tcp) {
560093454e2SDmitry Fleytman             sofar = frames * tp->props.mss;
5616bd194abSPeter Maydell             stl_be_p(tp->data+css+4, ldl_be_p(tp->data+css+4)+sofar); /* seq */
562093454e2SDmitry Fleytman             if (tp->props.paylen - sofar > tp->props.mss) {
56320f3e863SLeonid Bloch                 tp->data[css + 13] &= ~9;    /* PSH, FIN */
5643b274301SLeonid Bloch             } else if (frames) {
565093454e2SDmitry Fleytman                 e1000x_inc_reg_if_not_full(s->mac_reg, TSCTC);
5663b274301SLeonid Bloch             }
56720f3e863SLeonid Bloch         } else    /* UDP */
568d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4, len);
569*7d08c73eSEd Swierk via Qemu-devel         if (tp->sum_needed & E1000_TXD_POPTS_TXSM) {
570e685b4ebSAlex Williamson             unsigned int phsum;
5717c23b892Sbalrog             // add pseudo-header length before checksum calculation
57214e60aaeSPeter Maydell             void *sp = tp->data + tp->props.tucso;
57314e60aaeSPeter Maydell 
57414e60aaeSPeter Maydell             phsum = lduw_be_p(sp) + len;
575e685b4ebSAlex Williamson             phsum = (phsum >> 16) + (phsum & 0xffff);
576d8ee2591SPeter Maydell             stw_be_p(sp, phsum);
5777c23b892Sbalrog         }
5787c23b892Sbalrog         tp->tso_frames++;
5797c23b892Sbalrog     }
5807c23b892Sbalrog 
581*7d08c73eSEd Swierk via Qemu-devel     if (tp->sum_needed & E1000_TXD_POPTS_TXSM) {
582093454e2SDmitry Fleytman         putsum(tp->data, tp->size, tp->props.tucso,
583093454e2SDmitry Fleytman                tp->props.tucss, tp->props.tucse);
584093454e2SDmitry Fleytman     }
585*7d08c73eSEd Swierk via Qemu-devel     if (tp->sum_needed & E1000_TXD_POPTS_IXSM) {
586093454e2SDmitry Fleytman         putsum(tp->data, tp->size, tp->props.ipcso,
587093454e2SDmitry Fleytman                tp->props.ipcss, tp->props.ipcse);
588093454e2SDmitry Fleytman     }
5898f2e8d1fSaliguori     if (tp->vlan_needed) {
590b10fec9bSStefan Weil         memmove(tp->vlan, tp->data, 4);
591b10fec9bSStefan Weil         memmove(tp->data, tp->data + 4, 8);
5928f2e8d1fSaliguori         memcpy(tp->data + 8, tp->vlan_header, 4);
59393e37d76SJason Wang         e1000_send_packet(s, tp->vlan, tp->size + 4);
59420f3e863SLeonid Bloch     } else {
59593e37d76SJason Wang         e1000_send_packet(s, tp->data, tp->size);
59620f3e863SLeonid Bloch     }
59720f3e863SLeonid Bloch 
598093454e2SDmitry Fleytman     e1000x_inc_reg_if_not_full(s->mac_reg, TPT);
599093454e2SDmitry Fleytman     e1000x_grow_8reg_if_not_full(s->mac_reg, TOTL, s->tx.size);
6001f67f92cSLeonid Bloch     s->mac_reg[GPTC] = s->mac_reg[TPT];
6013b274301SLeonid Bloch     s->mac_reg[GOTCL] = s->mac_reg[TOTL];
6023b274301SLeonid Bloch     s->mac_reg[GOTCH] = s->mac_reg[TOTH];
6037c23b892Sbalrog }
6047c23b892Sbalrog 
6057c23b892Sbalrog static void
6067c23b892Sbalrog process_tx_desc(E1000State *s, struct e1000_tx_desc *dp)
6077c23b892Sbalrog {
608b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
6097c23b892Sbalrog     uint32_t txd_lower = le32_to_cpu(dp->lower.data);
6107c23b892Sbalrog     uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D);
611093454e2SDmitry Fleytman     unsigned int split_size = txd_lower & 0xffff, bytes, sz;
612a0ae17a6SAndrew Jones     unsigned int msh = 0xfffff;
6137c23b892Sbalrog     uint64_t addr;
6147c23b892Sbalrog     struct e1000_context_desc *xp = (struct e1000_context_desc *)dp;
6157c23b892Sbalrog     struct e1000_tx *tp = &s->tx;
6167c23b892Sbalrog 
617e9845f09SVincenzo Maffione     s->mit_ide |= (txd_lower & E1000_TXD_CMD_IDE);
61820f3e863SLeonid Bloch     if (dtype == E1000_TXD_CMD_DEXT) {    /* context descriptor */
619093454e2SDmitry Fleytman         e1000x_read_tx_ctx_descr(xp, &tp->props);
6207c23b892Sbalrog         tp->tso_frames = 0;
621093454e2SDmitry Fleytman         if (tp->props.tucso == 0) {    /* this is probably wrong */
6227c23b892Sbalrog             DBGOUT(TXSUM, "TCP/UDP: cso 0!\n");
623093454e2SDmitry Fleytman             tp->props.tucso = tp->props.tucss + (tp->props.tcp ? 16 : 6);
6247c23b892Sbalrog         }
6257c23b892Sbalrog         return;
6261b0009dbSbalrog     } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) {
6271b0009dbSbalrog         // data descriptor
628735e77ecSStefan Hajnoczi         if (tp->size == 0) {
629*7d08c73eSEd Swierk via Qemu-devel             tp->sum_needed = le32_to_cpu(dp->upper.data) >> 8;
630735e77ecSStefan Hajnoczi         }
631*7d08c73eSEd Swierk via Qemu-devel         tp->cptse = (txd_lower & E1000_TXD_CMD_TSE) ? 1 : 0;
63243ad7e3eSJes Sorensen     } else {
6331b0009dbSbalrog         // legacy descriptor
634*7d08c73eSEd Swierk via Qemu-devel         tp->cptse = 0;
63543ad7e3eSJes Sorensen     }
6367c23b892Sbalrog 
637093454e2SDmitry Fleytman     if (e1000x_vlan_enabled(s->mac_reg) &&
638093454e2SDmitry Fleytman         e1000x_is_vlan_txd(txd_lower) &&
639*7d08c73eSEd Swierk via Qemu-devel         (tp->cptse || txd_lower & E1000_TXD_CMD_EOP)) {
6408f2e8d1fSaliguori         tp->vlan_needed = 1;
641d8ee2591SPeter Maydell         stw_be_p(tp->vlan_header,
6424e60a250SShannon Zhao                       le16_to_cpu(s->mac_reg[VET]));
643d8ee2591SPeter Maydell         stw_be_p(tp->vlan_header + 2,
6448f2e8d1fSaliguori                       le16_to_cpu(dp->upper.fields.special));
6458f2e8d1fSaliguori     }
6468f2e8d1fSaliguori 
6477c23b892Sbalrog     addr = le64_to_cpu(dp->buffer_addr);
648*7d08c73eSEd Swierk via Qemu-devel     if (tp->props.tse && tp->cptse) {
649093454e2SDmitry Fleytman         msh = tp->props.hdr_len + tp->props.mss;
6507c23b892Sbalrog         do {
6517c23b892Sbalrog             bytes = split_size;
6527c23b892Sbalrog             if (tp->size + bytes > msh)
6537c23b892Sbalrog                 bytes = msh - tp->size;
65465f82df0SAnthony Liguori 
65565f82df0SAnthony Liguori             bytes = MIN(sizeof(tp->data) - tp->size, bytes);
656b08340d5SAndreas Färber             pci_dma_read(d, addr, tp->data + tp->size, bytes);
657a0ae17a6SAndrew Jones             sz = tp->size + bytes;
658093454e2SDmitry Fleytman             if (sz >= tp->props.hdr_len && tp->size < tp->props.hdr_len) {
659093454e2SDmitry Fleytman                 memmove(tp->header, tp->data, tp->props.hdr_len);
660a0ae17a6SAndrew Jones             }
6617c23b892Sbalrog             tp->size = sz;
6627c23b892Sbalrog             addr += bytes;
6637c23b892Sbalrog             if (sz == msh) {
6647c23b892Sbalrog                 xmit_seg(s);
665093454e2SDmitry Fleytman                 memmove(tp->data, tp->header, tp->props.hdr_len);
666093454e2SDmitry Fleytman                 tp->size = tp->props.hdr_len;
6677c23b892Sbalrog             }
668b947ac2bSP J P             split_size -= bytes;
669b947ac2bSP J P         } while (bytes && split_size);
670*7d08c73eSEd Swierk via Qemu-devel     } else if (!tp->props.tse && tp->cptse) {
6711b0009dbSbalrog         // context descriptor TSE is not set, while data descriptor TSE is set
672362f5fb5SStefan Weil         DBGOUT(TXERR, "TCP segmentation error\n");
6731b0009dbSbalrog     } else {
67465f82df0SAnthony Liguori         split_size = MIN(sizeof(tp->data) - tp->size, split_size);
675b08340d5SAndreas Färber         pci_dma_read(d, addr, tp->data + tp->size, split_size);
6761b0009dbSbalrog         tp->size += split_size;
6771b0009dbSbalrog     }
6787c23b892Sbalrog 
6797c23b892Sbalrog     if (!(txd_lower & E1000_TXD_CMD_EOP))
6807c23b892Sbalrog         return;
681*7d08c73eSEd Swierk via Qemu-devel     if (!(tp->props.tse && tp->cptse && tp->size < tp->props.hdr_len)) {
6827c23b892Sbalrog         xmit_seg(s);
683a0ae17a6SAndrew Jones     }
6847c23b892Sbalrog     tp->tso_frames = 0;
685*7d08c73eSEd Swierk via Qemu-devel     tp->sum_needed = 0;
6868f2e8d1fSaliguori     tp->vlan_needed = 0;
6877c23b892Sbalrog     tp->size = 0;
688*7d08c73eSEd Swierk via Qemu-devel     tp->cptse = 0;
6897c23b892Sbalrog }
6907c23b892Sbalrog 
6917c23b892Sbalrog static uint32_t
69262ecbd35SEduard - Gabriel Munteanu txdesc_writeback(E1000State *s, dma_addr_t base, struct e1000_tx_desc *dp)
6937c23b892Sbalrog {
694b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
6957c23b892Sbalrog     uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data);
6967c23b892Sbalrog 
6977c23b892Sbalrog     if (!(txd_lower & (E1000_TXD_CMD_RS|E1000_TXD_CMD_RPS)))
6987c23b892Sbalrog         return 0;
6997c23b892Sbalrog     txd_upper = (le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD) &
7007c23b892Sbalrog                 ~(E1000_TXD_STAT_EC | E1000_TXD_STAT_LC | E1000_TXD_STAT_TU);
7017c23b892Sbalrog     dp->upper.data = cpu_to_le32(txd_upper);
702b08340d5SAndreas Färber     pci_dma_write(d, base + ((char *)&dp->upper - (char *)dp),
70300c3a05bSDavid Gibson                   &dp->upper, sizeof(dp->upper));
7047c23b892Sbalrog     return E1000_ICR_TXDW;
7057c23b892Sbalrog }
7067c23b892Sbalrog 
707d17161f6SKevin Wolf static uint64_t tx_desc_base(E1000State *s)
708d17161f6SKevin Wolf {
709d17161f6SKevin Wolf     uint64_t bah = s->mac_reg[TDBAH];
710d17161f6SKevin Wolf     uint64_t bal = s->mac_reg[TDBAL] & ~0xf;
711d17161f6SKevin Wolf 
712d17161f6SKevin Wolf     return (bah << 32) + bal;
713d17161f6SKevin Wolf }
714d17161f6SKevin Wolf 
7157c23b892Sbalrog static void
7167c23b892Sbalrog start_xmit(E1000State *s)
7177c23b892Sbalrog {
718b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
71962ecbd35SEduard - Gabriel Munteanu     dma_addr_t base;
7207c23b892Sbalrog     struct e1000_tx_desc desc;
7217c23b892Sbalrog     uint32_t tdh_start = s->mac_reg[TDH], cause = E1000_ICS_TXQE;
7227c23b892Sbalrog 
7237c23b892Sbalrog     if (!(s->mac_reg[TCTL] & E1000_TCTL_EN)) {
7247c23b892Sbalrog         DBGOUT(TX, "tx disabled\n");
7257c23b892Sbalrog         return;
7267c23b892Sbalrog     }
7277c23b892Sbalrog 
7287c23b892Sbalrog     while (s->mac_reg[TDH] != s->mac_reg[TDT]) {
729d17161f6SKevin Wolf         base = tx_desc_base(s) +
7307c23b892Sbalrog                sizeof(struct e1000_tx_desc) * s->mac_reg[TDH];
731b08340d5SAndreas Färber         pci_dma_read(d, base, &desc, sizeof(desc));
7327c23b892Sbalrog 
7337c23b892Sbalrog         DBGOUT(TX, "index %d: %p : %x %x\n", s->mac_reg[TDH],
7346106075bSths                (void *)(intptr_t)desc.buffer_addr, desc.lower.data,
7357c23b892Sbalrog                desc.upper.data);
7367c23b892Sbalrog 
7377c23b892Sbalrog         process_tx_desc(s, &desc);
73862ecbd35SEduard - Gabriel Munteanu         cause |= txdesc_writeback(s, base, &desc);
7397c23b892Sbalrog 
7407c23b892Sbalrog         if (++s->mac_reg[TDH] * sizeof(desc) >= s->mac_reg[TDLEN])
7417c23b892Sbalrog             s->mac_reg[TDH] = 0;
7427c23b892Sbalrog         /*
7437c23b892Sbalrog          * the following could happen only if guest sw assigns
7447c23b892Sbalrog          * bogus values to TDT/TDLEN.
7457c23b892Sbalrog          * there's nothing too intelligent we could do about this.
7467c23b892Sbalrog          */
747dd793a74SLaszlo Ersek         if (s->mac_reg[TDH] == tdh_start ||
748dd793a74SLaszlo Ersek             tdh_start >= s->mac_reg[TDLEN] / sizeof(desc)) {
7497c23b892Sbalrog             DBGOUT(TXERR, "TDH wraparound @%x, TDT %x, TDLEN %x\n",
7507c23b892Sbalrog                    tdh_start, s->mac_reg[TDT], s->mac_reg[TDLEN]);
7517c23b892Sbalrog             break;
7527c23b892Sbalrog         }
7537c23b892Sbalrog     }
7547c23b892Sbalrog     set_ics(s, 0, cause);
7557c23b892Sbalrog }
7567c23b892Sbalrog 
7577c23b892Sbalrog static int
7587c23b892Sbalrog receive_filter(E1000State *s, const uint8_t *buf, int size)
7597c23b892Sbalrog {
760093454e2SDmitry Fleytman     uint32_t rctl = s->mac_reg[RCTL];
7614aeea330SLeonid Bloch     int isbcast = !memcmp(buf, bcast, sizeof bcast), ismcast = (buf[0] & 1);
7627c23b892Sbalrog 
763093454e2SDmitry Fleytman     if (e1000x_is_vlan_packet(buf, le16_to_cpu(s->mac_reg[VET])) &&
764093454e2SDmitry Fleytman         e1000x_vlan_rx_filter_enabled(s->mac_reg)) {
76514e60aaeSPeter Maydell         uint16_t vid = lduw_be_p(buf + 14);
76614e60aaeSPeter Maydell         uint32_t vfta = ldl_le_p((uint32_t*)(s->mac_reg + VFTA) +
7678f2e8d1fSaliguori                                  ((vid >> 5) & 0x7f));
7688f2e8d1fSaliguori         if ((vfta & (1 << (vid & 0x1f))) == 0)
7698f2e8d1fSaliguori             return 0;
7708f2e8d1fSaliguori     }
7718f2e8d1fSaliguori 
7724aeea330SLeonid Bloch     if (!isbcast && !ismcast && (rctl & E1000_RCTL_UPE)) { /* promiscuous ucast */
7737c23b892Sbalrog         return 1;
7744aeea330SLeonid Bloch     }
7757c23b892Sbalrog 
7764aeea330SLeonid Bloch     if (ismcast && (rctl & E1000_RCTL_MPE)) {          /* promiscuous mcast */
777093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, MPRC);
7787c23b892Sbalrog         return 1;
7794aeea330SLeonid Bloch     }
7807c23b892Sbalrog 
7814aeea330SLeonid Bloch     if (isbcast && (rctl & E1000_RCTL_BAM)) {          /* broadcast enabled */
782093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, BPRC);
7837c23b892Sbalrog         return 1;
7844aeea330SLeonid Bloch     }
7857c23b892Sbalrog 
786093454e2SDmitry Fleytman     return e1000x_rx_group_filter(s->mac_reg, buf);
7877c23b892Sbalrog }
7887c23b892Sbalrog 
78999ed7e30Saliguori static void
7904e68f7a0SStefan Hajnoczi e1000_set_link_status(NetClientState *nc)
79199ed7e30Saliguori {
792cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
79399ed7e30Saliguori     uint32_t old_status = s->mac_reg[STATUS];
79499ed7e30Saliguori 
795d4044c2aSBjørn Mork     if (nc->link_down) {
796093454e2SDmitry Fleytman         e1000x_update_regs_on_link_down(s->mac_reg, s->phy_reg);
797d4044c2aSBjørn Mork     } else {
798d7a41552SGabriel L. Somlo         if (have_autoneg(s) &&
7996a2acedbSGabriel L. Somlo             !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
800093454e2SDmitry Fleytman             e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer);
8016a2acedbSGabriel L. Somlo         } else {
80271aadd3cSJason Wang             e1000_link_up(s);
803d4044c2aSBjørn Mork         }
8046a2acedbSGabriel L. Somlo     }
80599ed7e30Saliguori 
80699ed7e30Saliguori     if (s->mac_reg[STATUS] != old_status)
80799ed7e30Saliguori         set_ics(s, 0, E1000_ICR_LSC);
80899ed7e30Saliguori }
80999ed7e30Saliguori 
810322fd48aSMichael S. Tsirkin static bool e1000_has_rxbufs(E1000State *s, size_t total_size)
811322fd48aSMichael S. Tsirkin {
812322fd48aSMichael S. Tsirkin     int bufs;
813322fd48aSMichael S. Tsirkin     /* Fast-path short packets */
814322fd48aSMichael S. Tsirkin     if (total_size <= s->rxbuf_size) {
815e5b8b0d4SDmitry Fleytman         return s->mac_reg[RDH] != s->mac_reg[RDT];
816322fd48aSMichael S. Tsirkin     }
817322fd48aSMichael S. Tsirkin     if (s->mac_reg[RDH] < s->mac_reg[RDT]) {
818322fd48aSMichael S. Tsirkin         bufs = s->mac_reg[RDT] - s->mac_reg[RDH];
819e5b8b0d4SDmitry Fleytman     } else if (s->mac_reg[RDH] > s->mac_reg[RDT]) {
820322fd48aSMichael S. Tsirkin         bufs = s->mac_reg[RDLEN] /  sizeof(struct e1000_rx_desc) +
821322fd48aSMichael S. Tsirkin             s->mac_reg[RDT] - s->mac_reg[RDH];
822322fd48aSMichael S. Tsirkin     } else {
823322fd48aSMichael S. Tsirkin         return false;
824322fd48aSMichael S. Tsirkin     }
825322fd48aSMichael S. Tsirkin     return total_size <= bufs * s->rxbuf_size;
826322fd48aSMichael S. Tsirkin }
827322fd48aSMichael S. Tsirkin 
8286cdfab28SMichael S. Tsirkin static int
8294e68f7a0SStefan Hajnoczi e1000_can_receive(NetClientState *nc)
8306cdfab28SMichael S. Tsirkin {
831cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
8326cdfab28SMichael S. Tsirkin 
833093454e2SDmitry Fleytman     return e1000x_rx_ready(&s->parent_obj, s->mac_reg) &&
83420302e71SMichael S. Tsirkin         e1000_has_rxbufs(s, 1);
8356cdfab28SMichael S. Tsirkin }
8366cdfab28SMichael S. Tsirkin 
837d17161f6SKevin Wolf static uint64_t rx_desc_base(E1000State *s)
838d17161f6SKevin Wolf {
839d17161f6SKevin Wolf     uint64_t bah = s->mac_reg[RDBAH];
840d17161f6SKevin Wolf     uint64_t bal = s->mac_reg[RDBAL] & ~0xf;
841d17161f6SKevin Wolf 
842d17161f6SKevin Wolf     return (bah << 32) + bal;
843d17161f6SKevin Wolf }
844d17161f6SKevin Wolf 
8454f1c942bSMark McLoughlin static ssize_t
84697410ddeSVincenzo Maffione e1000_receive_iov(NetClientState *nc, const struct iovec *iov, int iovcnt)
8477c23b892Sbalrog {
848cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
849b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
8507c23b892Sbalrog     struct e1000_rx_desc desc;
85162ecbd35SEduard - Gabriel Munteanu     dma_addr_t base;
8527c23b892Sbalrog     unsigned int n, rdt;
8537c23b892Sbalrog     uint32_t rdh_start;
8548f2e8d1fSaliguori     uint16_t vlan_special = 0;
85597410ddeSVincenzo Maffione     uint8_t vlan_status = 0;
85678aeb23eSStefan Hajnoczi     uint8_t min_buf[MIN_BUF_SIZE];
85797410ddeSVincenzo Maffione     struct iovec min_iov;
85897410ddeSVincenzo Maffione     uint8_t *filter_buf = iov->iov_base;
85997410ddeSVincenzo Maffione     size_t size = iov_size(iov, iovcnt);
86097410ddeSVincenzo Maffione     size_t iov_ofs = 0;
861b19487e2SMichael S. Tsirkin     size_t desc_offset;
862b19487e2SMichael S. Tsirkin     size_t desc_size;
863b19487e2SMichael S. Tsirkin     size_t total_size;
8647c23b892Sbalrog 
865093454e2SDmitry Fleytman     if (!e1000x_hw_rx_enabled(s->mac_reg)) {
866ddcb73b7SMichael S. Tsirkin         return -1;
867ddcb73b7SMichael S. Tsirkin     }
8687c23b892Sbalrog 
86978aeb23eSStefan Hajnoczi     /* Pad to minimum Ethernet frame length */
87078aeb23eSStefan Hajnoczi     if (size < sizeof(min_buf)) {
87197410ddeSVincenzo Maffione         iov_to_buf(iov, iovcnt, 0, min_buf, size);
87278aeb23eSStefan Hajnoczi         memset(&min_buf[size], 0, sizeof(min_buf) - size);
873093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, RUC);
87497410ddeSVincenzo Maffione         min_iov.iov_base = filter_buf = min_buf;
87597410ddeSVincenzo Maffione         min_iov.iov_len = size = sizeof(min_buf);
87697410ddeSVincenzo Maffione         iovcnt = 1;
87797410ddeSVincenzo Maffione         iov = &min_iov;
87897410ddeSVincenzo Maffione     } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) {
87997410ddeSVincenzo Maffione         /* This is very unlikely, but may happen. */
88097410ddeSVincenzo Maffione         iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN);
88197410ddeSVincenzo Maffione         filter_buf = min_buf;
88278aeb23eSStefan Hajnoczi     }
88378aeb23eSStefan Hajnoczi 
884b0d9ffcdSMichael Contreras     /* Discard oversized packets if !LPE and !SBP. */
885093454e2SDmitry Fleytman     if (e1000x_is_oversized(s->mac_reg, size)) {
886b0d9ffcdSMichael Contreras         return size;
887b0d9ffcdSMichael Contreras     }
888b0d9ffcdSMichael Contreras 
88997410ddeSVincenzo Maffione     if (!receive_filter(s, filter_buf, size)) {
8904f1c942bSMark McLoughlin         return size;
89197410ddeSVincenzo Maffione     }
8927c23b892Sbalrog 
893093454e2SDmitry Fleytman     if (e1000x_vlan_enabled(s->mac_reg) &&
894093454e2SDmitry Fleytman         e1000x_is_vlan_packet(filter_buf, le16_to_cpu(s->mac_reg[VET]))) {
89514e60aaeSPeter Maydell         vlan_special = cpu_to_le16(lduw_be_p(filter_buf + 14));
89697410ddeSVincenzo Maffione         iov_ofs = 4;
89797410ddeSVincenzo Maffione         if (filter_buf == iov->iov_base) {
89897410ddeSVincenzo Maffione             memmove(filter_buf + 4, filter_buf, 12);
89997410ddeSVincenzo Maffione         } else {
90097410ddeSVincenzo Maffione             iov_from_buf(iov, iovcnt, 4, filter_buf, 12);
90197410ddeSVincenzo Maffione             while (iov->iov_len <= iov_ofs) {
90297410ddeSVincenzo Maffione                 iov_ofs -= iov->iov_len;
90397410ddeSVincenzo Maffione                 iov++;
90497410ddeSVincenzo Maffione             }
90597410ddeSVincenzo Maffione         }
9068f2e8d1fSaliguori         vlan_status = E1000_RXD_STAT_VP;
9078f2e8d1fSaliguori         size -= 4;
9088f2e8d1fSaliguori     }
9098f2e8d1fSaliguori 
9107c23b892Sbalrog     rdh_start = s->mac_reg[RDH];
911b19487e2SMichael S. Tsirkin     desc_offset = 0;
912093454e2SDmitry Fleytman     total_size = size + e1000x_fcs_len(s->mac_reg);
913322fd48aSMichael S. Tsirkin     if (!e1000_has_rxbufs(s, total_size)) {
914322fd48aSMichael S. Tsirkin             set_ics(s, 0, E1000_ICS_RXO);
915322fd48aSMichael S. Tsirkin             return -1;
916322fd48aSMichael S. Tsirkin     }
9177c23b892Sbalrog     do {
918b19487e2SMichael S. Tsirkin         desc_size = total_size - desc_offset;
919b19487e2SMichael S. Tsirkin         if (desc_size > s->rxbuf_size) {
920b19487e2SMichael S. Tsirkin             desc_size = s->rxbuf_size;
921b19487e2SMichael S. Tsirkin         }
922d17161f6SKevin Wolf         base = rx_desc_base(s) + sizeof(desc) * s->mac_reg[RDH];
923b08340d5SAndreas Färber         pci_dma_read(d, base, &desc, sizeof(desc));
9248f2e8d1fSaliguori         desc.special = vlan_special;
9258f2e8d1fSaliguori         desc.status |= (vlan_status | E1000_RXD_STAT_DD);
9267c23b892Sbalrog         if (desc.buffer_addr) {
927b19487e2SMichael S. Tsirkin             if (desc_offset < size) {
92897410ddeSVincenzo Maffione                 size_t iov_copy;
92997410ddeSVincenzo Maffione                 hwaddr ba = le64_to_cpu(desc.buffer_addr);
930b19487e2SMichael S. Tsirkin                 size_t copy_size = size - desc_offset;
931b19487e2SMichael S. Tsirkin                 if (copy_size > s->rxbuf_size) {
932b19487e2SMichael S. Tsirkin                     copy_size = s->rxbuf_size;
933b19487e2SMichael S. Tsirkin                 }
93497410ddeSVincenzo Maffione                 do {
93597410ddeSVincenzo Maffione                     iov_copy = MIN(copy_size, iov->iov_len - iov_ofs);
93697410ddeSVincenzo Maffione                     pci_dma_write(d, ba, iov->iov_base + iov_ofs, iov_copy);
93797410ddeSVincenzo Maffione                     copy_size -= iov_copy;
93897410ddeSVincenzo Maffione                     ba += iov_copy;
93997410ddeSVincenzo Maffione                     iov_ofs += iov_copy;
94097410ddeSVincenzo Maffione                     if (iov_ofs == iov->iov_len) {
94197410ddeSVincenzo Maffione                         iov++;
94297410ddeSVincenzo Maffione                         iov_ofs = 0;
94397410ddeSVincenzo Maffione                     }
94497410ddeSVincenzo Maffione                 } while (copy_size);
945b19487e2SMichael S. Tsirkin             }
946b19487e2SMichael S. Tsirkin             desc_offset += desc_size;
947b19487e2SMichael S. Tsirkin             desc.length = cpu_to_le16(desc_size);
948ee912ccfSMichael S. Tsirkin             if (desc_offset >= total_size) {
9497c23b892Sbalrog                 desc.status |= E1000_RXD_STAT_EOP | E1000_RXD_STAT_IXSM;
950b19487e2SMichael S. Tsirkin             } else {
951ee912ccfSMichael S. Tsirkin                 /* Guest zeroing out status is not a hardware requirement.
952ee912ccfSMichael S. Tsirkin                    Clear EOP in case guest didn't do it. */
953ee912ccfSMichael S. Tsirkin                 desc.status &= ~E1000_RXD_STAT_EOP;
954b19487e2SMichael S. Tsirkin             }
95543ad7e3eSJes Sorensen         } else { // as per intel docs; skip descriptors with null buf addr
9567c23b892Sbalrog             DBGOUT(RX, "Null RX descriptor!!\n");
95743ad7e3eSJes Sorensen         }
958b08340d5SAndreas Färber         pci_dma_write(d, base, &desc, sizeof(desc));
9597c23b892Sbalrog 
9607c23b892Sbalrog         if (++s->mac_reg[RDH] * sizeof(desc) >= s->mac_reg[RDLEN])
9617c23b892Sbalrog             s->mac_reg[RDH] = 0;
9627c23b892Sbalrog         /* see comment in start_xmit; same here */
963dd793a74SLaszlo Ersek         if (s->mac_reg[RDH] == rdh_start ||
964dd793a74SLaszlo Ersek             rdh_start >= s->mac_reg[RDLEN] / sizeof(desc)) {
9657c23b892Sbalrog             DBGOUT(RXERR, "RDH wraparound @%x, RDT %x, RDLEN %x\n",
9667c23b892Sbalrog                    rdh_start, s->mac_reg[RDT], s->mac_reg[RDLEN]);
9677c23b892Sbalrog             set_ics(s, 0, E1000_ICS_RXO);
9684f1c942bSMark McLoughlin             return -1;
9697c23b892Sbalrog         }
970b19487e2SMichael S. Tsirkin     } while (desc_offset < total_size);
9717c23b892Sbalrog 
972093454e2SDmitry Fleytman     e1000x_update_rx_total_stats(s->mac_reg, size, total_size);
9737c23b892Sbalrog 
9747c23b892Sbalrog     n = E1000_ICS_RXT0;
9757c23b892Sbalrog     if ((rdt = s->mac_reg[RDT]) < s->mac_reg[RDH])
9767c23b892Sbalrog         rdt += s->mac_reg[RDLEN] / sizeof(desc);
977bf16cc8fSaliguori     if (((rdt - s->mac_reg[RDH]) * sizeof(desc)) <= s->mac_reg[RDLEN] >>
978bf16cc8fSaliguori         s->rxbuf_min_shift)
9797c23b892Sbalrog         n |= E1000_ICS_RXDMT0;
9807c23b892Sbalrog 
9817c23b892Sbalrog     set_ics(s, 0, n);
9824f1c942bSMark McLoughlin 
9834f1c942bSMark McLoughlin     return size;
9847c23b892Sbalrog }
9857c23b892Sbalrog 
98697410ddeSVincenzo Maffione static ssize_t
98797410ddeSVincenzo Maffione e1000_receive(NetClientState *nc, const uint8_t *buf, size_t size)
98897410ddeSVincenzo Maffione {
98997410ddeSVincenzo Maffione     const struct iovec iov = {
99097410ddeSVincenzo Maffione         .iov_base = (uint8_t *)buf,
99197410ddeSVincenzo Maffione         .iov_len = size
99297410ddeSVincenzo Maffione     };
99397410ddeSVincenzo Maffione 
99497410ddeSVincenzo Maffione     return e1000_receive_iov(nc, &iov, 1);
99597410ddeSVincenzo Maffione }
99697410ddeSVincenzo Maffione 
9977c23b892Sbalrog static uint32_t
9987c23b892Sbalrog mac_readreg(E1000State *s, int index)
9997c23b892Sbalrog {
10007c23b892Sbalrog     return s->mac_reg[index];
10017c23b892Sbalrog }
10027c23b892Sbalrog 
10037c23b892Sbalrog static uint32_t
100472ea771cSLeonid Bloch mac_low4_read(E1000State *s, int index)
100572ea771cSLeonid Bloch {
100672ea771cSLeonid Bloch     return s->mac_reg[index] & 0xf;
100772ea771cSLeonid Bloch }
100872ea771cSLeonid Bloch 
100972ea771cSLeonid Bloch static uint32_t
101072ea771cSLeonid Bloch mac_low11_read(E1000State *s, int index)
101172ea771cSLeonid Bloch {
101272ea771cSLeonid Bloch     return s->mac_reg[index] & 0x7ff;
101372ea771cSLeonid Bloch }
101472ea771cSLeonid Bloch 
101572ea771cSLeonid Bloch static uint32_t
101672ea771cSLeonid Bloch mac_low13_read(E1000State *s, int index)
101772ea771cSLeonid Bloch {
101872ea771cSLeonid Bloch     return s->mac_reg[index] & 0x1fff;
101972ea771cSLeonid Bloch }
102072ea771cSLeonid Bloch 
102172ea771cSLeonid Bloch static uint32_t
102272ea771cSLeonid Bloch mac_low16_read(E1000State *s, int index)
102372ea771cSLeonid Bloch {
102472ea771cSLeonid Bloch     return s->mac_reg[index] & 0xffff;
102572ea771cSLeonid Bloch }
102672ea771cSLeonid Bloch 
102772ea771cSLeonid Bloch static uint32_t
10287c23b892Sbalrog mac_icr_read(E1000State *s, int index)
10297c23b892Sbalrog {
10307c23b892Sbalrog     uint32_t ret = s->mac_reg[ICR];
10317c23b892Sbalrog 
10327c23b892Sbalrog     DBGOUT(INTERRUPT, "ICR read: %x\n", ret);
10337c23b892Sbalrog     set_interrupt_cause(s, 0, 0);
10347c23b892Sbalrog     return ret;
10357c23b892Sbalrog }
10367c23b892Sbalrog 
10377c23b892Sbalrog static uint32_t
10387c23b892Sbalrog mac_read_clr4(E1000State *s, int index)
10397c23b892Sbalrog {
10407c23b892Sbalrog     uint32_t ret = s->mac_reg[index];
10417c23b892Sbalrog 
10427c23b892Sbalrog     s->mac_reg[index] = 0;
10437c23b892Sbalrog     return ret;
10447c23b892Sbalrog }
10457c23b892Sbalrog 
10467c23b892Sbalrog static uint32_t
10477c23b892Sbalrog mac_read_clr8(E1000State *s, int index)
10487c23b892Sbalrog {
10497c23b892Sbalrog     uint32_t ret = s->mac_reg[index];
10507c23b892Sbalrog 
10517c23b892Sbalrog     s->mac_reg[index] = 0;
10527c23b892Sbalrog     s->mac_reg[index-1] = 0;
10537c23b892Sbalrog     return ret;
10547c23b892Sbalrog }
10557c23b892Sbalrog 
10567c23b892Sbalrog static void
10577c23b892Sbalrog mac_writereg(E1000State *s, int index, uint32_t val)
10587c23b892Sbalrog {
10597c36507cSAmos Kong     uint32_t macaddr[2];
10607c36507cSAmos Kong 
10617c23b892Sbalrog     s->mac_reg[index] = val;
10627c36507cSAmos Kong 
106390d131fbSMichael S. Tsirkin     if (index == RA + 1) {
10647c36507cSAmos Kong         macaddr[0] = cpu_to_le32(s->mac_reg[RA]);
10657c36507cSAmos Kong         macaddr[1] = cpu_to_le32(s->mac_reg[RA + 1]);
10667c36507cSAmos Kong         qemu_format_nic_info_str(qemu_get_queue(s->nic), (uint8_t *)macaddr);
10677c36507cSAmos Kong     }
10687c23b892Sbalrog }
10697c23b892Sbalrog 
10707c23b892Sbalrog static void
10717c23b892Sbalrog set_rdt(E1000State *s, int index, uint32_t val)
10727c23b892Sbalrog {
10737c23b892Sbalrog     s->mac_reg[index] = val & 0xffff;
1074e8b4c680SPaolo Bonzini     if (e1000_has_rxbufs(s, 1)) {
1075b356f76dSJason Wang         qemu_flush_queued_packets(qemu_get_queue(s->nic));
1076e8b4c680SPaolo Bonzini     }
10777c23b892Sbalrog }
10787c23b892Sbalrog 
10797c23b892Sbalrog static void
10807c23b892Sbalrog set_16bit(E1000State *s, int index, uint32_t val)
10817c23b892Sbalrog {
10827c23b892Sbalrog     s->mac_reg[index] = val & 0xffff;
10837c23b892Sbalrog }
10847c23b892Sbalrog 
10857c23b892Sbalrog static void
10867c23b892Sbalrog set_dlen(E1000State *s, int index, uint32_t val)
10877c23b892Sbalrog {
10887c23b892Sbalrog     s->mac_reg[index] = val & 0xfff80;
10897c23b892Sbalrog }
10907c23b892Sbalrog 
10917c23b892Sbalrog static void
10927c23b892Sbalrog set_tctl(E1000State *s, int index, uint32_t val)
10937c23b892Sbalrog {
10947c23b892Sbalrog     s->mac_reg[index] = val;
10957c23b892Sbalrog     s->mac_reg[TDT] &= 0xffff;
10967c23b892Sbalrog     start_xmit(s);
10977c23b892Sbalrog }
10987c23b892Sbalrog 
10997c23b892Sbalrog static void
11007c23b892Sbalrog set_icr(E1000State *s, int index, uint32_t val)
11017c23b892Sbalrog {
11027c23b892Sbalrog     DBGOUT(INTERRUPT, "set_icr %x\n", val);
11037c23b892Sbalrog     set_interrupt_cause(s, 0, s->mac_reg[ICR] & ~val);
11047c23b892Sbalrog }
11057c23b892Sbalrog 
11067c23b892Sbalrog static void
11077c23b892Sbalrog set_imc(E1000State *s, int index, uint32_t val)
11087c23b892Sbalrog {
11097c23b892Sbalrog     s->mac_reg[IMS] &= ~val;
11107c23b892Sbalrog     set_ics(s, 0, 0);
11117c23b892Sbalrog }
11127c23b892Sbalrog 
11137c23b892Sbalrog static void
11147c23b892Sbalrog set_ims(E1000State *s, int index, uint32_t val)
11157c23b892Sbalrog {
11167c23b892Sbalrog     s->mac_reg[IMS] |= val;
11177c23b892Sbalrog     set_ics(s, 0, 0);
11187c23b892Sbalrog }
11197c23b892Sbalrog 
11207c23b892Sbalrog #define getreg(x)    [x] = mac_readreg
11217c23b892Sbalrog static uint32_t (*macreg_readops[])(E1000State *, int) = {
11227c23b892Sbalrog     getreg(PBA),      getreg(RCTL),     getreg(TDH),      getreg(TXDCTL),
11237c23b892Sbalrog     getreg(WUFC),     getreg(TDT),      getreg(CTRL),     getreg(LEDCTL),
11247c23b892Sbalrog     getreg(MANC),     getreg(MDIC),     getreg(SWSM),     getreg(STATUS),
11257c23b892Sbalrog     getreg(TORL),     getreg(TOTL),     getreg(IMS),      getreg(TCTL),
1126b1332393SBill Paul     getreg(RDH),      getreg(RDT),      getreg(VET),      getreg(ICS),
1127a00b2335SKay Ackermann     getreg(TDBAL),    getreg(TDBAH),    getreg(RDBAH),    getreg(RDBAL),
1128e9845f09SVincenzo Maffione     getreg(TDLEN),    getreg(RDLEN),    getreg(RDTR),     getreg(RADV),
112972ea771cSLeonid Bloch     getreg(TADV),     getreg(ITR),      getreg(FCRUC),    getreg(IPAV),
113072ea771cSLeonid Bloch     getreg(WUC),      getreg(WUS),      getreg(SCC),      getreg(ECOL),
113172ea771cSLeonid Bloch     getreg(MCC),      getreg(LATECOL),  getreg(COLC),     getreg(DC),
1132757704f1SKamil Rytarowski     getreg(TNCRS),    getreg(SEQEC),    getreg(CEXTERR),  getreg(RLEC),
113372ea771cSLeonid Bloch     getreg(XONRXC),   getreg(XONTXC),   getreg(XOFFRXC),  getreg(XOFFTXC),
113472ea771cSLeonid Bloch     getreg(RFC),      getreg(RJC),      getreg(RNBC),     getreg(TSCTFC),
11353b274301SLeonid Bloch     getreg(MGTPRC),   getreg(MGTPDC),   getreg(MGTPTC),   getreg(GORCL),
11363b274301SLeonid Bloch     getreg(GOTCL),
11377c23b892Sbalrog 
113820f3e863SLeonid Bloch     [TOTH]    = mac_read_clr8,      [TORH]    = mac_read_clr8,
11393b274301SLeonid Bloch     [GOTCH]   = mac_read_clr8,      [GORCH]   = mac_read_clr8,
11403b274301SLeonid Bloch     [PRC64]   = mac_read_clr4,      [PRC127]  = mac_read_clr4,
11413b274301SLeonid Bloch     [PRC255]  = mac_read_clr4,      [PRC511]  = mac_read_clr4,
11423b274301SLeonid Bloch     [PRC1023] = mac_read_clr4,      [PRC1522] = mac_read_clr4,
11433b274301SLeonid Bloch     [PTC64]   = mac_read_clr4,      [PTC127]  = mac_read_clr4,
11443b274301SLeonid Bloch     [PTC255]  = mac_read_clr4,      [PTC511]  = mac_read_clr4,
11453b274301SLeonid Bloch     [PTC1023] = mac_read_clr4,      [PTC1522] = mac_read_clr4,
114620f3e863SLeonid Bloch     [GPRC]    = mac_read_clr4,      [GPTC]    = mac_read_clr4,
114720f3e863SLeonid Bloch     [TPT]     = mac_read_clr4,      [TPR]     = mac_read_clr4,
11483b274301SLeonid Bloch     [RUC]     = mac_read_clr4,      [ROC]     = mac_read_clr4,
11493b274301SLeonid Bloch     [BPRC]    = mac_read_clr4,      [MPRC]    = mac_read_clr4,
11503b274301SLeonid Bloch     [TSCTC]   = mac_read_clr4,      [BPTC]    = mac_read_clr4,
11513b274301SLeonid Bloch     [MPTC]    = mac_read_clr4,
115220f3e863SLeonid Bloch     [ICR]     = mac_icr_read,       [EECD]    = get_eecd,
115320f3e863SLeonid Bloch     [EERD]    = flash_eerd_read,
115472ea771cSLeonid Bloch     [RDFH]    = mac_low13_read,     [RDFT]    = mac_low13_read,
115572ea771cSLeonid Bloch     [RDFHS]   = mac_low13_read,     [RDFTS]   = mac_low13_read,
115672ea771cSLeonid Bloch     [RDFPC]   = mac_low13_read,
115772ea771cSLeonid Bloch     [TDFH]    = mac_low11_read,     [TDFT]    = mac_low11_read,
115872ea771cSLeonid Bloch     [TDFHS]   = mac_low13_read,     [TDFTS]   = mac_low13_read,
115972ea771cSLeonid Bloch     [TDFPC]   = mac_low13_read,
116072ea771cSLeonid Bloch     [AIT]     = mac_low16_read,
116120f3e863SLeonid Bloch 
11627c23b892Sbalrog     [CRCERRS ... MPC]   = &mac_readreg,
116372ea771cSLeonid Bloch     [IP6AT ... IP6AT+3] = &mac_readreg,    [IP4AT ... IP4AT+6] = &mac_readreg,
116472ea771cSLeonid Bloch     [FFLT ... FFLT+6]   = &mac_low11_read,
11657c23b892Sbalrog     [RA ... RA+31]      = &mac_readreg,
116672ea771cSLeonid Bloch     [WUPM ... WUPM+31]  = &mac_readreg,
11677c23b892Sbalrog     [MTA ... MTA+127]   = &mac_readreg,
11688f2e8d1fSaliguori     [VFTA ... VFTA+127] = &mac_readreg,
116972ea771cSLeonid Bloch     [FFMT ... FFMT+254] = &mac_low4_read,
117072ea771cSLeonid Bloch     [FFVT ... FFVT+254] = &mac_readreg,
117172ea771cSLeonid Bloch     [PBM ... PBM+16383] = &mac_readreg,
11727c23b892Sbalrog };
1173b1503cdaSmalc enum { NREADOPS = ARRAY_SIZE(macreg_readops) };
11747c23b892Sbalrog 
11757c23b892Sbalrog #define putreg(x)    [x] = mac_writereg
11767c23b892Sbalrog static void (*macreg_writeops[])(E1000State *, int, uint32_t) = {
11777c23b892Sbalrog     putreg(PBA),      putreg(EERD),     putreg(SWSM),     putreg(WUFC),
11787c23b892Sbalrog     putreg(TDBAL),    putreg(TDBAH),    putreg(TXDCTL),   putreg(RDBAH),
117972ea771cSLeonid Bloch     putreg(RDBAL),    putreg(LEDCTL),   putreg(VET),      putreg(FCRUC),
118072ea771cSLeonid Bloch     putreg(TDFH),     putreg(TDFT),     putreg(TDFHS),    putreg(TDFTS),
118172ea771cSLeonid Bloch     putreg(TDFPC),    putreg(RDFH),     putreg(RDFT),     putreg(RDFHS),
118272ea771cSLeonid Bloch     putreg(RDFTS),    putreg(RDFPC),    putreg(IPAV),     putreg(WUC),
118372ea771cSLeonid Bloch     putreg(WUS),      putreg(AIT),
118420f3e863SLeonid Bloch 
11857c23b892Sbalrog     [TDLEN]  = set_dlen,   [RDLEN]  = set_dlen,       [TCTL] = set_tctl,
11867c23b892Sbalrog     [TDT]    = set_tctl,   [MDIC]   = set_mdic,       [ICS]  = set_ics,
11877c23b892Sbalrog     [TDH]    = set_16bit,  [RDH]    = set_16bit,      [RDT]  = set_rdt,
11887c23b892Sbalrog     [IMC]    = set_imc,    [IMS]    = set_ims,        [ICR]  = set_icr,
1189cab3c825SKevin Wolf     [EECD]   = set_eecd,   [RCTL]   = set_rx_control, [CTRL] = set_ctrl,
1190e9845f09SVincenzo Maffione     [RDTR]   = set_16bit,  [RADV]   = set_16bit,      [TADV] = set_16bit,
1191e9845f09SVincenzo Maffione     [ITR]    = set_16bit,
119220f3e863SLeonid Bloch 
119372ea771cSLeonid Bloch     [IP6AT ... IP6AT+3] = &mac_writereg, [IP4AT ... IP4AT+6] = &mac_writereg,
119472ea771cSLeonid Bloch     [FFLT ... FFLT+6]   = &mac_writereg,
11957c23b892Sbalrog     [RA ... RA+31]      = &mac_writereg,
119672ea771cSLeonid Bloch     [WUPM ... WUPM+31]  = &mac_writereg,
11977c23b892Sbalrog     [MTA ... MTA+127]   = &mac_writereg,
11988f2e8d1fSaliguori     [VFTA ... VFTA+127] = &mac_writereg,
119972ea771cSLeonid Bloch     [FFMT ... FFMT+254] = &mac_writereg, [FFVT ... FFVT+254] = &mac_writereg,
120072ea771cSLeonid Bloch     [PBM ... PBM+16383] = &mac_writereg,
12017c23b892Sbalrog };
1202b9d03e35SJason Wang 
1203b1503cdaSmalc enum { NWRITEOPS = ARRAY_SIZE(macreg_writeops) };
12047c23b892Sbalrog 
1205bc0f0674SLeonid Bloch enum { MAC_ACCESS_PARTIAL = 1, MAC_ACCESS_FLAG_NEEDED = 2 };
1206bc0f0674SLeonid Bloch 
1207bc0f0674SLeonid Bloch #define markflag(x)    ((E1000_FLAG_##x << 2) | MAC_ACCESS_FLAG_NEEDED)
1208bc0f0674SLeonid Bloch /* In the array below the meaning of the bits is: [f|f|f|f|f|f|n|p]
1209bc0f0674SLeonid Bloch  * f - flag bits (up to 6 possible flags)
1210bc0f0674SLeonid Bloch  * n - flag needed
1211bc0f0674SLeonid Bloch  * p - partially implenented */
1212bc0f0674SLeonid Bloch static const uint8_t mac_reg_access[0x8000] = {
1213bc0f0674SLeonid Bloch     [RDTR]    = markflag(MIT),    [TADV]    = markflag(MIT),
1214bc0f0674SLeonid Bloch     [RADV]    = markflag(MIT),    [ITR]     = markflag(MIT),
121572ea771cSLeonid Bloch 
121672ea771cSLeonid Bloch     [IPAV]    = markflag(MAC),    [WUC]     = markflag(MAC),
121772ea771cSLeonid Bloch     [IP6AT]   = markflag(MAC),    [IP4AT]   = markflag(MAC),
121872ea771cSLeonid Bloch     [FFVT]    = markflag(MAC),    [WUPM]    = markflag(MAC),
121972ea771cSLeonid Bloch     [ECOL]    = markflag(MAC),    [MCC]     = markflag(MAC),
122072ea771cSLeonid Bloch     [DC]      = markflag(MAC),    [TNCRS]   = markflag(MAC),
122172ea771cSLeonid Bloch     [RLEC]    = markflag(MAC),    [XONRXC]  = markflag(MAC),
122272ea771cSLeonid Bloch     [XOFFTXC] = markflag(MAC),    [RFC]     = markflag(MAC),
122372ea771cSLeonid Bloch     [TSCTFC]  = markflag(MAC),    [MGTPRC]  = markflag(MAC),
122472ea771cSLeonid Bloch     [WUS]     = markflag(MAC),    [AIT]     = markflag(MAC),
122572ea771cSLeonid Bloch     [FFLT]    = markflag(MAC),    [FFMT]    = markflag(MAC),
122672ea771cSLeonid Bloch     [SCC]     = markflag(MAC),    [FCRUC]   = markflag(MAC),
122772ea771cSLeonid Bloch     [LATECOL] = markflag(MAC),    [COLC]    = markflag(MAC),
1228757704f1SKamil Rytarowski     [SEQEC]   = markflag(MAC),    [CEXTERR] = markflag(MAC),
122972ea771cSLeonid Bloch     [XONTXC]  = markflag(MAC),    [XOFFRXC] = markflag(MAC),
123072ea771cSLeonid Bloch     [RJC]     = markflag(MAC),    [RNBC]    = markflag(MAC),
123172ea771cSLeonid Bloch     [MGTPDC]  = markflag(MAC),    [MGTPTC]  = markflag(MAC),
12323b274301SLeonid Bloch     [RUC]     = markflag(MAC),    [ROC]     = markflag(MAC),
12333b274301SLeonid Bloch     [GORCL]   = markflag(MAC),    [GORCH]   = markflag(MAC),
12343b274301SLeonid Bloch     [GOTCL]   = markflag(MAC),    [GOTCH]   = markflag(MAC),
12353b274301SLeonid Bloch     [BPRC]    = markflag(MAC),    [MPRC]    = markflag(MAC),
12363b274301SLeonid Bloch     [TSCTC]   = markflag(MAC),    [PRC64]   = markflag(MAC),
12373b274301SLeonid Bloch     [PRC127]  = markflag(MAC),    [PRC255]  = markflag(MAC),
12383b274301SLeonid Bloch     [PRC511]  = markflag(MAC),    [PRC1023] = markflag(MAC),
12393b274301SLeonid Bloch     [PRC1522] = markflag(MAC),    [PTC64]   = markflag(MAC),
12403b274301SLeonid Bloch     [PTC127]  = markflag(MAC),    [PTC255]  = markflag(MAC),
12413b274301SLeonid Bloch     [PTC511]  = markflag(MAC),    [PTC1023] = markflag(MAC),
12423b274301SLeonid Bloch     [PTC1522] = markflag(MAC),    [MPTC]    = markflag(MAC),
12433b274301SLeonid Bloch     [BPTC]    = markflag(MAC),
124472ea771cSLeonid Bloch 
124572ea771cSLeonid Bloch     [TDFH]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
124672ea771cSLeonid Bloch     [TDFT]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
124772ea771cSLeonid Bloch     [TDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
124872ea771cSLeonid Bloch     [TDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
124972ea771cSLeonid Bloch     [TDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL,
125072ea771cSLeonid Bloch     [RDFH]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
125172ea771cSLeonid Bloch     [RDFT]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
125272ea771cSLeonid Bloch     [RDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
125372ea771cSLeonid Bloch     [RDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
125472ea771cSLeonid Bloch     [RDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL,
125572ea771cSLeonid Bloch     [PBM]   = markflag(MAC) | MAC_ACCESS_PARTIAL,
1256bc0f0674SLeonid Bloch };
1257bc0f0674SLeonid Bloch 
12587c23b892Sbalrog static void
1259a8170e5eSAvi Kivity e1000_mmio_write(void *opaque, hwaddr addr, uint64_t val,
1260ad00a9b9SAvi Kivity                  unsigned size)
12617c23b892Sbalrog {
12627c23b892Sbalrog     E1000State *s = opaque;
12638da3ff18Spbrook     unsigned int index = (addr & 0x1ffff) >> 2;
12647c23b892Sbalrog 
126543ad7e3eSJes Sorensen     if (index < NWRITEOPS && macreg_writeops[index]) {
1266bc0f0674SLeonid Bloch         if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED)
1267bc0f0674SLeonid Bloch             || (s->compat_flags & (mac_reg_access[index] >> 2))) {
1268bc0f0674SLeonid Bloch             if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
1269bc0f0674SLeonid Bloch                 DBGOUT(GENERAL, "Writing to register at offset: 0x%08x. "
1270bc0f0674SLeonid Bloch                        "It is not fully implemented.\n", index<<2);
1271bc0f0674SLeonid Bloch             }
12726b59fc74Saurel32             macreg_writeops[index](s, index, val);
1273bc0f0674SLeonid Bloch         } else {    /* "flag needed" bit is set, but the flag is not active */
1274bc0f0674SLeonid Bloch             DBGOUT(MMIO, "MMIO write attempt to disabled reg. addr=0x%08x\n",
1275bc0f0674SLeonid Bloch                    index<<2);
1276bc0f0674SLeonid Bloch         }
127743ad7e3eSJes Sorensen     } else if (index < NREADOPS && macreg_readops[index]) {
1278bc0f0674SLeonid Bloch         DBGOUT(MMIO, "e1000_mmio_writel RO %x: 0x%04"PRIx64"\n",
1279bc0f0674SLeonid Bloch                index<<2, val);
128043ad7e3eSJes Sorensen     } else {
1281ad00a9b9SAvi Kivity         DBGOUT(UNKNOWN, "MMIO unknown write addr=0x%08x,val=0x%08"PRIx64"\n",
12827c23b892Sbalrog                index<<2, val);
12837c23b892Sbalrog     }
128443ad7e3eSJes Sorensen }
12857c23b892Sbalrog 
1286ad00a9b9SAvi Kivity static uint64_t
1287a8170e5eSAvi Kivity e1000_mmio_read(void *opaque, hwaddr addr, unsigned size)
12887c23b892Sbalrog {
12897c23b892Sbalrog     E1000State *s = opaque;
12908da3ff18Spbrook     unsigned int index = (addr & 0x1ffff) >> 2;
12917c23b892Sbalrog 
1292bc0f0674SLeonid Bloch     if (index < NREADOPS && macreg_readops[index]) {
1293bc0f0674SLeonid Bloch         if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED)
1294bc0f0674SLeonid Bloch             || (s->compat_flags & (mac_reg_access[index] >> 2))) {
1295bc0f0674SLeonid Bloch             if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
1296bc0f0674SLeonid Bloch                 DBGOUT(GENERAL, "Reading register at offset: 0x%08x. "
1297bc0f0674SLeonid Bloch                        "It is not fully implemented.\n", index<<2);
12986b59fc74Saurel32             }
1299bc0f0674SLeonid Bloch             return macreg_readops[index](s, index);
1300bc0f0674SLeonid Bloch         } else {    /* "flag needed" bit is set, but the flag is not active */
1301bc0f0674SLeonid Bloch             DBGOUT(MMIO, "MMIO read attempt of disabled reg. addr=0x%08x\n",
1302bc0f0674SLeonid Bloch                    index<<2);
1303bc0f0674SLeonid Bloch         }
1304bc0f0674SLeonid Bloch     } else {
13057c23b892Sbalrog         DBGOUT(UNKNOWN, "MMIO unknown read addr=0x%08x\n", index<<2);
1306bc0f0674SLeonid Bloch     }
13077c23b892Sbalrog     return 0;
13087c23b892Sbalrog }
13097c23b892Sbalrog 
1310ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_mmio_ops = {
1311ad00a9b9SAvi Kivity     .read = e1000_mmio_read,
1312ad00a9b9SAvi Kivity     .write = e1000_mmio_write,
1313ad00a9b9SAvi Kivity     .endianness = DEVICE_LITTLE_ENDIAN,
1314ad00a9b9SAvi Kivity     .impl = {
1315ad00a9b9SAvi Kivity         .min_access_size = 4,
1316ad00a9b9SAvi Kivity         .max_access_size = 4,
1317ad00a9b9SAvi Kivity     },
1318ad00a9b9SAvi Kivity };
1319ad00a9b9SAvi Kivity 
1320a8170e5eSAvi Kivity static uint64_t e1000_io_read(void *opaque, hwaddr addr,
1321ad00a9b9SAvi Kivity                               unsigned size)
13227c23b892Sbalrog {
1323ad00a9b9SAvi Kivity     E1000State *s = opaque;
1324ad00a9b9SAvi Kivity 
1325ad00a9b9SAvi Kivity     (void)s;
1326ad00a9b9SAvi Kivity     return 0;
13277c23b892Sbalrog }
13287c23b892Sbalrog 
1329a8170e5eSAvi Kivity static void e1000_io_write(void *opaque, hwaddr addr,
1330ad00a9b9SAvi Kivity                            uint64_t val, unsigned size)
13317c23b892Sbalrog {
1332ad00a9b9SAvi Kivity     E1000State *s = opaque;
1333ad00a9b9SAvi Kivity 
1334ad00a9b9SAvi Kivity     (void)s;
13357c23b892Sbalrog }
13367c23b892Sbalrog 
1337ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_io_ops = {
1338ad00a9b9SAvi Kivity     .read = e1000_io_read,
1339ad00a9b9SAvi Kivity     .write = e1000_io_write,
1340ad00a9b9SAvi Kivity     .endianness = DEVICE_LITTLE_ENDIAN,
1341ad00a9b9SAvi Kivity };
1342ad00a9b9SAvi Kivity 
1343e482dc3eSJuan Quintela static bool is_version_1(void *opaque, int version_id)
13447c23b892Sbalrog {
1345e482dc3eSJuan Quintela     return version_id == 1;
13467c23b892Sbalrog }
13477c23b892Sbalrog 
134844b1ff31SDr. David Alan Gilbert static int e1000_pre_save(void *opaque)
1349ddcb73b7SMichael S. Tsirkin {
1350ddcb73b7SMichael S. Tsirkin     E1000State *s = opaque;
1351ddcb73b7SMichael S. Tsirkin     NetClientState *nc = qemu_get_queue(s->nic);
13522af234e6SMichael S. Tsirkin 
1353e9845f09SVincenzo Maffione     /* If the mitigation timer is active, emulate a timeout now. */
1354e9845f09SVincenzo Maffione     if (s->mit_timer_on) {
1355e9845f09SVincenzo Maffione         e1000_mit_timer(s);
1356e9845f09SVincenzo Maffione     }
1357e9845f09SVincenzo Maffione 
1358ddcb73b7SMichael S. Tsirkin     /*
13596a2acedbSGabriel L. Somlo      * If link is down and auto-negotiation is supported and ongoing,
13606a2acedbSGabriel L. Somlo      * complete auto-negotiation immediately. This allows us to look
13616a2acedbSGabriel L. Somlo      * at MII_SR_AUTONEG_COMPLETE to infer link status on load.
1362ddcb73b7SMichael S. Tsirkin      */
1363d7a41552SGabriel L. Somlo     if (nc->link_down && have_autoneg(s)) {
1364ddcb73b7SMichael S. Tsirkin         s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
1365ddcb73b7SMichael S. Tsirkin     }
136644b1ff31SDr. David Alan Gilbert 
136744b1ff31SDr. David Alan Gilbert     return 0;
1368ddcb73b7SMichael S. Tsirkin }
1369ddcb73b7SMichael S. Tsirkin 
1370e4b82364SAmos Kong static int e1000_post_load(void *opaque, int version_id)
1371e4b82364SAmos Kong {
1372e4b82364SAmos Kong     E1000State *s = opaque;
1373b356f76dSJason Wang     NetClientState *nc = qemu_get_queue(s->nic);
1374e4b82364SAmos Kong 
1375bc0f0674SLeonid Bloch     if (!chkflag(MIT)) {
1376e9845f09SVincenzo Maffione         s->mac_reg[ITR] = s->mac_reg[RDTR] = s->mac_reg[RADV] =
1377e9845f09SVincenzo Maffione             s->mac_reg[TADV] = 0;
1378e9845f09SVincenzo Maffione         s->mit_irq_level = false;
1379e9845f09SVincenzo Maffione     }
1380e9845f09SVincenzo Maffione     s->mit_ide = 0;
1381e9845f09SVincenzo Maffione     s->mit_timer_on = false;
1382e9845f09SVincenzo Maffione 
1383e4b82364SAmos Kong     /* nc.link_down can't be migrated, so infer link_down according
1384ddcb73b7SMichael S. Tsirkin      * to link status bit in mac_reg[STATUS].
1385ddcb73b7SMichael S. Tsirkin      * Alternatively, restart link negotiation if it was in progress. */
1386b356f76dSJason Wang     nc->link_down = (s->mac_reg[STATUS] & E1000_STATUS_LU) == 0;
13872af234e6SMichael S. Tsirkin 
1388d7a41552SGabriel L. Somlo     if (have_autoneg(s) &&
1389ddcb73b7SMichael S. Tsirkin         !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
1390ddcb73b7SMichael S. Tsirkin         nc->link_down = false;
1391d7a41552SGabriel L. Somlo         timer_mod(s->autoneg_timer,
1392d7a41552SGabriel L. Somlo                   qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
1393ddcb73b7SMichael S. Tsirkin     }
1394e4b82364SAmos Kong 
1395e4b82364SAmos Kong     return 0;
1396e4b82364SAmos Kong }
1397e4b82364SAmos Kong 
1398e9845f09SVincenzo Maffione static bool e1000_mit_state_needed(void *opaque)
1399e9845f09SVincenzo Maffione {
1400e9845f09SVincenzo Maffione     E1000State *s = opaque;
1401e9845f09SVincenzo Maffione 
1402bc0f0674SLeonid Bloch     return chkflag(MIT);
1403e9845f09SVincenzo Maffione }
1404e9845f09SVincenzo Maffione 
14059e117734SLeonid Bloch static bool e1000_full_mac_needed(void *opaque)
14069e117734SLeonid Bloch {
14079e117734SLeonid Bloch     E1000State *s = opaque;
14089e117734SLeonid Bloch 
1409bc0f0674SLeonid Bloch     return chkflag(MAC);
14109e117734SLeonid Bloch }
14119e117734SLeonid Bloch 
1412e9845f09SVincenzo Maffione static const VMStateDescription vmstate_e1000_mit_state = {
1413e9845f09SVincenzo Maffione     .name = "e1000/mit_state",
1414e9845f09SVincenzo Maffione     .version_id = 1,
1415e9845f09SVincenzo Maffione     .minimum_version_id = 1,
14165cd8cadaSJuan Quintela     .needed = e1000_mit_state_needed,
1417e9845f09SVincenzo Maffione     .fields = (VMStateField[]) {
1418e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[RDTR], E1000State),
1419e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[RADV], E1000State),
1420e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[TADV], E1000State),
1421e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[ITR], E1000State),
1422e9845f09SVincenzo Maffione         VMSTATE_BOOL(mit_irq_level, E1000State),
1423e9845f09SVincenzo Maffione         VMSTATE_END_OF_LIST()
1424e9845f09SVincenzo Maffione     }
1425e9845f09SVincenzo Maffione };
1426e9845f09SVincenzo Maffione 
14279e117734SLeonid Bloch static const VMStateDescription vmstate_e1000_full_mac_state = {
14289e117734SLeonid Bloch     .name = "e1000/full_mac_state",
14299e117734SLeonid Bloch     .version_id = 1,
14309e117734SLeonid Bloch     .minimum_version_id = 1,
14319e117734SLeonid Bloch     .needed = e1000_full_mac_needed,
14329e117734SLeonid Bloch     .fields = (VMStateField[]) {
14339e117734SLeonid Bloch         VMSTATE_UINT32_ARRAY(mac_reg, E1000State, 0x8000),
14349e117734SLeonid Bloch         VMSTATE_END_OF_LIST()
14359e117734SLeonid Bloch     }
14369e117734SLeonid Bloch };
14379e117734SLeonid Bloch 
1438e482dc3eSJuan Quintela static const VMStateDescription vmstate_e1000 = {
1439e482dc3eSJuan Quintela     .name = "e1000",
1440e482dc3eSJuan Quintela     .version_id = 2,
1441e482dc3eSJuan Quintela     .minimum_version_id = 1,
1442ddcb73b7SMichael S. Tsirkin     .pre_save = e1000_pre_save,
1443e4b82364SAmos Kong     .post_load = e1000_post_load,
1444e482dc3eSJuan Quintela     .fields = (VMStateField[]) {
1445b08340d5SAndreas Färber         VMSTATE_PCI_DEVICE(parent_obj, E1000State),
1446e482dc3eSJuan Quintela         VMSTATE_UNUSED_TEST(is_version_1, 4), /* was instance id */
1447e482dc3eSJuan Quintela         VMSTATE_UNUSED(4), /* Was mmio_base.  */
1448e482dc3eSJuan Quintela         VMSTATE_UINT32(rxbuf_size, E1000State),
1449e482dc3eSJuan Quintela         VMSTATE_UINT32(rxbuf_min_shift, E1000State),
1450e482dc3eSJuan Quintela         VMSTATE_UINT32(eecd_state.val_in, E1000State),
1451e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.bitnum_in, E1000State),
1452e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.bitnum_out, E1000State),
1453e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.reading, E1000State),
1454e482dc3eSJuan Quintela         VMSTATE_UINT32(eecd_state.old_eecd, E1000State),
1455093454e2SDmitry Fleytman         VMSTATE_UINT8(tx.props.ipcss, E1000State),
1456093454e2SDmitry Fleytman         VMSTATE_UINT8(tx.props.ipcso, E1000State),
1457093454e2SDmitry Fleytman         VMSTATE_UINT16(tx.props.ipcse, E1000State),
1458093454e2SDmitry Fleytman         VMSTATE_UINT8(tx.props.tucss, E1000State),
1459093454e2SDmitry Fleytman         VMSTATE_UINT8(tx.props.tucso, E1000State),
1460093454e2SDmitry Fleytman         VMSTATE_UINT16(tx.props.tucse, E1000State),
1461093454e2SDmitry Fleytman         VMSTATE_UINT32(tx.props.paylen, E1000State),
1462093454e2SDmitry Fleytman         VMSTATE_UINT8(tx.props.hdr_len, E1000State),
1463093454e2SDmitry Fleytman         VMSTATE_UINT16(tx.props.mss, E1000State),
1464e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.size, E1000State),
1465e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.tso_frames, E1000State),
1466*7d08c73eSEd Swierk via Qemu-devel         VMSTATE_UINT8(tx.sum_needed, E1000State),
1467093454e2SDmitry Fleytman         VMSTATE_INT8(tx.props.ip, E1000State),
1468093454e2SDmitry Fleytman         VMSTATE_INT8(tx.props.tcp, E1000State),
1469e482dc3eSJuan Quintela         VMSTATE_BUFFER(tx.header, E1000State),
1470e482dc3eSJuan Quintela         VMSTATE_BUFFER(tx.data, E1000State),
1471e482dc3eSJuan Quintela         VMSTATE_UINT16_ARRAY(eeprom_data, E1000State, 64),
1472e482dc3eSJuan Quintela         VMSTATE_UINT16_ARRAY(phy_reg, E1000State, 0x20),
1473e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[CTRL], E1000State),
1474e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[EECD], E1000State),
1475e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[EERD], E1000State),
1476e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[GPRC], E1000State),
1477e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[GPTC], E1000State),
1478e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[ICR], E1000State),
1479e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[ICS], E1000State),
1480e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[IMC], E1000State),
1481e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[IMS], E1000State),
1482e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[LEDCTL], E1000State),
1483e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MANC], E1000State),
1484e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MDIC], E1000State),
1485e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MPC], E1000State),
1486e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[PBA], E1000State),
1487e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RCTL], E1000State),
1488e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDBAH], E1000State),
1489e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDBAL], E1000State),
1490e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDH], E1000State),
1491e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDLEN], E1000State),
1492e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDT], E1000State),
1493e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[STATUS], E1000State),
1494e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[SWSM], E1000State),
1495e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TCTL], E1000State),
1496e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDBAH], E1000State),
1497e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDBAL], E1000State),
1498e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDH], E1000State),
1499e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDLEN], E1000State),
1500e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDT], E1000State),
1501e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TORH], E1000State),
1502e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TORL], E1000State),
1503e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TOTH], E1000State),
1504e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TOTL], E1000State),
1505e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TPR], E1000State),
1506e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TPT], E1000State),
1507e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TXDCTL], E1000State),
1508e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[WUFC], E1000State),
1509e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[VET], E1000State),
1510e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, RA, 32),
1511e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, MTA, 128),
1512e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, VFTA, 128),
1513e482dc3eSJuan Quintela         VMSTATE_END_OF_LIST()
1514e9845f09SVincenzo Maffione     },
15155cd8cadaSJuan Quintela     .subsections = (const VMStateDescription*[]) {
15165cd8cadaSJuan Quintela         &vmstate_e1000_mit_state,
15179e117734SLeonid Bloch         &vmstate_e1000_full_mac_state,
15185cd8cadaSJuan Quintela         NULL
15197c23b892Sbalrog     }
1520e482dc3eSJuan Quintela };
15217c23b892Sbalrog 
15228597f2e1SGabriel L. Somlo /*
15238597f2e1SGabriel L. Somlo  * EEPROM contents documented in Tables 5-2 and 5-3, pp. 98-102.
15248597f2e1SGabriel L. Somlo  * Note: A valid DevId will be inserted during pci_e1000_init().
15258597f2e1SGabriel L. Somlo  */
152688b4e9dbSblueswir1 static const uint16_t e1000_eeprom_template[64] = {
15277c23b892Sbalrog     0x0000, 0x0000, 0x0000, 0x0000,      0xffff, 0x0000,      0x0000, 0x0000,
15288597f2e1SGabriel L. Somlo     0x3000, 0x1000, 0x6403, 0 /*DevId*/, 0x8086, 0 /*DevId*/, 0x8086, 0x3040,
15297c23b892Sbalrog     0x0008, 0x2000, 0x7e14, 0x0048,      0x1000, 0x00d8,      0x0000, 0x2700,
15307c23b892Sbalrog     0x6cc9, 0x3150, 0x0722, 0x040b,      0x0984, 0x0000,      0xc000, 0x0706,
15317c23b892Sbalrog     0x1008, 0x0000, 0x0f04, 0x7fff,      0x4d01, 0xffff,      0xffff, 0xffff,
15327c23b892Sbalrog     0xffff, 0xffff, 0xffff, 0xffff,      0xffff, 0xffff,      0xffff, 0xffff,
15337c23b892Sbalrog     0x0100, 0x4000, 0x121c, 0xffff,      0xffff, 0xffff,      0xffff, 0xffff,
15347c23b892Sbalrog     0xffff, 0xffff, 0xffff, 0xffff,      0xffff, 0xffff,      0xffff, 0x0000,
15357c23b892Sbalrog };
15367c23b892Sbalrog 
15377c23b892Sbalrog /* PCI interface */
15387c23b892Sbalrog 
15397c23b892Sbalrog static void
1540ad00a9b9SAvi Kivity e1000_mmio_setup(E1000State *d)
15417c23b892Sbalrog {
1542f65ed4c1Saliguori     int i;
1543f65ed4c1Saliguori     const uint32_t excluded_regs[] = {
1544f65ed4c1Saliguori         E1000_MDIC, E1000_ICR, E1000_ICS, E1000_IMS,
1545f65ed4c1Saliguori         E1000_IMC, E1000_TCTL, E1000_TDT, PNPMMIO_SIZE
1546f65ed4c1Saliguori     };
1547f65ed4c1Saliguori 
1548eedfac6fSPaolo Bonzini     memory_region_init_io(&d->mmio, OBJECT(d), &e1000_mmio_ops, d,
1549eedfac6fSPaolo Bonzini                           "e1000-mmio", PNPMMIO_SIZE);
1550ad00a9b9SAvi Kivity     memory_region_add_coalescing(&d->mmio, 0, excluded_regs[0]);
1551f65ed4c1Saliguori     for (i = 0; excluded_regs[i] != PNPMMIO_SIZE; i++)
1552ad00a9b9SAvi Kivity         memory_region_add_coalescing(&d->mmio, excluded_regs[i] + 4,
1553ad00a9b9SAvi Kivity                                      excluded_regs[i+1] - excluded_regs[i] - 4);
1554eedfac6fSPaolo Bonzini     memory_region_init_io(&d->io, OBJECT(d), &e1000_io_ops, d, "e1000-io", IOPORT_SIZE);
15557c23b892Sbalrog }
15567c23b892Sbalrog 
1557b946a153Saliguori static void
15584b09be85Saliguori pci_e1000_uninit(PCIDevice *dev)
15594b09be85Saliguori {
1560567a3c9eSPeter Crosthwaite     E1000State *d = E1000(dev);
15614b09be85Saliguori 
1562bc72ad67SAlex Bligh     timer_del(d->autoneg_timer);
1563bc72ad67SAlex Bligh     timer_free(d->autoneg_timer);
1564e9845f09SVincenzo Maffione     timer_del(d->mit_timer);
1565e9845f09SVincenzo Maffione     timer_free(d->mit_timer);
1566948ecf21SJason Wang     qemu_del_nic(d->nic);
15674b09be85Saliguori }
15684b09be85Saliguori 
1569a03e2aecSMark McLoughlin static NetClientInfo net_e1000_info = {
1570f394b2e2SEric Blake     .type = NET_CLIENT_DRIVER_NIC,
1571a03e2aecSMark McLoughlin     .size = sizeof(NICState),
1572a03e2aecSMark McLoughlin     .can_receive = e1000_can_receive,
1573a03e2aecSMark McLoughlin     .receive = e1000_receive,
157497410ddeSVincenzo Maffione     .receive_iov = e1000_receive_iov,
1575a03e2aecSMark McLoughlin     .link_status_changed = e1000_set_link_status,
1576a03e2aecSMark McLoughlin };
1577a03e2aecSMark McLoughlin 
157820302e71SMichael S. Tsirkin static void e1000_write_config(PCIDevice *pci_dev, uint32_t address,
157920302e71SMichael S. Tsirkin                                 uint32_t val, int len)
158020302e71SMichael S. Tsirkin {
158120302e71SMichael S. Tsirkin     E1000State *s = E1000(pci_dev);
158220302e71SMichael S. Tsirkin 
158320302e71SMichael S. Tsirkin     pci_default_write_config(pci_dev, address, val, len);
158420302e71SMichael S. Tsirkin 
158520302e71SMichael S. Tsirkin     if (range_covers_byte(address, len, PCI_COMMAND) &&
158620302e71SMichael S. Tsirkin         (pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
158720302e71SMichael S. Tsirkin         qemu_flush_queued_packets(qemu_get_queue(s->nic));
158820302e71SMichael S. Tsirkin     }
158920302e71SMichael S. Tsirkin }
159020302e71SMichael S. Tsirkin 
15919af21dbeSMarkus Armbruster static void pci_e1000_realize(PCIDevice *pci_dev, Error **errp)
15927c23b892Sbalrog {
1593567a3c9eSPeter Crosthwaite     DeviceState *dev = DEVICE(pci_dev);
1594567a3c9eSPeter Crosthwaite     E1000State *d = E1000(pci_dev);
15957c23b892Sbalrog     uint8_t *pci_conf;
1596fbdaa002SGerd Hoffmann     uint8_t *macaddr;
1597aff427a1SChris Wright 
159820302e71SMichael S. Tsirkin     pci_dev->config_write = e1000_write_config;
159920302e71SMichael S. Tsirkin 
1600b08340d5SAndreas Färber     pci_conf = pci_dev->config;
16017c23b892Sbalrog 
1602a9cbacb0SMichael S. Tsirkin     /* TODO: RST# value should be 0, PCI spec 6.2.4 */
1603a9cbacb0SMichael S. Tsirkin     pci_conf[PCI_CACHE_LINE_SIZE] = 0x10;
16047c23b892Sbalrog 
1605817e0b6fSMichael S. Tsirkin     pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
16067c23b892Sbalrog 
1607ad00a9b9SAvi Kivity     e1000_mmio_setup(d);
16087c23b892Sbalrog 
1609b08340d5SAndreas Färber     pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
16107c23b892Sbalrog 
1611b08340d5SAndreas Färber     pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->io);
16127c23b892Sbalrog 
1613fbdaa002SGerd Hoffmann     qemu_macaddr_default_if_unset(&d->conf.macaddr);
1614fbdaa002SGerd Hoffmann     macaddr = d->conf.macaddr.a;
1615093454e2SDmitry Fleytman 
1616093454e2SDmitry Fleytman     e1000x_core_prepare_eeprom(d->eeprom_data,
1617093454e2SDmitry Fleytman                                e1000_eeprom_template,
1618093454e2SDmitry Fleytman                                sizeof(e1000_eeprom_template),
1619093454e2SDmitry Fleytman                                PCI_DEVICE_GET_CLASS(pci_dev)->device_id,
1620093454e2SDmitry Fleytman                                macaddr);
16217c23b892Sbalrog 
1622a03e2aecSMark McLoughlin     d->nic = qemu_new_nic(&net_e1000_info, &d->conf,
1623567a3c9eSPeter Crosthwaite                           object_get_typename(OBJECT(d)), dev->id, d);
16247c23b892Sbalrog 
1625b356f76dSJason Wang     qemu_format_nic_info_str(qemu_get_queue(d->nic), macaddr);
16261ca4d09aSGleb Natapov 
1627bc72ad67SAlex Bligh     d->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, e1000_autoneg_timer, d);
1628e9845f09SVincenzo Maffione     d->mit_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000_mit_timer, d);
16297c23b892Sbalrog }
16309d07d757SPaul Brook 
1631fbdaa002SGerd Hoffmann static void qdev_e1000_reset(DeviceState *dev)
1632fbdaa002SGerd Hoffmann {
1633567a3c9eSPeter Crosthwaite     E1000State *d = E1000(dev);
1634fbdaa002SGerd Hoffmann     e1000_reset(d);
1635fbdaa002SGerd Hoffmann }
1636fbdaa002SGerd Hoffmann 
163740021f08SAnthony Liguori static Property e1000_properties[] = {
1638fbdaa002SGerd Hoffmann     DEFINE_NIC_PROPERTIES(E1000State, conf),
16392af234e6SMichael S. Tsirkin     DEFINE_PROP_BIT("autonegotiation", E1000State,
16402af234e6SMichael S. Tsirkin                     compat_flags, E1000_FLAG_AUTONEG_BIT, true),
1641e9845f09SVincenzo Maffione     DEFINE_PROP_BIT("mitigation", E1000State,
1642e9845f09SVincenzo Maffione                     compat_flags, E1000_FLAG_MIT_BIT, true),
1643ba63ec85SLeonid Bloch     DEFINE_PROP_BIT("extra_mac_registers", E1000State,
1644ba63ec85SLeonid Bloch                     compat_flags, E1000_FLAG_MAC_BIT, true),
1645fbdaa002SGerd Hoffmann     DEFINE_PROP_END_OF_LIST(),
164640021f08SAnthony Liguori };
164740021f08SAnthony Liguori 
16488597f2e1SGabriel L. Somlo typedef struct E1000Info {
16498597f2e1SGabriel L. Somlo     const char *name;
16508597f2e1SGabriel L. Somlo     uint16_t   device_id;
16518597f2e1SGabriel L. Somlo     uint8_t    revision;
16528597f2e1SGabriel L. Somlo     uint16_t   phy_id2;
16538597f2e1SGabriel L. Somlo } E1000Info;
16548597f2e1SGabriel L. Somlo 
165540021f08SAnthony Liguori static void e1000_class_init(ObjectClass *klass, void *data)
165640021f08SAnthony Liguori {
165739bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
165840021f08SAnthony Liguori     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
16598597f2e1SGabriel L. Somlo     E1000BaseClass *e = E1000_DEVICE_CLASS(klass);
16608597f2e1SGabriel L. Somlo     const E1000Info *info = data;
166140021f08SAnthony Liguori 
16629af21dbeSMarkus Armbruster     k->realize = pci_e1000_realize;
166340021f08SAnthony Liguori     k->exit = pci_e1000_uninit;
1664c45e5b5bSGerd Hoffmann     k->romfile = "efi-e1000.rom";
166540021f08SAnthony Liguori     k->vendor_id = PCI_VENDOR_ID_INTEL;
16668597f2e1SGabriel L. Somlo     k->device_id = info->device_id;
16678597f2e1SGabriel L. Somlo     k->revision = info->revision;
16688597f2e1SGabriel L. Somlo     e->phy_id2 = info->phy_id2;
166940021f08SAnthony Liguori     k->class_id = PCI_CLASS_NETWORK_ETHERNET;
1670125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
167139bffca2SAnthony Liguori     dc->desc = "Intel Gigabit Ethernet";
167239bffca2SAnthony Liguori     dc->reset = qdev_e1000_reset;
167339bffca2SAnthony Liguori     dc->vmsd = &vmstate_e1000;
167439bffca2SAnthony Liguori     dc->props = e1000_properties;
1675fbdaa002SGerd Hoffmann }
167640021f08SAnthony Liguori 
16775df3bf62SGonglei static void e1000_instance_init(Object *obj)
16785df3bf62SGonglei {
16795df3bf62SGonglei     E1000State *n = E1000(obj);
16805df3bf62SGonglei     device_add_bootindex_property(obj, &n->conf.bootindex,
16815df3bf62SGonglei                                   "bootindex", "/ethernet-phy@0",
16825df3bf62SGonglei                                   DEVICE(n), NULL);
16835df3bf62SGonglei }
16845df3bf62SGonglei 
16858597f2e1SGabriel L. Somlo static const TypeInfo e1000_base_info = {
16868597f2e1SGabriel L. Somlo     .name          = TYPE_E1000_BASE,
168739bffca2SAnthony Liguori     .parent        = TYPE_PCI_DEVICE,
168839bffca2SAnthony Liguori     .instance_size = sizeof(E1000State),
16895df3bf62SGonglei     .instance_init = e1000_instance_init,
16908597f2e1SGabriel L. Somlo     .class_size    = sizeof(E1000BaseClass),
16918597f2e1SGabriel L. Somlo     .abstract      = true,
1692fd3b02c8SEduardo Habkost     .interfaces = (InterfaceInfo[]) {
1693fd3b02c8SEduardo Habkost         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1694fd3b02c8SEduardo Habkost         { },
1695fd3b02c8SEduardo Habkost     },
16968597f2e1SGabriel L. Somlo };
16978597f2e1SGabriel L. Somlo 
16988597f2e1SGabriel L. Somlo static const E1000Info e1000_devices[] = {
16998597f2e1SGabriel L. Somlo     {
170083044020SJason Wang         .name      = "e1000",
17018597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82540EM,
17028597f2e1SGabriel L. Somlo         .revision  = 0x03,
17038597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_8254xx_DEFAULT,
17048597f2e1SGabriel L. Somlo     },
17058597f2e1SGabriel L. Somlo     {
17068597f2e1SGabriel L. Somlo         .name      = "e1000-82544gc",
17078597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82544GC_COPPER,
17088597f2e1SGabriel L. Somlo         .revision  = 0x03,
17098597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_82544x,
17108597f2e1SGabriel L. Somlo     },
17118597f2e1SGabriel L. Somlo     {
17128597f2e1SGabriel L. Somlo         .name      = "e1000-82545em",
17138597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82545EM_COPPER,
17148597f2e1SGabriel L. Somlo         .revision  = 0x03,
17158597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_8254xx_DEFAULT,
17168597f2e1SGabriel L. Somlo     },
17178597f2e1SGabriel L. Somlo };
17188597f2e1SGabriel L. Somlo 
171983f7d43aSAndreas Färber static void e1000_register_types(void)
17209d07d757SPaul Brook {
17218597f2e1SGabriel L. Somlo     int i;
17228597f2e1SGabriel L. Somlo 
17238597f2e1SGabriel L. Somlo     type_register_static(&e1000_base_info);
17248597f2e1SGabriel L. Somlo     for (i = 0; i < ARRAY_SIZE(e1000_devices); i++) {
17258597f2e1SGabriel L. Somlo         const E1000Info *info = &e1000_devices[i];
17268597f2e1SGabriel L. Somlo         TypeInfo type_info = {};
17278597f2e1SGabriel L. Somlo 
17288597f2e1SGabriel L. Somlo         type_info.name = info->name;
17298597f2e1SGabriel L. Somlo         type_info.parent = TYPE_E1000_BASE;
17308597f2e1SGabriel L. Somlo         type_info.class_data = (void *)info;
17318597f2e1SGabriel L. Somlo         type_info.class_init = e1000_class_init;
17325df3bf62SGonglei         type_info.instance_init = e1000_instance_init;
17338597f2e1SGabriel L. Somlo 
17348597f2e1SGabriel L. Somlo         type_register(&type_info);
17358597f2e1SGabriel L. Somlo     }
17369d07d757SPaul Brook }
17379d07d757SPaul Brook 
173883f7d43aSAndreas Färber type_init(e1000_register_types)
1739