xref: /qemu/hw/net/e1000.c (revision 3b6b3a279ad3c32903b0c0fdf3cacf24b02541f2)
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/pci/pci.h"
30a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
31d6454270SMarkus Armbruster #include "migration/vmstate.h"
321422e32dSPaolo Bonzini #include "net/net.h"
337200ac3cSMark McLoughlin #include "net/checksum.h"
349c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
359c17d615SPaolo Bonzini #include "sysemu/dma.h"
3697410ddeSVincenzo Maffione #include "qemu/iov.h"
370b8fa32fSMarkus Armbruster #include "qemu/module.h"
3820302e71SMichael S. Tsirkin #include "qemu/range.h"
397c23b892Sbalrog 
40093454e2SDmitry Fleytman #include "e1000x_common.h"
411001cf45SJason Wang #include "trace.h"
427c23b892Sbalrog 
433b274301SLeonid Bloch static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
443b274301SLeonid Bloch 
45b4053c64SJason Wang /* #define E1000_DEBUG */
467c23b892Sbalrog 
4727124888SJes Sorensen #ifdef E1000_DEBUG
487c23b892Sbalrog enum {
497c23b892Sbalrog     DEBUG_GENERAL,      DEBUG_IO,       DEBUG_MMIO,     DEBUG_INTERRUPT,
507c23b892Sbalrog     DEBUG_RX,           DEBUG_TX,       DEBUG_MDIC,     DEBUG_EEPROM,
517c23b892Sbalrog     DEBUG_UNKNOWN,      DEBUG_TXSUM,    DEBUG_TXERR,    DEBUG_RXERR,
52f9c1cdf4SJason Wang     DEBUG_RXFILTER,     DEBUG_PHY,      DEBUG_NOTYET,
537c23b892Sbalrog };
547c23b892Sbalrog #define DBGBIT(x)    (1<<DEBUG_##x)
557c23b892Sbalrog static int debugflags = DBGBIT(TXERR) | DBGBIT(GENERAL);
567c23b892Sbalrog 
576c7f4b47SBlue Swirl #define DBGOUT(what, fmt, ...) do { \
587c23b892Sbalrog     if (debugflags & DBGBIT(what)) \
596c7f4b47SBlue Swirl         fprintf(stderr, "e1000: " fmt, ## __VA_ARGS__); \
607c23b892Sbalrog     } while (0)
617c23b892Sbalrog #else
626c7f4b47SBlue Swirl #define DBGOUT(what, fmt, ...) do {} while (0)
637c23b892Sbalrog #endif
647c23b892Sbalrog 
657c23b892Sbalrog #define IOPORT_SIZE       0x40
66e94bbefeSaurel32 #define PNPMMIO_SIZE      0x20000
6778aeb23eSStefan Hajnoczi #define MIN_BUF_SIZE      60 /* Min. octets in an ethernet frame sans FCS */
687c23b892Sbalrog 
6997410ddeSVincenzo Maffione #define MAXIMUM_ETHERNET_HDR_LEN (14+4)
7097410ddeSVincenzo Maffione 
717c23b892Sbalrog /*
727c23b892Sbalrog  * HW models:
738597f2e1SGabriel L. Somlo  *  E1000_DEV_ID_82540EM works with Windows, Linux, and OS X <= 10.8
747c23b892Sbalrog  *  E1000_DEV_ID_82544GC_COPPER appears to work; not well tested
758597f2e1SGabriel L. Somlo  *  E1000_DEV_ID_82545EM_COPPER works with Linux and OS X >= 10.6
767c23b892Sbalrog  *  Others never tested
777c23b892Sbalrog  */
787c23b892Sbalrog 
797c23b892Sbalrog typedef struct E1000State_st {
80b08340d5SAndreas Färber     /*< private >*/
81b08340d5SAndreas Färber     PCIDevice parent_obj;
82b08340d5SAndreas Färber     /*< public >*/
83b08340d5SAndreas Färber 
84a03e2aecSMark McLoughlin     NICState *nic;
85fbdaa002SGerd Hoffmann     NICConf conf;
86ad00a9b9SAvi Kivity     MemoryRegion mmio;
87ad00a9b9SAvi Kivity     MemoryRegion io;
887c23b892Sbalrog 
897c23b892Sbalrog     uint32_t mac_reg[0x8000];
907c23b892Sbalrog     uint16_t phy_reg[0x20];
917c23b892Sbalrog     uint16_t eeprom_data[64];
927c23b892Sbalrog 
937c23b892Sbalrog     uint32_t rxbuf_size;
947c23b892Sbalrog     uint32_t rxbuf_min_shift;
957c23b892Sbalrog     struct e1000_tx {
967c23b892Sbalrog         unsigned char header[256];
978f2e8d1fSaliguori         unsigned char vlan_header[4];
98b10fec9bSStefan Weil         /* Fields vlan and data must not be reordered or separated. */
998f2e8d1fSaliguori         unsigned char vlan[4];
1007c23b892Sbalrog         unsigned char data[0x10000];
1017c23b892Sbalrog         uint16_t size;
1028f2e8d1fSaliguori         unsigned char vlan_needed;
1037d08c73eSEd Swierk via Qemu-devel         unsigned char sum_needed;
1047d08c73eSEd Swierk via Qemu-devel         bool cptse;
105093454e2SDmitry Fleytman         e1000x_txd_props props;
106d62644b4SEd Swierk via Qemu-devel         e1000x_txd_props tso_props;
1077c23b892Sbalrog         uint16_t tso_frames;
1087c23b892Sbalrog     } tx;
1097c23b892Sbalrog 
1107c23b892Sbalrog     struct {
11120f3e863SLeonid Bloch         uint32_t val_in;    /* shifted in from guest driver */
1127c23b892Sbalrog         uint16_t bitnum_in;
1137c23b892Sbalrog         uint16_t bitnum_out;
1147c23b892Sbalrog         uint16_t reading;
1157c23b892Sbalrog         uint32_t old_eecd;
1167c23b892Sbalrog     } eecd_state;
117b9d03e35SJason Wang 
118b9d03e35SJason Wang     QEMUTimer *autoneg_timer;
1192af234e6SMichael S. Tsirkin 
120e9845f09SVincenzo Maffione     QEMUTimer *mit_timer;      /* Mitigation timer. */
121e9845f09SVincenzo Maffione     bool mit_timer_on;         /* Mitigation timer is running. */
122e9845f09SVincenzo Maffione     bool mit_irq_level;        /* Tracks interrupt pin level. */
123e9845f09SVincenzo Maffione     uint32_t mit_ide;          /* Tracks E1000_TXD_CMD_IDE bit. */
124e9845f09SVincenzo Maffione 
125157628d0Syuchenlin     QEMUTimer *flush_queue_timer;
126157628d0Syuchenlin 
1272af234e6SMichael S. Tsirkin /* Compatibility flags for migration to/from qemu 1.3.0 and older */
1282af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG_BIT 0
129e9845f09SVincenzo Maffione #define E1000_FLAG_MIT_BIT 1
1309e117734SLeonid Bloch #define E1000_FLAG_MAC_BIT 2
13146f2a9ecSDr. David Alan Gilbert #define E1000_FLAG_TSO_BIT 3
1322af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG (1 << E1000_FLAG_AUTONEG_BIT)
133e9845f09SVincenzo Maffione #define E1000_FLAG_MIT (1 << E1000_FLAG_MIT_BIT)
1349e117734SLeonid Bloch #define E1000_FLAG_MAC (1 << E1000_FLAG_MAC_BIT)
13546f2a9ecSDr. David Alan Gilbert #define E1000_FLAG_TSO (1 << E1000_FLAG_TSO_BIT)
1362af234e6SMichael S. Tsirkin     uint32_t compat_flags;
1373c4053c5SDr. David Alan Gilbert     bool received_tx_tso;
138ff214d42SDr. David Alan Gilbert     bool use_tso_for_migration;
13959354484SDr. David Alan Gilbert     e1000x_txd_props mig_props;
1407c23b892Sbalrog } E1000State;
1417c23b892Sbalrog 
142bc0f0674SLeonid Bloch #define chkflag(x)     (s->compat_flags & E1000_FLAG_##x)
143bc0f0674SLeonid Bloch 
1448597f2e1SGabriel L. Somlo typedef struct E1000BaseClass {
1458597f2e1SGabriel L. Somlo     PCIDeviceClass parent_class;
1468597f2e1SGabriel L. Somlo     uint16_t phy_id2;
1478597f2e1SGabriel L. Somlo } E1000BaseClass;
1488597f2e1SGabriel L. Somlo 
1498597f2e1SGabriel L. Somlo #define TYPE_E1000_BASE "e1000-base"
150567a3c9eSPeter Crosthwaite 
151567a3c9eSPeter Crosthwaite #define E1000(obj) \
1528597f2e1SGabriel L. Somlo     OBJECT_CHECK(E1000State, (obj), TYPE_E1000_BASE)
1538597f2e1SGabriel L. Somlo 
1548597f2e1SGabriel L. Somlo #define E1000_DEVICE_CLASS(klass) \
1558597f2e1SGabriel L. Somlo      OBJECT_CLASS_CHECK(E1000BaseClass, (klass), TYPE_E1000_BASE)
1568597f2e1SGabriel L. Somlo #define E1000_DEVICE_GET_CLASS(obj) \
1578597f2e1SGabriel L. Somlo     OBJECT_GET_CLASS(E1000BaseClass, (obj), TYPE_E1000_BASE)
158567a3c9eSPeter Crosthwaite 
15971aadd3cSJason Wang static void
16071aadd3cSJason Wang e1000_link_up(E1000State *s)
16171aadd3cSJason Wang {
162093454e2SDmitry Fleytman     e1000x_update_regs_on_link_up(s->mac_reg, s->phy_reg);
163093454e2SDmitry Fleytman 
164093454e2SDmitry Fleytman     /* E1000_STATUS_LU is tested by e1000_can_receive() */
165093454e2SDmitry Fleytman     qemu_flush_queued_packets(qemu_get_queue(s->nic));
166093454e2SDmitry Fleytman }
167093454e2SDmitry Fleytman 
168093454e2SDmitry Fleytman static void
169093454e2SDmitry Fleytman e1000_autoneg_done(E1000State *s)
170093454e2SDmitry Fleytman {
171093454e2SDmitry Fleytman     e1000x_update_regs_on_autoneg_done(s->mac_reg, s->phy_reg);
1725df6a185SStefan Hajnoczi 
1735df6a185SStefan Hajnoczi     /* E1000_STATUS_LU is tested by e1000_can_receive() */
1745df6a185SStefan Hajnoczi     qemu_flush_queued_packets(qemu_get_queue(s->nic));
17571aadd3cSJason Wang }
17671aadd3cSJason Wang 
1771195fed9SGabriel L. Somlo static bool
1781195fed9SGabriel L. Somlo have_autoneg(E1000State *s)
1791195fed9SGabriel L. Somlo {
180bc0f0674SLeonid Bloch     return chkflag(AUTONEG) && (s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN);
1811195fed9SGabriel L. Somlo }
1821195fed9SGabriel L. Somlo 
183b9d03e35SJason Wang static void
184b9d03e35SJason Wang set_phy_ctrl(E1000State *s, int index, uint16_t val)
185b9d03e35SJason Wang {
1861195fed9SGabriel L. Somlo     /* bits 0-5 reserved; MII_CR_[RESTART_AUTO_NEG,RESET] are self clearing */
1871195fed9SGabriel L. Somlo     s->phy_reg[PHY_CTRL] = val & ~(0x3f |
1881195fed9SGabriel L. Somlo                                    MII_CR_RESET |
1891195fed9SGabriel L. Somlo                                    MII_CR_RESTART_AUTO_NEG);
1901195fed9SGabriel L. Somlo 
1912af234e6SMichael S. Tsirkin     /*
1922af234e6SMichael S. Tsirkin      * QEMU 1.3 does not support link auto-negotiation emulation, so if we
1932af234e6SMichael S. Tsirkin      * migrate during auto negotiation, after migration the link will be
1942af234e6SMichael S. Tsirkin      * down.
1952af234e6SMichael S. Tsirkin      */
1961195fed9SGabriel L. Somlo     if (have_autoneg(s) && (val & MII_CR_RESTART_AUTO_NEG)) {
197093454e2SDmitry Fleytman         e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer);
198b9d03e35SJason Wang     }
199b9d03e35SJason Wang }
200b9d03e35SJason Wang 
201b9d03e35SJason Wang static void (*phyreg_writeops[])(E1000State *, int, uint16_t) = {
202b9d03e35SJason Wang     [PHY_CTRL] = set_phy_ctrl,
203b9d03e35SJason Wang };
204b9d03e35SJason Wang 
205b9d03e35SJason Wang enum { NPHYWRITEOPS = ARRAY_SIZE(phyreg_writeops) };
206b9d03e35SJason Wang 
2077c23b892Sbalrog enum { PHY_R = 1, PHY_W = 2, PHY_RW = PHY_R | PHY_W };
20888b4e9dbSblueswir1 static const char phy_regcap[0x20] = {
2097c23b892Sbalrog     [PHY_STATUS]      = PHY_R,     [M88E1000_EXT_PHY_SPEC_CTRL] = PHY_RW,
2107c23b892Sbalrog     [PHY_ID1]         = PHY_R,     [M88E1000_PHY_SPEC_CTRL]     = PHY_RW,
2117c23b892Sbalrog     [PHY_CTRL]        = PHY_RW,    [PHY_1000T_CTRL]             = PHY_RW,
2127c23b892Sbalrog     [PHY_LP_ABILITY]  = PHY_R,     [PHY_1000T_STATUS]           = PHY_R,
2137c23b892Sbalrog     [PHY_AUTONEG_ADV] = PHY_RW,    [M88E1000_RX_ERR_CNTR]       = PHY_R,
2146883b591SGabriel L. Somlo     [PHY_ID2]         = PHY_R,     [M88E1000_PHY_SPEC_STATUS]   = PHY_R,
2156883b591SGabriel L. Somlo     [PHY_AUTONEG_EXP] = PHY_R,
2167c23b892Sbalrog };
2177c23b892Sbalrog 
2188597f2e1SGabriel L. Somlo /* PHY_ID2 documented in 8254x_GBe_SDM.pdf, pp. 250 */
219814cd3acSMichael S. Tsirkin static const uint16_t phy_reg_init[] = {
2209616c290SGabriel L. Somlo     [PHY_CTRL]   = MII_CR_SPEED_SELECT_MSB |
2219616c290SGabriel L. Somlo                    MII_CR_FULL_DUPLEX |
2229616c290SGabriel L. Somlo                    MII_CR_AUTO_NEG_EN,
2239616c290SGabriel L. Somlo 
2249616c290SGabriel L. Somlo     [PHY_STATUS] = MII_SR_EXTENDED_CAPS |
2259616c290SGabriel L. Somlo                    MII_SR_LINK_STATUS |   /* link initially up */
2269616c290SGabriel L. Somlo                    MII_SR_AUTONEG_CAPS |
2279616c290SGabriel L. Somlo                    /* MII_SR_AUTONEG_COMPLETE: initially NOT completed */
2289616c290SGabriel L. Somlo                    MII_SR_PREAMBLE_SUPPRESS |
2299616c290SGabriel L. Somlo                    MII_SR_EXTENDED_STATUS |
2309616c290SGabriel L. Somlo                    MII_SR_10T_HD_CAPS |
2319616c290SGabriel L. Somlo                    MII_SR_10T_FD_CAPS |
2329616c290SGabriel L. Somlo                    MII_SR_100X_HD_CAPS |
2339616c290SGabriel L. Somlo                    MII_SR_100X_FD_CAPS,
2349616c290SGabriel L. Somlo 
2359616c290SGabriel L. Somlo     [PHY_ID1] = 0x141,
2369616c290SGabriel L. Somlo     /* [PHY_ID2] configured per DevId, from e1000_reset() */
2379616c290SGabriel L. Somlo     [PHY_AUTONEG_ADV] = 0xde1,
2389616c290SGabriel L. Somlo     [PHY_LP_ABILITY] = 0x1e0,
2399616c290SGabriel L. Somlo     [PHY_1000T_CTRL] = 0x0e00,
2409616c290SGabriel L. Somlo     [PHY_1000T_STATUS] = 0x3c00,
2419616c290SGabriel L. Somlo     [M88E1000_PHY_SPEC_CTRL] = 0x360,
242814cd3acSMichael S. Tsirkin     [M88E1000_PHY_SPEC_STATUS] = 0xac00,
2439616c290SGabriel L. Somlo     [M88E1000_EXT_PHY_SPEC_CTRL] = 0x0d60,
244814cd3acSMichael S. Tsirkin };
245814cd3acSMichael S. Tsirkin 
246814cd3acSMichael S. Tsirkin static const uint32_t mac_reg_init[] = {
247814cd3acSMichael S. Tsirkin     [PBA]     = 0x00100030,
248814cd3acSMichael S. Tsirkin     [LEDCTL]  = 0x602,
249814cd3acSMichael S. Tsirkin     [CTRL]    = E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN0 |
250814cd3acSMichael S. Tsirkin                 E1000_CTRL_SPD_1000 | E1000_CTRL_SLU,
251814cd3acSMichael S. Tsirkin     [STATUS]  = 0x80000000 | E1000_STATUS_GIO_MASTER_ENABLE |
252814cd3acSMichael S. Tsirkin                 E1000_STATUS_ASDV | E1000_STATUS_MTXCKOK |
253814cd3acSMichael S. Tsirkin                 E1000_STATUS_SPEED_1000 | E1000_STATUS_FD |
254814cd3acSMichael S. Tsirkin                 E1000_STATUS_LU,
255814cd3acSMichael S. Tsirkin     [MANC]    = E1000_MANC_EN_MNG2HOST | E1000_MANC_RCV_TCO_EN |
256814cd3acSMichael S. Tsirkin                 E1000_MANC_ARP_EN | E1000_MANC_0298_EN |
257814cd3acSMichael S. Tsirkin                 E1000_MANC_RMCP_EN,
258814cd3acSMichael S. Tsirkin };
259814cd3acSMichael S. Tsirkin 
260e9845f09SVincenzo Maffione /* Helper function, *curr == 0 means the value is not set */
261e9845f09SVincenzo Maffione static inline void
262e9845f09SVincenzo Maffione mit_update_delay(uint32_t *curr, uint32_t value)
263e9845f09SVincenzo Maffione {
264e9845f09SVincenzo Maffione     if (value && (*curr == 0 || value < *curr)) {
265e9845f09SVincenzo Maffione         *curr = value;
266e9845f09SVincenzo Maffione     }
267e9845f09SVincenzo Maffione }
268e9845f09SVincenzo Maffione 
2697c23b892Sbalrog static void
2707c23b892Sbalrog set_interrupt_cause(E1000State *s, int index, uint32_t val)
2717c23b892Sbalrog {
272b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
273e9845f09SVincenzo Maffione     uint32_t pending_ints;
274e9845f09SVincenzo Maffione     uint32_t mit_delay;
275b08340d5SAndreas Färber 
2767c23b892Sbalrog     s->mac_reg[ICR] = val;
277a52a8841SMichael S. Tsirkin 
278a52a8841SMichael S. Tsirkin     /*
279a52a8841SMichael S. Tsirkin      * Make sure ICR and ICS registers have the same value.
280a52a8841SMichael S. Tsirkin      * The spec says that the ICS register is write-only.  However in practice,
281a52a8841SMichael S. Tsirkin      * on real hardware ICS is readable, and for reads it has the same value as
282a52a8841SMichael S. Tsirkin      * ICR (except that ICS does not have the clear on read behaviour of ICR).
283a52a8841SMichael S. Tsirkin      *
284a52a8841SMichael S. Tsirkin      * The VxWorks PRO/1000 driver uses this behaviour.
285a52a8841SMichael S. Tsirkin      */
286b1332393SBill Paul     s->mac_reg[ICS] = val;
287a52a8841SMichael S. Tsirkin 
288e9845f09SVincenzo Maffione     pending_ints = (s->mac_reg[IMS] & s->mac_reg[ICR]);
289e9845f09SVincenzo Maffione     if (!s->mit_irq_level && pending_ints) {
290e9845f09SVincenzo Maffione         /*
291e9845f09SVincenzo Maffione          * Here we detect a potential raising edge. We postpone raising the
292e9845f09SVincenzo Maffione          * interrupt line if we are inside the mitigation delay window
293e9845f09SVincenzo Maffione          * (s->mit_timer_on == 1).
294e9845f09SVincenzo Maffione          * We provide a partial implementation of interrupt mitigation,
295e9845f09SVincenzo Maffione          * emulating only RADV, TADV and ITR (lower 16 bits, 1024ns units for
296e9845f09SVincenzo Maffione          * RADV and TADV, 256ns units for ITR). RDTR is only used to enable
297e9845f09SVincenzo Maffione          * RADV; relative timers based on TIDV and RDTR are not implemented.
298e9845f09SVincenzo Maffione          */
299e9845f09SVincenzo Maffione         if (s->mit_timer_on) {
300e9845f09SVincenzo Maffione             return;
301e9845f09SVincenzo Maffione         }
302bc0f0674SLeonid Bloch         if (chkflag(MIT)) {
303e9845f09SVincenzo Maffione             /* Compute the next mitigation delay according to pending
304e9845f09SVincenzo Maffione              * interrupts and the current values of RADV (provided
305e9845f09SVincenzo Maffione              * RDTR!=0), TADV and ITR.
306e9845f09SVincenzo Maffione              * Then rearm the timer.
307e9845f09SVincenzo Maffione              */
308e9845f09SVincenzo Maffione             mit_delay = 0;
309e9845f09SVincenzo Maffione             if (s->mit_ide &&
310e9845f09SVincenzo Maffione                     (pending_ints & (E1000_ICR_TXQE | E1000_ICR_TXDW))) {
311e9845f09SVincenzo Maffione                 mit_update_delay(&mit_delay, s->mac_reg[TADV] * 4);
312e9845f09SVincenzo Maffione             }
313e9845f09SVincenzo Maffione             if (s->mac_reg[RDTR] && (pending_ints & E1000_ICS_RXT0)) {
314e9845f09SVincenzo Maffione                 mit_update_delay(&mit_delay, s->mac_reg[RADV] * 4);
315e9845f09SVincenzo Maffione             }
316e9845f09SVincenzo Maffione             mit_update_delay(&mit_delay, s->mac_reg[ITR]);
317e9845f09SVincenzo Maffione 
31874004e8cSSameeh Jubran             /*
31974004e8cSSameeh Jubran              * According to e1000 SPEC, the Ethernet controller guarantees
32074004e8cSSameeh Jubran              * a maximum observable interrupt rate of 7813 interrupts/sec.
32174004e8cSSameeh Jubran              * Thus if mit_delay < 500 then the delay should be set to the
32274004e8cSSameeh Jubran              * minimum delay possible which is 500.
32374004e8cSSameeh Jubran              */
32474004e8cSSameeh Jubran             mit_delay = (mit_delay < 500) ? 500 : mit_delay;
32574004e8cSSameeh Jubran 
326e9845f09SVincenzo Maffione             s->mit_timer_on = 1;
327e9845f09SVincenzo Maffione             timer_mod(s->mit_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
328e9845f09SVincenzo Maffione                       mit_delay * 256);
329e9845f09SVincenzo Maffione             s->mit_ide = 0;
330e9845f09SVincenzo Maffione         }
331e9845f09SVincenzo Maffione     }
332e9845f09SVincenzo Maffione 
333e9845f09SVincenzo Maffione     s->mit_irq_level = (pending_ints != 0);
3349e64f8a3SMarcel Apfelbaum     pci_set_irq(d, s->mit_irq_level);
335e9845f09SVincenzo Maffione }
336e9845f09SVincenzo Maffione 
337e9845f09SVincenzo Maffione static void
338e9845f09SVincenzo Maffione e1000_mit_timer(void *opaque)
339e9845f09SVincenzo Maffione {
340e9845f09SVincenzo Maffione     E1000State *s = opaque;
341e9845f09SVincenzo Maffione 
342e9845f09SVincenzo Maffione     s->mit_timer_on = 0;
343e9845f09SVincenzo Maffione     /* Call set_interrupt_cause to update the irq level (if necessary). */
344e9845f09SVincenzo Maffione     set_interrupt_cause(s, 0, s->mac_reg[ICR]);
3457c23b892Sbalrog }
3467c23b892Sbalrog 
3477c23b892Sbalrog static void
3487c23b892Sbalrog set_ics(E1000State *s, int index, uint32_t val)
3497c23b892Sbalrog {
3507c23b892Sbalrog     DBGOUT(INTERRUPT, "set_ics %x, ICR %x, IMR %x\n", val, s->mac_reg[ICR],
3517c23b892Sbalrog         s->mac_reg[IMS]);
3527c23b892Sbalrog     set_interrupt_cause(s, 0, val | s->mac_reg[ICR]);
3537c23b892Sbalrog }
3547c23b892Sbalrog 
355d52aec95SGabriel L. Somlo static void
356d52aec95SGabriel L. Somlo e1000_autoneg_timer(void *opaque)
357d52aec95SGabriel L. Somlo {
358d52aec95SGabriel L. Somlo     E1000State *s = opaque;
359d52aec95SGabriel L. Somlo     if (!qemu_get_queue(s->nic)->link_down) {
360093454e2SDmitry Fleytman         e1000_autoneg_done(s);
361d52aec95SGabriel L. Somlo         set_ics(s, 0, E1000_ICS_LSC); /* signal link status change to guest */
362d52aec95SGabriel L. Somlo     }
363d52aec95SGabriel L. Somlo }
364d52aec95SGabriel L. Somlo 
365814cd3acSMichael S. Tsirkin static void e1000_reset(void *opaque)
366814cd3acSMichael S. Tsirkin {
367814cd3acSMichael S. Tsirkin     E1000State *d = opaque;
3688597f2e1SGabriel L. Somlo     E1000BaseClass *edc = E1000_DEVICE_GET_CLASS(d);
369372254c6SGabriel L. Somlo     uint8_t *macaddr = d->conf.macaddr.a;
370814cd3acSMichael S. Tsirkin 
371bc72ad67SAlex Bligh     timer_del(d->autoneg_timer);
372e9845f09SVincenzo Maffione     timer_del(d->mit_timer);
373157628d0Syuchenlin     timer_del(d->flush_queue_timer);
374e9845f09SVincenzo Maffione     d->mit_timer_on = 0;
375e9845f09SVincenzo Maffione     d->mit_irq_level = 0;
376e9845f09SVincenzo Maffione     d->mit_ide = 0;
377814cd3acSMichael S. Tsirkin     memset(d->phy_reg, 0, sizeof d->phy_reg);
378814cd3acSMichael S. Tsirkin     memmove(d->phy_reg, phy_reg_init, sizeof phy_reg_init);
3798597f2e1SGabriel L. Somlo     d->phy_reg[PHY_ID2] = edc->phy_id2;
380814cd3acSMichael S. Tsirkin     memset(d->mac_reg, 0, sizeof d->mac_reg);
381814cd3acSMichael S. Tsirkin     memmove(d->mac_reg, mac_reg_init, sizeof mac_reg_init);
382814cd3acSMichael S. Tsirkin     d->rxbuf_min_shift = 1;
383814cd3acSMichael S. Tsirkin     memset(&d->tx, 0, sizeof d->tx);
384814cd3acSMichael S. Tsirkin 
385b356f76dSJason Wang     if (qemu_get_queue(d->nic)->link_down) {
386093454e2SDmitry Fleytman         e1000x_update_regs_on_link_down(d->mac_reg, d->phy_reg);
387814cd3acSMichael S. Tsirkin     }
388372254c6SGabriel L. Somlo 
389093454e2SDmitry Fleytman     e1000x_reset_mac_addr(d->nic, d->mac_reg, macaddr);
390814cd3acSMichael S. Tsirkin }
391814cd3acSMichael S. Tsirkin 
3927c23b892Sbalrog static void
393cab3c825SKevin Wolf set_ctrl(E1000State *s, int index, uint32_t val)
394cab3c825SKevin Wolf {
395cab3c825SKevin Wolf     /* RST is self clearing */
396cab3c825SKevin Wolf     s->mac_reg[CTRL] = val & ~E1000_CTRL_RST;
397cab3c825SKevin Wolf }
398cab3c825SKevin Wolf 
399cab3c825SKevin Wolf static void
400157628d0Syuchenlin e1000_flush_queue_timer(void *opaque)
401157628d0Syuchenlin {
402157628d0Syuchenlin     E1000State *s = opaque;
403157628d0Syuchenlin 
404157628d0Syuchenlin     qemu_flush_queued_packets(qemu_get_queue(s->nic));
405157628d0Syuchenlin }
406157628d0Syuchenlin 
407157628d0Syuchenlin static void
4087c23b892Sbalrog set_rx_control(E1000State *s, int index, uint32_t val)
4097c23b892Sbalrog {
4107c23b892Sbalrog     s->mac_reg[RCTL] = val;
411093454e2SDmitry Fleytman     s->rxbuf_size = e1000x_rxbufsize(val);
4127c23b892Sbalrog     s->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1;
4137c23b892Sbalrog     DBGOUT(RX, "RCTL: %d, mac_reg[RCTL] = 0x%x\n", s->mac_reg[RDT],
4147c23b892Sbalrog            s->mac_reg[RCTL]);
415157628d0Syuchenlin     timer_mod(s->flush_queue_timer,
416157628d0Syuchenlin               qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1000);
4177c23b892Sbalrog }
4187c23b892Sbalrog 
4197c23b892Sbalrog static void
4207c23b892Sbalrog set_mdic(E1000State *s, int index, uint32_t val)
4217c23b892Sbalrog {
4227c23b892Sbalrog     uint32_t data = val & E1000_MDIC_DATA_MASK;
4237c23b892Sbalrog     uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT);
4247c23b892Sbalrog 
4257c23b892Sbalrog     if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) // phy #
4267c23b892Sbalrog         val = s->mac_reg[MDIC] | E1000_MDIC_ERROR;
4277c23b892Sbalrog     else if (val & E1000_MDIC_OP_READ) {
4287c23b892Sbalrog         DBGOUT(MDIC, "MDIC read reg 0x%x\n", addr);
4297c23b892Sbalrog         if (!(phy_regcap[addr] & PHY_R)) {
4307c23b892Sbalrog             DBGOUT(MDIC, "MDIC read reg %x unhandled\n", addr);
4317c23b892Sbalrog             val |= E1000_MDIC_ERROR;
4327c23b892Sbalrog         } else
4337c23b892Sbalrog             val = (val ^ data) | s->phy_reg[addr];
4347c23b892Sbalrog     } else if (val & E1000_MDIC_OP_WRITE) {
4357c23b892Sbalrog         DBGOUT(MDIC, "MDIC write reg 0x%x, value 0x%x\n", addr, data);
4367c23b892Sbalrog         if (!(phy_regcap[addr] & PHY_W)) {
4377c23b892Sbalrog             DBGOUT(MDIC, "MDIC write reg %x unhandled\n", addr);
4387c23b892Sbalrog             val |= E1000_MDIC_ERROR;
439b9d03e35SJason Wang         } else {
440b9d03e35SJason Wang             if (addr < NPHYWRITEOPS && phyreg_writeops[addr]) {
441b9d03e35SJason Wang                 phyreg_writeops[addr](s, index, data);
4421195fed9SGabriel L. Somlo             } else {
4437c23b892Sbalrog                 s->phy_reg[addr] = data;
4447c23b892Sbalrog             }
445b9d03e35SJason Wang         }
4461195fed9SGabriel L. Somlo     }
4477c23b892Sbalrog     s->mac_reg[MDIC] = val | E1000_MDIC_READY;
44817fbbb0bSJason Wang 
44917fbbb0bSJason Wang     if (val & E1000_MDIC_INT_EN) {
4507c23b892Sbalrog         set_ics(s, 0, E1000_ICR_MDAC);
4517c23b892Sbalrog     }
45217fbbb0bSJason Wang }
4537c23b892Sbalrog 
4547c23b892Sbalrog static uint32_t
4557c23b892Sbalrog get_eecd(E1000State *s, int index)
4567c23b892Sbalrog {
4577c23b892Sbalrog     uint32_t ret = E1000_EECD_PRES|E1000_EECD_GNT | s->eecd_state.old_eecd;
4587c23b892Sbalrog 
4597c23b892Sbalrog     DBGOUT(EEPROM, "reading eeprom bit %d (reading %d)\n",
4607c23b892Sbalrog            s->eecd_state.bitnum_out, s->eecd_state.reading);
4617c23b892Sbalrog     if (!s->eecd_state.reading ||
4627c23b892Sbalrog         ((s->eeprom_data[(s->eecd_state.bitnum_out >> 4) & 0x3f] >>
4637c23b892Sbalrog           ((s->eecd_state.bitnum_out & 0xf) ^ 0xf))) & 1)
4647c23b892Sbalrog         ret |= E1000_EECD_DO;
4657c23b892Sbalrog     return ret;
4667c23b892Sbalrog }
4677c23b892Sbalrog 
4687c23b892Sbalrog static void
4697c23b892Sbalrog set_eecd(E1000State *s, int index, uint32_t val)
4707c23b892Sbalrog {
4717c23b892Sbalrog     uint32_t oldval = s->eecd_state.old_eecd;
4727c23b892Sbalrog 
4737c23b892Sbalrog     s->eecd_state.old_eecd = val & (E1000_EECD_SK | E1000_EECD_CS |
4747c23b892Sbalrog             E1000_EECD_DI|E1000_EECD_FWE_MASK|E1000_EECD_REQ);
47520f3e863SLeonid Bloch     if (!(E1000_EECD_CS & val)) {            /* CS inactive; nothing to do */
4769651ac55SIzumi Tsutsui         return;
47720f3e863SLeonid Bloch     }
47820f3e863SLeonid Bloch     if (E1000_EECD_CS & (val ^ oldval)) {    /* CS rise edge; reset state */
4799651ac55SIzumi Tsutsui         s->eecd_state.val_in = 0;
4809651ac55SIzumi Tsutsui         s->eecd_state.bitnum_in = 0;
4819651ac55SIzumi Tsutsui         s->eecd_state.bitnum_out = 0;
4829651ac55SIzumi Tsutsui         s->eecd_state.reading = 0;
4839651ac55SIzumi Tsutsui     }
48420f3e863SLeonid Bloch     if (!(E1000_EECD_SK & (val ^ oldval))) {    /* no clock edge */
4857c23b892Sbalrog         return;
48620f3e863SLeonid Bloch     }
48720f3e863SLeonid Bloch     if (!(E1000_EECD_SK & val)) {               /* falling edge */
4887c23b892Sbalrog         s->eecd_state.bitnum_out++;
4897c23b892Sbalrog         return;
4907c23b892Sbalrog     }
4917c23b892Sbalrog     s->eecd_state.val_in <<= 1;
4927c23b892Sbalrog     if (val & E1000_EECD_DI)
4937c23b892Sbalrog         s->eecd_state.val_in |= 1;
4947c23b892Sbalrog     if (++s->eecd_state.bitnum_in == 9 && !s->eecd_state.reading) {
4957c23b892Sbalrog         s->eecd_state.bitnum_out = ((s->eecd_state.val_in & 0x3f)<<4)-1;
4967c23b892Sbalrog         s->eecd_state.reading = (((s->eecd_state.val_in >> 6) & 7) ==
4977c23b892Sbalrog             EEPROM_READ_OPCODE_MICROWIRE);
4987c23b892Sbalrog     }
4997c23b892Sbalrog     DBGOUT(EEPROM, "eeprom bitnum in %d out %d, reading %d\n",
5007c23b892Sbalrog            s->eecd_state.bitnum_in, s->eecd_state.bitnum_out,
5017c23b892Sbalrog            s->eecd_state.reading);
5027c23b892Sbalrog }
5037c23b892Sbalrog 
5047c23b892Sbalrog static uint32_t
5057c23b892Sbalrog flash_eerd_read(E1000State *s, int x)
5067c23b892Sbalrog {
5077c23b892Sbalrog     unsigned int index, r = s->mac_reg[EERD] & ~E1000_EEPROM_RW_REG_START;
5087c23b892Sbalrog 
509b1332393SBill Paul     if ((s->mac_reg[EERD] & E1000_EEPROM_RW_REG_START) == 0)
510b1332393SBill Paul         return (s->mac_reg[EERD]);
511b1332393SBill Paul 
5127c23b892Sbalrog     if ((index = r >> E1000_EEPROM_RW_ADDR_SHIFT) > EEPROM_CHECKSUM_REG)
513b1332393SBill Paul         return (E1000_EEPROM_RW_REG_DONE | r);
514b1332393SBill Paul 
515b1332393SBill Paul     return ((s->eeprom_data[index] << E1000_EEPROM_RW_REG_DATA) |
516b1332393SBill Paul            E1000_EEPROM_RW_REG_DONE | r);
5177c23b892Sbalrog }
5187c23b892Sbalrog 
5197c23b892Sbalrog static void
5207c23b892Sbalrog putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t css, uint32_t cse)
5217c23b892Sbalrog {
522c6a6a5e3Saliguori     uint32_t sum;
523c6a6a5e3Saliguori 
5247c23b892Sbalrog     if (cse && cse < n)
5257c23b892Sbalrog         n = cse + 1;
526c6a6a5e3Saliguori     if (sloc < n-1) {
527c6a6a5e3Saliguori         sum = net_checksum_add(n-css, data+css);
5280dacea92SEd Swierk         stw_be_p(data + sloc, net_checksum_finish_nozero(sum));
529c6a6a5e3Saliguori     }
5307c23b892Sbalrog }
5317c23b892Sbalrog 
5321f67f92cSLeonid Bloch static inline void
5333b274301SLeonid Bloch inc_tx_bcast_or_mcast_count(E1000State *s, const unsigned char *arr)
5343b274301SLeonid Bloch {
5353b274301SLeonid Bloch     if (!memcmp(arr, bcast, sizeof bcast)) {
536093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, BPTC);
5373b274301SLeonid Bloch     } else if (arr[0] & 1) {
538093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, MPTC);
5393b274301SLeonid Bloch     }
5403b274301SLeonid Bloch }
5413b274301SLeonid Bloch 
54245e93764SLeonid Bloch static void
54393e37d76SJason Wang e1000_send_packet(E1000State *s, const uint8_t *buf, int size)
54493e37d76SJason Wang {
5453b274301SLeonid Bloch     static const int PTCregs[6] = { PTC64, PTC127, PTC255, PTC511,
5463b274301SLeonid Bloch                                     PTC1023, PTC1522 };
5473b274301SLeonid Bloch 
548b356f76dSJason Wang     NetClientState *nc = qemu_get_queue(s->nic);
54993e37d76SJason Wang     if (s->phy_reg[PHY_CTRL] & MII_CR_LOOPBACK) {
550b356f76dSJason Wang         nc->info->receive(nc, buf, size);
55193e37d76SJason Wang     } else {
552b356f76dSJason Wang         qemu_send_packet(nc, buf, size);
55393e37d76SJason Wang     }
5543b274301SLeonid Bloch     inc_tx_bcast_or_mcast_count(s, buf);
555093454e2SDmitry Fleytman     e1000x_increase_size_stats(s->mac_reg, PTCregs, size);
55693e37d76SJason Wang }
55793e37d76SJason Wang 
55893e37d76SJason Wang static void
5597c23b892Sbalrog xmit_seg(E1000State *s)
5607c23b892Sbalrog {
56114e60aaeSPeter Maydell     uint16_t len;
56245e93764SLeonid Bloch     unsigned int frames = s->tx.tso_frames, css, sofar;
5637c23b892Sbalrog     struct e1000_tx *tp = &s->tx;
564d62644b4SEd Swierk via Qemu-devel     struct e1000x_txd_props *props = tp->cptse ? &tp->tso_props : &tp->props;
5657c23b892Sbalrog 
566d62644b4SEd Swierk via Qemu-devel     if (tp->cptse) {
567d62644b4SEd Swierk via Qemu-devel         css = props->ipcss;
5687c23b892Sbalrog         DBGOUT(TXSUM, "frames %d size %d ipcss %d\n",
5697c23b892Sbalrog                frames, tp->size, css);
570d62644b4SEd Swierk via Qemu-devel         if (props->ip) {    /* IPv4 */
571d8ee2591SPeter Maydell             stw_be_p(tp->data+css+2, tp->size - css);
572d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4,
57314e60aaeSPeter Maydell                      lduw_be_p(tp->data + css + 4) + frames);
57420f3e863SLeonid Bloch         } else {         /* IPv6 */
575d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4, tp->size - css);
57620f3e863SLeonid Bloch         }
577d62644b4SEd Swierk via Qemu-devel         css = props->tucss;
5787c23b892Sbalrog         len = tp->size - css;
579d62644b4SEd Swierk via Qemu-devel         DBGOUT(TXSUM, "tcp %d tucss %d len %d\n", props->tcp, css, len);
580d62644b4SEd Swierk via Qemu-devel         if (props->tcp) {
581d62644b4SEd Swierk via Qemu-devel             sofar = frames * props->mss;
5826bd194abSPeter Maydell             stl_be_p(tp->data+css+4, ldl_be_p(tp->data+css+4)+sofar); /* seq */
583d62644b4SEd Swierk via Qemu-devel             if (props->paylen - sofar > props->mss) {
58420f3e863SLeonid Bloch                 tp->data[css + 13] &= ~9;    /* PSH, FIN */
5853b274301SLeonid Bloch             } else if (frames) {
586093454e2SDmitry Fleytman                 e1000x_inc_reg_if_not_full(s->mac_reg, TSCTC);
5873b274301SLeonid Bloch             }
588d62644b4SEd Swierk via Qemu-devel         } else {    /* UDP */
589d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4, len);
590d62644b4SEd Swierk via Qemu-devel         }
5917d08c73eSEd Swierk via Qemu-devel         if (tp->sum_needed & E1000_TXD_POPTS_TXSM) {
592e685b4ebSAlex Williamson             unsigned int phsum;
5937c23b892Sbalrog             // add pseudo-header length before checksum calculation
594d62644b4SEd Swierk via Qemu-devel             void *sp = tp->data + props->tucso;
59514e60aaeSPeter Maydell 
59614e60aaeSPeter Maydell             phsum = lduw_be_p(sp) + len;
597e685b4ebSAlex Williamson             phsum = (phsum >> 16) + (phsum & 0xffff);
598d8ee2591SPeter Maydell             stw_be_p(sp, phsum);
5997c23b892Sbalrog         }
6007c23b892Sbalrog         tp->tso_frames++;
6017c23b892Sbalrog     }
6027c23b892Sbalrog 
6037d08c73eSEd Swierk via Qemu-devel     if (tp->sum_needed & E1000_TXD_POPTS_TXSM) {
604d62644b4SEd Swierk via Qemu-devel         putsum(tp->data, tp->size, props->tucso, props->tucss, props->tucse);
605093454e2SDmitry Fleytman     }
6067d08c73eSEd Swierk via Qemu-devel     if (tp->sum_needed & E1000_TXD_POPTS_IXSM) {
607d62644b4SEd Swierk via Qemu-devel         putsum(tp->data, tp->size, props->ipcso, props->ipcss, props->ipcse);
608093454e2SDmitry Fleytman     }
6098f2e8d1fSaliguori     if (tp->vlan_needed) {
610b10fec9bSStefan Weil         memmove(tp->vlan, tp->data, 4);
611b10fec9bSStefan Weil         memmove(tp->data, tp->data + 4, 8);
6128f2e8d1fSaliguori         memcpy(tp->data + 8, tp->vlan_header, 4);
61393e37d76SJason Wang         e1000_send_packet(s, tp->vlan, tp->size + 4);
61420f3e863SLeonid Bloch     } else {
61593e37d76SJason Wang         e1000_send_packet(s, tp->data, tp->size);
61620f3e863SLeonid Bloch     }
61720f3e863SLeonid Bloch 
618093454e2SDmitry Fleytman     e1000x_inc_reg_if_not_full(s->mac_reg, TPT);
619093454e2SDmitry Fleytman     e1000x_grow_8reg_if_not_full(s->mac_reg, TOTL, s->tx.size);
6201f67f92cSLeonid Bloch     s->mac_reg[GPTC] = s->mac_reg[TPT];
6213b274301SLeonid Bloch     s->mac_reg[GOTCL] = s->mac_reg[TOTL];
6223b274301SLeonid Bloch     s->mac_reg[GOTCH] = s->mac_reg[TOTH];
6237c23b892Sbalrog }
6247c23b892Sbalrog 
6257c23b892Sbalrog static void
6267c23b892Sbalrog process_tx_desc(E1000State *s, struct e1000_tx_desc *dp)
6277c23b892Sbalrog {
628b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
6297c23b892Sbalrog     uint32_t txd_lower = le32_to_cpu(dp->lower.data);
6307c23b892Sbalrog     uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D);
631093454e2SDmitry Fleytman     unsigned int split_size = txd_lower & 0xffff, bytes, sz;
632a0ae17a6SAndrew Jones     unsigned int msh = 0xfffff;
6337c23b892Sbalrog     uint64_t addr;
6347c23b892Sbalrog     struct e1000_context_desc *xp = (struct e1000_context_desc *)dp;
6357c23b892Sbalrog     struct e1000_tx *tp = &s->tx;
6367c23b892Sbalrog 
637e9845f09SVincenzo Maffione     s->mit_ide |= (txd_lower & E1000_TXD_CMD_IDE);
63820f3e863SLeonid Bloch     if (dtype == E1000_TXD_CMD_DEXT) {    /* context descriptor */
639d62644b4SEd Swierk via Qemu-devel         if (le32_to_cpu(xp->cmd_and_length) & E1000_TXD_CMD_TSE) {
640d62644b4SEd Swierk via Qemu-devel             e1000x_read_tx_ctx_descr(xp, &tp->tso_props);
641ff214d42SDr. David Alan Gilbert             s->use_tso_for_migration = 1;
6427c23b892Sbalrog             tp->tso_frames = 0;
643d62644b4SEd Swierk via Qemu-devel         } else {
644d62644b4SEd Swierk via Qemu-devel             e1000x_read_tx_ctx_descr(xp, &tp->props);
645ff214d42SDr. David Alan Gilbert             s->use_tso_for_migration = 0;
6467c23b892Sbalrog         }
6477c23b892Sbalrog         return;
6481b0009dbSbalrog     } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) {
6491b0009dbSbalrog         // data descriptor
650735e77ecSStefan Hajnoczi         if (tp->size == 0) {
6517d08c73eSEd Swierk via Qemu-devel             tp->sum_needed = le32_to_cpu(dp->upper.data) >> 8;
652735e77ecSStefan Hajnoczi         }
6537d08c73eSEd Swierk via Qemu-devel         tp->cptse = (txd_lower & E1000_TXD_CMD_TSE) ? 1 : 0;
65443ad7e3eSJes Sorensen     } else {
6551b0009dbSbalrog         // legacy descriptor
6567d08c73eSEd Swierk via Qemu-devel         tp->cptse = 0;
65743ad7e3eSJes Sorensen     }
6587c23b892Sbalrog 
659093454e2SDmitry Fleytman     if (e1000x_vlan_enabled(s->mac_reg) &&
660093454e2SDmitry Fleytman         e1000x_is_vlan_txd(txd_lower) &&
6617d08c73eSEd Swierk via Qemu-devel         (tp->cptse || txd_lower & E1000_TXD_CMD_EOP)) {
6628f2e8d1fSaliguori         tp->vlan_needed = 1;
663d8ee2591SPeter Maydell         stw_be_p(tp->vlan_header,
6644e60a250SShannon Zhao                       le16_to_cpu(s->mac_reg[VET]));
665d8ee2591SPeter Maydell         stw_be_p(tp->vlan_header + 2,
6668f2e8d1fSaliguori                       le16_to_cpu(dp->upper.fields.special));
6678f2e8d1fSaliguori     }
6688f2e8d1fSaliguori 
6697c23b892Sbalrog     addr = le64_to_cpu(dp->buffer_addr);
670d62644b4SEd Swierk via Qemu-devel     if (tp->cptse) {
671d62644b4SEd Swierk via Qemu-devel         msh = tp->tso_props.hdr_len + tp->tso_props.mss;
6727c23b892Sbalrog         do {
6737c23b892Sbalrog             bytes = split_size;
6747c23b892Sbalrog             if (tp->size + bytes > msh)
6757c23b892Sbalrog                 bytes = msh - tp->size;
67665f82df0SAnthony Liguori 
67765f82df0SAnthony Liguori             bytes = MIN(sizeof(tp->data) - tp->size, bytes);
678b08340d5SAndreas Färber             pci_dma_read(d, addr, tp->data + tp->size, bytes);
679a0ae17a6SAndrew Jones             sz = tp->size + bytes;
680d62644b4SEd Swierk via Qemu-devel             if (sz >= tp->tso_props.hdr_len
681d62644b4SEd Swierk via Qemu-devel                 && tp->size < tp->tso_props.hdr_len) {
682d62644b4SEd Swierk via Qemu-devel                 memmove(tp->header, tp->data, tp->tso_props.hdr_len);
683a0ae17a6SAndrew Jones             }
6847c23b892Sbalrog             tp->size = sz;
6857c23b892Sbalrog             addr += bytes;
6867c23b892Sbalrog             if (sz == msh) {
6877c23b892Sbalrog                 xmit_seg(s);
688d62644b4SEd Swierk via Qemu-devel                 memmove(tp->data, tp->header, tp->tso_props.hdr_len);
689d62644b4SEd Swierk via Qemu-devel                 tp->size = tp->tso_props.hdr_len;
6907c23b892Sbalrog             }
691b947ac2bSP J P             split_size -= bytes;
692b947ac2bSP J P         } while (bytes && split_size);
6931b0009dbSbalrog     } else {
69465f82df0SAnthony Liguori         split_size = MIN(sizeof(tp->data) - tp->size, split_size);
695b08340d5SAndreas Färber         pci_dma_read(d, addr, tp->data + tp->size, split_size);
6961b0009dbSbalrog         tp->size += split_size;
6971b0009dbSbalrog     }
6987c23b892Sbalrog 
6997c23b892Sbalrog     if (!(txd_lower & E1000_TXD_CMD_EOP))
7007c23b892Sbalrog         return;
701d62644b4SEd Swierk via Qemu-devel     if (!(tp->cptse && tp->size < tp->tso_props.hdr_len)) {
7027c23b892Sbalrog         xmit_seg(s);
703a0ae17a6SAndrew Jones     }
7047c23b892Sbalrog     tp->tso_frames = 0;
7057d08c73eSEd Swierk via Qemu-devel     tp->sum_needed = 0;
7068f2e8d1fSaliguori     tp->vlan_needed = 0;
7077c23b892Sbalrog     tp->size = 0;
7087d08c73eSEd Swierk via Qemu-devel     tp->cptse = 0;
7097c23b892Sbalrog }
7107c23b892Sbalrog 
7117c23b892Sbalrog static uint32_t
71262ecbd35SEduard - Gabriel Munteanu txdesc_writeback(E1000State *s, dma_addr_t base, struct e1000_tx_desc *dp)
7137c23b892Sbalrog {
714b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
7157c23b892Sbalrog     uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data);
7167c23b892Sbalrog 
7177c23b892Sbalrog     if (!(txd_lower & (E1000_TXD_CMD_RS|E1000_TXD_CMD_RPS)))
7187c23b892Sbalrog         return 0;
7197c23b892Sbalrog     txd_upper = (le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD) &
7207c23b892Sbalrog                 ~(E1000_TXD_STAT_EC | E1000_TXD_STAT_LC | E1000_TXD_STAT_TU);
7217c23b892Sbalrog     dp->upper.data = cpu_to_le32(txd_upper);
722b08340d5SAndreas Färber     pci_dma_write(d, base + ((char *)&dp->upper - (char *)dp),
72300c3a05bSDavid Gibson                   &dp->upper, sizeof(dp->upper));
7247c23b892Sbalrog     return E1000_ICR_TXDW;
7257c23b892Sbalrog }
7267c23b892Sbalrog 
727d17161f6SKevin Wolf static uint64_t tx_desc_base(E1000State *s)
728d17161f6SKevin Wolf {
729d17161f6SKevin Wolf     uint64_t bah = s->mac_reg[TDBAH];
730d17161f6SKevin Wolf     uint64_t bal = s->mac_reg[TDBAL] & ~0xf;
731d17161f6SKevin Wolf 
732d17161f6SKevin Wolf     return (bah << 32) + bal;
733d17161f6SKevin Wolf }
734d17161f6SKevin Wolf 
7357c23b892Sbalrog static void
7367c23b892Sbalrog start_xmit(E1000State *s)
7377c23b892Sbalrog {
738b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
73962ecbd35SEduard - Gabriel Munteanu     dma_addr_t base;
7407c23b892Sbalrog     struct e1000_tx_desc desc;
7417c23b892Sbalrog     uint32_t tdh_start = s->mac_reg[TDH], cause = E1000_ICS_TXQE;
7427c23b892Sbalrog 
7437c23b892Sbalrog     if (!(s->mac_reg[TCTL] & E1000_TCTL_EN)) {
7447c23b892Sbalrog         DBGOUT(TX, "tx disabled\n");
7457c23b892Sbalrog         return;
7467c23b892Sbalrog     }
7477c23b892Sbalrog 
7487c23b892Sbalrog     while (s->mac_reg[TDH] != s->mac_reg[TDT]) {
749d17161f6SKevin Wolf         base = tx_desc_base(s) +
7507c23b892Sbalrog                sizeof(struct e1000_tx_desc) * s->mac_reg[TDH];
751b08340d5SAndreas Färber         pci_dma_read(d, base, &desc, sizeof(desc));
7527c23b892Sbalrog 
7537c23b892Sbalrog         DBGOUT(TX, "index %d: %p : %x %x\n", s->mac_reg[TDH],
7546106075bSths                (void *)(intptr_t)desc.buffer_addr, desc.lower.data,
7557c23b892Sbalrog                desc.upper.data);
7567c23b892Sbalrog 
7577c23b892Sbalrog         process_tx_desc(s, &desc);
75862ecbd35SEduard - Gabriel Munteanu         cause |= txdesc_writeback(s, base, &desc);
7597c23b892Sbalrog 
7607c23b892Sbalrog         if (++s->mac_reg[TDH] * sizeof(desc) >= s->mac_reg[TDLEN])
7617c23b892Sbalrog             s->mac_reg[TDH] = 0;
7627c23b892Sbalrog         /*
7637c23b892Sbalrog          * the following could happen only if guest sw assigns
7647c23b892Sbalrog          * bogus values to TDT/TDLEN.
7657c23b892Sbalrog          * there's nothing too intelligent we could do about this.
7667c23b892Sbalrog          */
767dd793a74SLaszlo Ersek         if (s->mac_reg[TDH] == tdh_start ||
768dd793a74SLaszlo Ersek             tdh_start >= s->mac_reg[TDLEN] / sizeof(desc)) {
7697c23b892Sbalrog             DBGOUT(TXERR, "TDH wraparound @%x, TDT %x, TDLEN %x\n",
7707c23b892Sbalrog                    tdh_start, s->mac_reg[TDT], s->mac_reg[TDLEN]);
7717c23b892Sbalrog             break;
7727c23b892Sbalrog         }
7737c23b892Sbalrog     }
7747c23b892Sbalrog     set_ics(s, 0, cause);
7757c23b892Sbalrog }
7767c23b892Sbalrog 
7777c23b892Sbalrog static int
7787c23b892Sbalrog receive_filter(E1000State *s, const uint8_t *buf, int size)
7797c23b892Sbalrog {
780093454e2SDmitry Fleytman     uint32_t rctl = s->mac_reg[RCTL];
7814aeea330SLeonid Bloch     int isbcast = !memcmp(buf, bcast, sizeof bcast), ismcast = (buf[0] & 1);
7827c23b892Sbalrog 
783093454e2SDmitry Fleytman     if (e1000x_is_vlan_packet(buf, le16_to_cpu(s->mac_reg[VET])) &&
784093454e2SDmitry Fleytman         e1000x_vlan_rx_filter_enabled(s->mac_reg)) {
78514e60aaeSPeter Maydell         uint16_t vid = lduw_be_p(buf + 14);
78614e60aaeSPeter Maydell         uint32_t vfta = ldl_le_p((uint32_t*)(s->mac_reg + VFTA) +
7878f2e8d1fSaliguori                                  ((vid >> 5) & 0x7f));
7888f2e8d1fSaliguori         if ((vfta & (1 << (vid & 0x1f))) == 0)
7898f2e8d1fSaliguori             return 0;
7908f2e8d1fSaliguori     }
7918f2e8d1fSaliguori 
7924aeea330SLeonid Bloch     if (!isbcast && !ismcast && (rctl & E1000_RCTL_UPE)) { /* promiscuous ucast */
7937c23b892Sbalrog         return 1;
7944aeea330SLeonid Bloch     }
7957c23b892Sbalrog 
7964aeea330SLeonid Bloch     if (ismcast && (rctl & E1000_RCTL_MPE)) {          /* promiscuous mcast */
797093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, MPRC);
7987c23b892Sbalrog         return 1;
7994aeea330SLeonid Bloch     }
8007c23b892Sbalrog 
8014aeea330SLeonid Bloch     if (isbcast && (rctl & E1000_RCTL_BAM)) {          /* broadcast enabled */
802093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, BPRC);
8037c23b892Sbalrog         return 1;
8044aeea330SLeonid Bloch     }
8057c23b892Sbalrog 
806093454e2SDmitry Fleytman     return e1000x_rx_group_filter(s->mac_reg, buf);
8077c23b892Sbalrog }
8087c23b892Sbalrog 
80999ed7e30Saliguori static void
8104e68f7a0SStefan Hajnoczi e1000_set_link_status(NetClientState *nc)
81199ed7e30Saliguori {
812cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
81399ed7e30Saliguori     uint32_t old_status = s->mac_reg[STATUS];
81499ed7e30Saliguori 
815d4044c2aSBjørn Mork     if (nc->link_down) {
816093454e2SDmitry Fleytman         e1000x_update_regs_on_link_down(s->mac_reg, s->phy_reg);
817d4044c2aSBjørn Mork     } else {
818d7a41552SGabriel L. Somlo         if (have_autoneg(s) &&
8196a2acedbSGabriel L. Somlo             !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
820093454e2SDmitry Fleytman             e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer);
8216a2acedbSGabriel L. Somlo         } else {
82271aadd3cSJason Wang             e1000_link_up(s);
823d4044c2aSBjørn Mork         }
8246a2acedbSGabriel L. Somlo     }
82599ed7e30Saliguori 
82699ed7e30Saliguori     if (s->mac_reg[STATUS] != old_status)
82799ed7e30Saliguori         set_ics(s, 0, E1000_ICR_LSC);
82899ed7e30Saliguori }
82999ed7e30Saliguori 
830322fd48aSMichael S. Tsirkin static bool e1000_has_rxbufs(E1000State *s, size_t total_size)
831322fd48aSMichael S. Tsirkin {
832322fd48aSMichael S. Tsirkin     int bufs;
833322fd48aSMichael S. Tsirkin     /* Fast-path short packets */
834322fd48aSMichael S. Tsirkin     if (total_size <= s->rxbuf_size) {
835e5b8b0d4SDmitry Fleytman         return s->mac_reg[RDH] != s->mac_reg[RDT];
836322fd48aSMichael S. Tsirkin     }
837322fd48aSMichael S. Tsirkin     if (s->mac_reg[RDH] < s->mac_reg[RDT]) {
838322fd48aSMichael S. Tsirkin         bufs = s->mac_reg[RDT] - s->mac_reg[RDH];
839e5b8b0d4SDmitry Fleytman     } else if (s->mac_reg[RDH] > s->mac_reg[RDT]) {
840322fd48aSMichael S. Tsirkin         bufs = s->mac_reg[RDLEN] /  sizeof(struct e1000_rx_desc) +
841322fd48aSMichael S. Tsirkin             s->mac_reg[RDT] - s->mac_reg[RDH];
842322fd48aSMichael S. Tsirkin     } else {
843322fd48aSMichael S. Tsirkin         return false;
844322fd48aSMichael S. Tsirkin     }
845322fd48aSMichael S. Tsirkin     return total_size <= bufs * s->rxbuf_size;
846322fd48aSMichael S. Tsirkin }
847322fd48aSMichael S. Tsirkin 
8486cdfab28SMichael S. Tsirkin static int
8494e68f7a0SStefan Hajnoczi e1000_can_receive(NetClientState *nc)
8506cdfab28SMichael S. Tsirkin {
851cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
8526cdfab28SMichael S. Tsirkin 
853093454e2SDmitry Fleytman     return e1000x_rx_ready(&s->parent_obj, s->mac_reg) &&
854157628d0Syuchenlin         e1000_has_rxbufs(s, 1) && !timer_pending(s->flush_queue_timer);
8556cdfab28SMichael S. Tsirkin }
8566cdfab28SMichael S. Tsirkin 
857d17161f6SKevin Wolf static uint64_t rx_desc_base(E1000State *s)
858d17161f6SKevin Wolf {
859d17161f6SKevin Wolf     uint64_t bah = s->mac_reg[RDBAH];
860d17161f6SKevin Wolf     uint64_t bal = s->mac_reg[RDBAL] & ~0xf;
861d17161f6SKevin Wolf 
862d17161f6SKevin Wolf     return (bah << 32) + bal;
863d17161f6SKevin Wolf }
864d17161f6SKevin Wolf 
8651001cf45SJason Wang static void
8661001cf45SJason Wang e1000_receiver_overrun(E1000State *s, size_t size)
8671001cf45SJason Wang {
8681001cf45SJason Wang     trace_e1000_receiver_overrun(size, s->mac_reg[RDH], s->mac_reg[RDT]);
8691001cf45SJason Wang     e1000x_inc_reg_if_not_full(s->mac_reg, RNBC);
8701001cf45SJason Wang     e1000x_inc_reg_if_not_full(s->mac_reg, MPC);
8711001cf45SJason Wang     set_ics(s, 0, E1000_ICS_RXO);
8721001cf45SJason Wang }
8731001cf45SJason Wang 
8744f1c942bSMark McLoughlin static ssize_t
87597410ddeSVincenzo Maffione e1000_receive_iov(NetClientState *nc, const struct iovec *iov, int iovcnt)
8767c23b892Sbalrog {
877cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
878b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
8797c23b892Sbalrog     struct e1000_rx_desc desc;
88062ecbd35SEduard - Gabriel Munteanu     dma_addr_t base;
8817c23b892Sbalrog     unsigned int n, rdt;
8827c23b892Sbalrog     uint32_t rdh_start;
8838f2e8d1fSaliguori     uint16_t vlan_special = 0;
88497410ddeSVincenzo Maffione     uint8_t vlan_status = 0;
88578aeb23eSStefan Hajnoczi     uint8_t min_buf[MIN_BUF_SIZE];
88697410ddeSVincenzo Maffione     struct iovec min_iov;
88797410ddeSVincenzo Maffione     uint8_t *filter_buf = iov->iov_base;
88897410ddeSVincenzo Maffione     size_t size = iov_size(iov, iovcnt);
88997410ddeSVincenzo Maffione     size_t iov_ofs = 0;
890b19487e2SMichael S. Tsirkin     size_t desc_offset;
891b19487e2SMichael S. Tsirkin     size_t desc_size;
892b19487e2SMichael S. Tsirkin     size_t total_size;
8937c23b892Sbalrog 
894093454e2SDmitry Fleytman     if (!e1000x_hw_rx_enabled(s->mac_reg)) {
895ddcb73b7SMichael S. Tsirkin         return -1;
896ddcb73b7SMichael S. Tsirkin     }
8977c23b892Sbalrog 
898157628d0Syuchenlin     if (timer_pending(s->flush_queue_timer)) {
899157628d0Syuchenlin         return 0;
900157628d0Syuchenlin     }
901157628d0Syuchenlin 
90278aeb23eSStefan Hajnoczi     /* Pad to minimum Ethernet frame length */
90378aeb23eSStefan Hajnoczi     if (size < sizeof(min_buf)) {
90497410ddeSVincenzo Maffione         iov_to_buf(iov, iovcnt, 0, min_buf, size);
90578aeb23eSStefan Hajnoczi         memset(&min_buf[size], 0, sizeof(min_buf) - size);
90697410ddeSVincenzo Maffione         min_iov.iov_base = filter_buf = min_buf;
90797410ddeSVincenzo Maffione         min_iov.iov_len = size = sizeof(min_buf);
90897410ddeSVincenzo Maffione         iovcnt = 1;
90997410ddeSVincenzo Maffione         iov = &min_iov;
91097410ddeSVincenzo Maffione     } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) {
91197410ddeSVincenzo Maffione         /* This is very unlikely, but may happen. */
91297410ddeSVincenzo Maffione         iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN);
91397410ddeSVincenzo Maffione         filter_buf = min_buf;
91478aeb23eSStefan Hajnoczi     }
91578aeb23eSStefan Hajnoczi 
916b0d9ffcdSMichael Contreras     /* Discard oversized packets if !LPE and !SBP. */
917093454e2SDmitry Fleytman     if (e1000x_is_oversized(s->mac_reg, size)) {
918b0d9ffcdSMichael Contreras         return size;
919b0d9ffcdSMichael Contreras     }
920b0d9ffcdSMichael Contreras 
92197410ddeSVincenzo Maffione     if (!receive_filter(s, filter_buf, size)) {
9224f1c942bSMark McLoughlin         return size;
92397410ddeSVincenzo Maffione     }
9247c23b892Sbalrog 
925093454e2SDmitry Fleytman     if (e1000x_vlan_enabled(s->mac_reg) &&
926093454e2SDmitry Fleytman         e1000x_is_vlan_packet(filter_buf, le16_to_cpu(s->mac_reg[VET]))) {
92714e60aaeSPeter Maydell         vlan_special = cpu_to_le16(lduw_be_p(filter_buf + 14));
92897410ddeSVincenzo Maffione         iov_ofs = 4;
92997410ddeSVincenzo Maffione         if (filter_buf == iov->iov_base) {
93097410ddeSVincenzo Maffione             memmove(filter_buf + 4, filter_buf, 12);
93197410ddeSVincenzo Maffione         } else {
93297410ddeSVincenzo Maffione             iov_from_buf(iov, iovcnt, 4, filter_buf, 12);
93397410ddeSVincenzo Maffione             while (iov->iov_len <= iov_ofs) {
93497410ddeSVincenzo Maffione                 iov_ofs -= iov->iov_len;
93597410ddeSVincenzo Maffione                 iov++;
93697410ddeSVincenzo Maffione             }
93797410ddeSVincenzo Maffione         }
9388f2e8d1fSaliguori         vlan_status = E1000_RXD_STAT_VP;
9398f2e8d1fSaliguori         size -= 4;
9408f2e8d1fSaliguori     }
9418f2e8d1fSaliguori 
9427c23b892Sbalrog     rdh_start = s->mac_reg[RDH];
943b19487e2SMichael S. Tsirkin     desc_offset = 0;
944093454e2SDmitry Fleytman     total_size = size + e1000x_fcs_len(s->mac_reg);
945322fd48aSMichael S. Tsirkin     if (!e1000_has_rxbufs(s, total_size)) {
9461001cf45SJason Wang         e1000_receiver_overrun(s, total_size);
947322fd48aSMichael S. Tsirkin         return -1;
948322fd48aSMichael S. Tsirkin     }
9497c23b892Sbalrog     do {
950b19487e2SMichael S. Tsirkin         desc_size = total_size - desc_offset;
951b19487e2SMichael S. Tsirkin         if (desc_size > s->rxbuf_size) {
952b19487e2SMichael S. Tsirkin             desc_size = s->rxbuf_size;
953b19487e2SMichael S. Tsirkin         }
954d17161f6SKevin Wolf         base = rx_desc_base(s) + sizeof(desc) * s->mac_reg[RDH];
955b08340d5SAndreas Färber         pci_dma_read(d, base, &desc, sizeof(desc));
9568f2e8d1fSaliguori         desc.special = vlan_special;
9578f2e8d1fSaliguori         desc.status |= (vlan_status | E1000_RXD_STAT_DD);
9587c23b892Sbalrog         if (desc.buffer_addr) {
959b19487e2SMichael S. Tsirkin             if (desc_offset < size) {
96097410ddeSVincenzo Maffione                 size_t iov_copy;
96197410ddeSVincenzo Maffione                 hwaddr ba = le64_to_cpu(desc.buffer_addr);
962b19487e2SMichael S. Tsirkin                 size_t copy_size = size - desc_offset;
963b19487e2SMichael S. Tsirkin                 if (copy_size > s->rxbuf_size) {
964b19487e2SMichael S. Tsirkin                     copy_size = s->rxbuf_size;
965b19487e2SMichael S. Tsirkin                 }
96697410ddeSVincenzo Maffione                 do {
96797410ddeSVincenzo Maffione                     iov_copy = MIN(copy_size, iov->iov_len - iov_ofs);
96897410ddeSVincenzo Maffione                     pci_dma_write(d, ba, iov->iov_base + iov_ofs, iov_copy);
96997410ddeSVincenzo Maffione                     copy_size -= iov_copy;
97097410ddeSVincenzo Maffione                     ba += iov_copy;
97197410ddeSVincenzo Maffione                     iov_ofs += iov_copy;
97297410ddeSVincenzo Maffione                     if (iov_ofs == iov->iov_len) {
97397410ddeSVincenzo Maffione                         iov++;
97497410ddeSVincenzo Maffione                         iov_ofs = 0;
97597410ddeSVincenzo Maffione                     }
97697410ddeSVincenzo Maffione                 } while (copy_size);
977b19487e2SMichael S. Tsirkin             }
978b19487e2SMichael S. Tsirkin             desc_offset += desc_size;
979b19487e2SMichael S. Tsirkin             desc.length = cpu_to_le16(desc_size);
980ee912ccfSMichael S. Tsirkin             if (desc_offset >= total_size) {
9817c23b892Sbalrog                 desc.status |= E1000_RXD_STAT_EOP | E1000_RXD_STAT_IXSM;
982b19487e2SMichael S. Tsirkin             } else {
983ee912ccfSMichael S. Tsirkin                 /* Guest zeroing out status is not a hardware requirement.
984ee912ccfSMichael S. Tsirkin                    Clear EOP in case guest didn't do it. */
985ee912ccfSMichael S. Tsirkin                 desc.status &= ~E1000_RXD_STAT_EOP;
986b19487e2SMichael S. Tsirkin             }
98743ad7e3eSJes Sorensen         } else { // as per intel docs; skip descriptors with null buf addr
9887c23b892Sbalrog             DBGOUT(RX, "Null RX descriptor!!\n");
98943ad7e3eSJes Sorensen         }
990b08340d5SAndreas Färber         pci_dma_write(d, base, &desc, sizeof(desc));
9917c23b892Sbalrog 
9927c23b892Sbalrog         if (++s->mac_reg[RDH] * sizeof(desc) >= s->mac_reg[RDLEN])
9937c23b892Sbalrog             s->mac_reg[RDH] = 0;
9947c23b892Sbalrog         /* see comment in start_xmit; same here */
995dd793a74SLaszlo Ersek         if (s->mac_reg[RDH] == rdh_start ||
996dd793a74SLaszlo Ersek             rdh_start >= s->mac_reg[RDLEN] / sizeof(desc)) {
9977c23b892Sbalrog             DBGOUT(RXERR, "RDH wraparound @%x, RDT %x, RDLEN %x\n",
9987c23b892Sbalrog                    rdh_start, s->mac_reg[RDT], s->mac_reg[RDLEN]);
9991001cf45SJason Wang             e1000_receiver_overrun(s, total_size);
10004f1c942bSMark McLoughlin             return -1;
10017c23b892Sbalrog         }
1002b19487e2SMichael S. Tsirkin     } while (desc_offset < total_size);
10037c23b892Sbalrog 
1004093454e2SDmitry Fleytman     e1000x_update_rx_total_stats(s->mac_reg, size, total_size);
10057c23b892Sbalrog 
10067c23b892Sbalrog     n = E1000_ICS_RXT0;
10077c23b892Sbalrog     if ((rdt = s->mac_reg[RDT]) < s->mac_reg[RDH])
10087c23b892Sbalrog         rdt += s->mac_reg[RDLEN] / sizeof(desc);
1009bf16cc8fSaliguori     if (((rdt - s->mac_reg[RDH]) * sizeof(desc)) <= s->mac_reg[RDLEN] >>
1010bf16cc8fSaliguori         s->rxbuf_min_shift)
10117c23b892Sbalrog         n |= E1000_ICS_RXDMT0;
10127c23b892Sbalrog 
10137c23b892Sbalrog     set_ics(s, 0, n);
10144f1c942bSMark McLoughlin 
10154f1c942bSMark McLoughlin     return size;
10167c23b892Sbalrog }
10177c23b892Sbalrog 
101897410ddeSVincenzo Maffione static ssize_t
101997410ddeSVincenzo Maffione e1000_receive(NetClientState *nc, const uint8_t *buf, size_t size)
102097410ddeSVincenzo Maffione {
102197410ddeSVincenzo Maffione     const struct iovec iov = {
102297410ddeSVincenzo Maffione         .iov_base = (uint8_t *)buf,
102397410ddeSVincenzo Maffione         .iov_len = size
102497410ddeSVincenzo Maffione     };
102597410ddeSVincenzo Maffione 
102697410ddeSVincenzo Maffione     return e1000_receive_iov(nc, &iov, 1);
102797410ddeSVincenzo Maffione }
102897410ddeSVincenzo Maffione 
10297c23b892Sbalrog static uint32_t
10307c23b892Sbalrog mac_readreg(E1000State *s, int index)
10317c23b892Sbalrog {
10327c23b892Sbalrog     return s->mac_reg[index];
10337c23b892Sbalrog }
10347c23b892Sbalrog 
10357c23b892Sbalrog static uint32_t
103672ea771cSLeonid Bloch mac_low4_read(E1000State *s, int index)
103772ea771cSLeonid Bloch {
103872ea771cSLeonid Bloch     return s->mac_reg[index] & 0xf;
103972ea771cSLeonid Bloch }
104072ea771cSLeonid Bloch 
104172ea771cSLeonid Bloch static uint32_t
104272ea771cSLeonid Bloch mac_low11_read(E1000State *s, int index)
104372ea771cSLeonid Bloch {
104472ea771cSLeonid Bloch     return s->mac_reg[index] & 0x7ff;
104572ea771cSLeonid Bloch }
104672ea771cSLeonid Bloch 
104772ea771cSLeonid Bloch static uint32_t
104872ea771cSLeonid Bloch mac_low13_read(E1000State *s, int index)
104972ea771cSLeonid Bloch {
105072ea771cSLeonid Bloch     return s->mac_reg[index] & 0x1fff;
105172ea771cSLeonid Bloch }
105272ea771cSLeonid Bloch 
105372ea771cSLeonid Bloch static uint32_t
105472ea771cSLeonid Bloch mac_low16_read(E1000State *s, int index)
105572ea771cSLeonid Bloch {
105672ea771cSLeonid Bloch     return s->mac_reg[index] & 0xffff;
105772ea771cSLeonid Bloch }
105872ea771cSLeonid Bloch 
105972ea771cSLeonid Bloch static uint32_t
10607c23b892Sbalrog mac_icr_read(E1000State *s, int index)
10617c23b892Sbalrog {
10627c23b892Sbalrog     uint32_t ret = s->mac_reg[ICR];
10637c23b892Sbalrog 
10647c23b892Sbalrog     DBGOUT(INTERRUPT, "ICR read: %x\n", ret);
10657c23b892Sbalrog     set_interrupt_cause(s, 0, 0);
10667c23b892Sbalrog     return ret;
10677c23b892Sbalrog }
10687c23b892Sbalrog 
10697c23b892Sbalrog static uint32_t
10707c23b892Sbalrog mac_read_clr4(E1000State *s, int index)
10717c23b892Sbalrog {
10727c23b892Sbalrog     uint32_t ret = s->mac_reg[index];
10737c23b892Sbalrog 
10747c23b892Sbalrog     s->mac_reg[index] = 0;
10757c23b892Sbalrog     return ret;
10767c23b892Sbalrog }
10777c23b892Sbalrog 
10787c23b892Sbalrog static uint32_t
10797c23b892Sbalrog mac_read_clr8(E1000State *s, int index)
10807c23b892Sbalrog {
10817c23b892Sbalrog     uint32_t ret = s->mac_reg[index];
10827c23b892Sbalrog 
10837c23b892Sbalrog     s->mac_reg[index] = 0;
10847c23b892Sbalrog     s->mac_reg[index-1] = 0;
10857c23b892Sbalrog     return ret;
10867c23b892Sbalrog }
10877c23b892Sbalrog 
10887c23b892Sbalrog static void
10897c23b892Sbalrog mac_writereg(E1000State *s, int index, uint32_t val)
10907c23b892Sbalrog {
10917c36507cSAmos Kong     uint32_t macaddr[2];
10927c36507cSAmos Kong 
10937c23b892Sbalrog     s->mac_reg[index] = val;
10947c36507cSAmos Kong 
109590d131fbSMichael S. Tsirkin     if (index == RA + 1) {
10967c36507cSAmos Kong         macaddr[0] = cpu_to_le32(s->mac_reg[RA]);
10977c36507cSAmos Kong         macaddr[1] = cpu_to_le32(s->mac_reg[RA + 1]);
10987c36507cSAmos Kong         qemu_format_nic_info_str(qemu_get_queue(s->nic), (uint8_t *)macaddr);
10997c36507cSAmos Kong     }
11007c23b892Sbalrog }
11017c23b892Sbalrog 
11027c23b892Sbalrog static void
11037c23b892Sbalrog set_rdt(E1000State *s, int index, uint32_t val)
11047c23b892Sbalrog {
11057c23b892Sbalrog     s->mac_reg[index] = val & 0xffff;
1106e8b4c680SPaolo Bonzini     if (e1000_has_rxbufs(s, 1)) {
1107b356f76dSJason Wang         qemu_flush_queued_packets(qemu_get_queue(s->nic));
1108e8b4c680SPaolo Bonzini     }
11097c23b892Sbalrog }
11107c23b892Sbalrog 
11117c23b892Sbalrog static void
11127c23b892Sbalrog set_16bit(E1000State *s, int index, uint32_t val)
11137c23b892Sbalrog {
11147c23b892Sbalrog     s->mac_reg[index] = val & 0xffff;
11157c23b892Sbalrog }
11167c23b892Sbalrog 
11177c23b892Sbalrog static void
11187c23b892Sbalrog set_dlen(E1000State *s, int index, uint32_t val)
11197c23b892Sbalrog {
11207c23b892Sbalrog     s->mac_reg[index] = val & 0xfff80;
11217c23b892Sbalrog }
11227c23b892Sbalrog 
11237c23b892Sbalrog static void
11247c23b892Sbalrog set_tctl(E1000State *s, int index, uint32_t val)
11257c23b892Sbalrog {
11267c23b892Sbalrog     s->mac_reg[index] = val;
11277c23b892Sbalrog     s->mac_reg[TDT] &= 0xffff;
11287c23b892Sbalrog     start_xmit(s);
11297c23b892Sbalrog }
11307c23b892Sbalrog 
11317c23b892Sbalrog static void
11327c23b892Sbalrog set_icr(E1000State *s, int index, uint32_t val)
11337c23b892Sbalrog {
11347c23b892Sbalrog     DBGOUT(INTERRUPT, "set_icr %x\n", val);
11357c23b892Sbalrog     set_interrupt_cause(s, 0, s->mac_reg[ICR] & ~val);
11367c23b892Sbalrog }
11377c23b892Sbalrog 
11387c23b892Sbalrog static void
11397c23b892Sbalrog set_imc(E1000State *s, int index, uint32_t val)
11407c23b892Sbalrog {
11417c23b892Sbalrog     s->mac_reg[IMS] &= ~val;
11427c23b892Sbalrog     set_ics(s, 0, 0);
11437c23b892Sbalrog }
11447c23b892Sbalrog 
11457c23b892Sbalrog static void
11467c23b892Sbalrog set_ims(E1000State *s, int index, uint32_t val)
11477c23b892Sbalrog {
11487c23b892Sbalrog     s->mac_reg[IMS] |= val;
11497c23b892Sbalrog     set_ics(s, 0, 0);
11507c23b892Sbalrog }
11517c23b892Sbalrog 
11527c23b892Sbalrog #define getreg(x)    [x] = mac_readreg
1153*3b6b3a27SPhilippe Mathieu-Daudé typedef uint32_t (*readops)(E1000State *, int);
1154*3b6b3a27SPhilippe Mathieu-Daudé static readops macreg_readops[] = {
11557c23b892Sbalrog     getreg(PBA),      getreg(RCTL),     getreg(TDH),      getreg(TXDCTL),
11567c23b892Sbalrog     getreg(WUFC),     getreg(TDT),      getreg(CTRL),     getreg(LEDCTL),
11577c23b892Sbalrog     getreg(MANC),     getreg(MDIC),     getreg(SWSM),     getreg(STATUS),
11587c23b892Sbalrog     getreg(TORL),     getreg(TOTL),     getreg(IMS),      getreg(TCTL),
1159b1332393SBill Paul     getreg(RDH),      getreg(RDT),      getreg(VET),      getreg(ICS),
1160a00b2335SKay Ackermann     getreg(TDBAL),    getreg(TDBAH),    getreg(RDBAH),    getreg(RDBAL),
1161e9845f09SVincenzo Maffione     getreg(TDLEN),    getreg(RDLEN),    getreg(RDTR),     getreg(RADV),
116272ea771cSLeonid Bloch     getreg(TADV),     getreg(ITR),      getreg(FCRUC),    getreg(IPAV),
116372ea771cSLeonid Bloch     getreg(WUC),      getreg(WUS),      getreg(SCC),      getreg(ECOL),
116472ea771cSLeonid Bloch     getreg(MCC),      getreg(LATECOL),  getreg(COLC),     getreg(DC),
1165757704f1SKamil Rytarowski     getreg(TNCRS),    getreg(SEQEC),    getreg(CEXTERR),  getreg(RLEC),
116672ea771cSLeonid Bloch     getreg(XONRXC),   getreg(XONTXC),   getreg(XOFFRXC),  getreg(XOFFTXC),
116772ea771cSLeonid Bloch     getreg(RFC),      getreg(RJC),      getreg(RNBC),     getreg(TSCTFC),
11683b274301SLeonid Bloch     getreg(MGTPRC),   getreg(MGTPDC),   getreg(MGTPTC),   getreg(GORCL),
11693b274301SLeonid Bloch     getreg(GOTCL),
11707c23b892Sbalrog 
117120f3e863SLeonid Bloch     [TOTH]    = mac_read_clr8,      [TORH]    = mac_read_clr8,
11723b274301SLeonid Bloch     [GOTCH]   = mac_read_clr8,      [GORCH]   = mac_read_clr8,
11733b274301SLeonid Bloch     [PRC64]   = mac_read_clr4,      [PRC127]  = mac_read_clr4,
11743b274301SLeonid Bloch     [PRC255]  = mac_read_clr4,      [PRC511]  = mac_read_clr4,
11753b274301SLeonid Bloch     [PRC1023] = mac_read_clr4,      [PRC1522] = mac_read_clr4,
11763b274301SLeonid Bloch     [PTC64]   = mac_read_clr4,      [PTC127]  = mac_read_clr4,
11773b274301SLeonid Bloch     [PTC255]  = mac_read_clr4,      [PTC511]  = mac_read_clr4,
11783b274301SLeonid Bloch     [PTC1023] = mac_read_clr4,      [PTC1522] = mac_read_clr4,
117920f3e863SLeonid Bloch     [GPRC]    = mac_read_clr4,      [GPTC]    = mac_read_clr4,
118020f3e863SLeonid Bloch     [TPT]     = mac_read_clr4,      [TPR]     = mac_read_clr4,
11813b274301SLeonid Bloch     [RUC]     = mac_read_clr4,      [ROC]     = mac_read_clr4,
11823b274301SLeonid Bloch     [BPRC]    = mac_read_clr4,      [MPRC]    = mac_read_clr4,
11833b274301SLeonid Bloch     [TSCTC]   = mac_read_clr4,      [BPTC]    = mac_read_clr4,
11843b274301SLeonid Bloch     [MPTC]    = mac_read_clr4,
118520f3e863SLeonid Bloch     [ICR]     = mac_icr_read,       [EECD]    = get_eecd,
118620f3e863SLeonid Bloch     [EERD]    = flash_eerd_read,
118772ea771cSLeonid Bloch     [RDFH]    = mac_low13_read,     [RDFT]    = mac_low13_read,
118872ea771cSLeonid Bloch     [RDFHS]   = mac_low13_read,     [RDFTS]   = mac_low13_read,
118972ea771cSLeonid Bloch     [RDFPC]   = mac_low13_read,
119072ea771cSLeonid Bloch     [TDFH]    = mac_low11_read,     [TDFT]    = mac_low11_read,
119172ea771cSLeonid Bloch     [TDFHS]   = mac_low13_read,     [TDFTS]   = mac_low13_read,
119272ea771cSLeonid Bloch     [TDFPC]   = mac_low13_read,
119372ea771cSLeonid Bloch     [AIT]     = mac_low16_read,
119420f3e863SLeonid Bloch 
11957c23b892Sbalrog     [CRCERRS ... MPC]   = &mac_readreg,
119672ea771cSLeonid Bloch     [IP6AT ... IP6AT+3] = &mac_readreg,    [IP4AT ... IP4AT+6] = &mac_readreg,
119772ea771cSLeonid Bloch     [FFLT ... FFLT+6]   = &mac_low11_read,
11987c23b892Sbalrog     [RA ... RA+31]      = &mac_readreg,
119972ea771cSLeonid Bloch     [WUPM ... WUPM+31]  = &mac_readreg,
12007c23b892Sbalrog     [MTA ... MTA+127]   = &mac_readreg,
12018f2e8d1fSaliguori     [VFTA ... VFTA+127] = &mac_readreg,
120272ea771cSLeonid Bloch     [FFMT ... FFMT+254] = &mac_low4_read,
120372ea771cSLeonid Bloch     [FFVT ... FFVT+254] = &mac_readreg,
120472ea771cSLeonid Bloch     [PBM ... PBM+16383] = &mac_readreg,
12057c23b892Sbalrog };
1206b1503cdaSmalc enum { NREADOPS = ARRAY_SIZE(macreg_readops) };
12077c23b892Sbalrog 
12087c23b892Sbalrog #define putreg(x)    [x] = mac_writereg
1209*3b6b3a27SPhilippe Mathieu-Daudé typedef void (*writeops)(E1000State *, int, uint32_t);
1210*3b6b3a27SPhilippe Mathieu-Daudé static writeops macreg_writeops[] = {
12117c23b892Sbalrog     putreg(PBA),      putreg(EERD),     putreg(SWSM),     putreg(WUFC),
12127c23b892Sbalrog     putreg(TDBAL),    putreg(TDBAH),    putreg(TXDCTL),   putreg(RDBAH),
121372ea771cSLeonid Bloch     putreg(RDBAL),    putreg(LEDCTL),   putreg(VET),      putreg(FCRUC),
121472ea771cSLeonid Bloch     putreg(TDFH),     putreg(TDFT),     putreg(TDFHS),    putreg(TDFTS),
121572ea771cSLeonid Bloch     putreg(TDFPC),    putreg(RDFH),     putreg(RDFT),     putreg(RDFHS),
121672ea771cSLeonid Bloch     putreg(RDFTS),    putreg(RDFPC),    putreg(IPAV),     putreg(WUC),
121772ea771cSLeonid Bloch     putreg(WUS),      putreg(AIT),
121820f3e863SLeonid Bloch 
12197c23b892Sbalrog     [TDLEN]  = set_dlen,   [RDLEN]  = set_dlen,       [TCTL] = set_tctl,
12207c23b892Sbalrog     [TDT]    = set_tctl,   [MDIC]   = set_mdic,       [ICS]  = set_ics,
12217c23b892Sbalrog     [TDH]    = set_16bit,  [RDH]    = set_16bit,      [RDT]  = set_rdt,
12227c23b892Sbalrog     [IMC]    = set_imc,    [IMS]    = set_ims,        [ICR]  = set_icr,
1223cab3c825SKevin Wolf     [EECD]   = set_eecd,   [RCTL]   = set_rx_control, [CTRL] = set_ctrl,
1224e9845f09SVincenzo Maffione     [RDTR]   = set_16bit,  [RADV]   = set_16bit,      [TADV] = set_16bit,
1225e9845f09SVincenzo Maffione     [ITR]    = set_16bit,
122620f3e863SLeonid Bloch 
122772ea771cSLeonid Bloch     [IP6AT ... IP6AT+3] = &mac_writereg, [IP4AT ... IP4AT+6] = &mac_writereg,
122872ea771cSLeonid Bloch     [FFLT ... FFLT+6]   = &mac_writereg,
12297c23b892Sbalrog     [RA ... RA+31]      = &mac_writereg,
123072ea771cSLeonid Bloch     [WUPM ... WUPM+31]  = &mac_writereg,
12317c23b892Sbalrog     [MTA ... MTA+127]   = &mac_writereg,
12328f2e8d1fSaliguori     [VFTA ... VFTA+127] = &mac_writereg,
123372ea771cSLeonid Bloch     [FFMT ... FFMT+254] = &mac_writereg, [FFVT ... FFVT+254] = &mac_writereg,
123472ea771cSLeonid Bloch     [PBM ... PBM+16383] = &mac_writereg,
12357c23b892Sbalrog };
1236b9d03e35SJason Wang 
1237b1503cdaSmalc enum { NWRITEOPS = ARRAY_SIZE(macreg_writeops) };
12387c23b892Sbalrog 
1239bc0f0674SLeonid Bloch enum { MAC_ACCESS_PARTIAL = 1, MAC_ACCESS_FLAG_NEEDED = 2 };
1240bc0f0674SLeonid Bloch 
1241bc0f0674SLeonid Bloch #define markflag(x)    ((E1000_FLAG_##x << 2) | MAC_ACCESS_FLAG_NEEDED)
1242bc0f0674SLeonid Bloch /* In the array below the meaning of the bits is: [f|f|f|f|f|f|n|p]
1243bc0f0674SLeonid Bloch  * f - flag bits (up to 6 possible flags)
1244bc0f0674SLeonid Bloch  * n - flag needed
1245bc0f0674SLeonid Bloch  * p - partially implenented */
1246bc0f0674SLeonid Bloch static const uint8_t mac_reg_access[0x8000] = {
1247bc0f0674SLeonid Bloch     [RDTR]    = markflag(MIT),    [TADV]    = markflag(MIT),
1248bc0f0674SLeonid Bloch     [RADV]    = markflag(MIT),    [ITR]     = markflag(MIT),
124972ea771cSLeonid Bloch 
125072ea771cSLeonid Bloch     [IPAV]    = markflag(MAC),    [WUC]     = markflag(MAC),
125172ea771cSLeonid Bloch     [IP6AT]   = markflag(MAC),    [IP4AT]   = markflag(MAC),
125272ea771cSLeonid Bloch     [FFVT]    = markflag(MAC),    [WUPM]    = markflag(MAC),
125372ea771cSLeonid Bloch     [ECOL]    = markflag(MAC),    [MCC]     = markflag(MAC),
125472ea771cSLeonid Bloch     [DC]      = markflag(MAC),    [TNCRS]   = markflag(MAC),
125572ea771cSLeonid Bloch     [RLEC]    = markflag(MAC),    [XONRXC]  = markflag(MAC),
125672ea771cSLeonid Bloch     [XOFFTXC] = markflag(MAC),    [RFC]     = markflag(MAC),
125772ea771cSLeonid Bloch     [TSCTFC]  = markflag(MAC),    [MGTPRC]  = markflag(MAC),
125872ea771cSLeonid Bloch     [WUS]     = markflag(MAC),    [AIT]     = markflag(MAC),
125972ea771cSLeonid Bloch     [FFLT]    = markflag(MAC),    [FFMT]    = markflag(MAC),
126072ea771cSLeonid Bloch     [SCC]     = markflag(MAC),    [FCRUC]   = markflag(MAC),
126172ea771cSLeonid Bloch     [LATECOL] = markflag(MAC),    [COLC]    = markflag(MAC),
1262757704f1SKamil Rytarowski     [SEQEC]   = markflag(MAC),    [CEXTERR] = markflag(MAC),
126372ea771cSLeonid Bloch     [XONTXC]  = markflag(MAC),    [XOFFRXC] = markflag(MAC),
126472ea771cSLeonid Bloch     [RJC]     = markflag(MAC),    [RNBC]    = markflag(MAC),
126572ea771cSLeonid Bloch     [MGTPDC]  = markflag(MAC),    [MGTPTC]  = markflag(MAC),
12663b274301SLeonid Bloch     [RUC]     = markflag(MAC),    [ROC]     = markflag(MAC),
12673b274301SLeonid Bloch     [GORCL]   = markflag(MAC),    [GORCH]   = markflag(MAC),
12683b274301SLeonid Bloch     [GOTCL]   = markflag(MAC),    [GOTCH]   = markflag(MAC),
12693b274301SLeonid Bloch     [BPRC]    = markflag(MAC),    [MPRC]    = markflag(MAC),
12703b274301SLeonid Bloch     [TSCTC]   = markflag(MAC),    [PRC64]   = markflag(MAC),
12713b274301SLeonid Bloch     [PRC127]  = markflag(MAC),    [PRC255]  = markflag(MAC),
12723b274301SLeonid Bloch     [PRC511]  = markflag(MAC),    [PRC1023] = markflag(MAC),
12733b274301SLeonid Bloch     [PRC1522] = markflag(MAC),    [PTC64]   = markflag(MAC),
12743b274301SLeonid Bloch     [PTC127]  = markflag(MAC),    [PTC255]  = markflag(MAC),
12753b274301SLeonid Bloch     [PTC511]  = markflag(MAC),    [PTC1023] = markflag(MAC),
12763b274301SLeonid Bloch     [PTC1522] = markflag(MAC),    [MPTC]    = markflag(MAC),
12773b274301SLeonid Bloch     [BPTC]    = markflag(MAC),
127872ea771cSLeonid Bloch 
127972ea771cSLeonid Bloch     [TDFH]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
128072ea771cSLeonid Bloch     [TDFT]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
128172ea771cSLeonid Bloch     [TDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
128272ea771cSLeonid Bloch     [TDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
128372ea771cSLeonid Bloch     [TDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL,
128472ea771cSLeonid Bloch     [RDFH]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
128572ea771cSLeonid Bloch     [RDFT]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
128672ea771cSLeonid Bloch     [RDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
128772ea771cSLeonid Bloch     [RDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
128872ea771cSLeonid Bloch     [RDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL,
128972ea771cSLeonid Bloch     [PBM]   = markflag(MAC) | MAC_ACCESS_PARTIAL,
1290bc0f0674SLeonid Bloch };
1291bc0f0674SLeonid Bloch 
12927c23b892Sbalrog static void
1293a8170e5eSAvi Kivity e1000_mmio_write(void *opaque, hwaddr addr, uint64_t val,
1294ad00a9b9SAvi Kivity                  unsigned size)
12957c23b892Sbalrog {
12967c23b892Sbalrog     E1000State *s = opaque;
12978da3ff18Spbrook     unsigned int index = (addr & 0x1ffff) >> 2;
12987c23b892Sbalrog 
129943ad7e3eSJes Sorensen     if (index < NWRITEOPS && macreg_writeops[index]) {
1300bc0f0674SLeonid Bloch         if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED)
1301bc0f0674SLeonid Bloch             || (s->compat_flags & (mac_reg_access[index] >> 2))) {
1302bc0f0674SLeonid Bloch             if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
1303bc0f0674SLeonid Bloch                 DBGOUT(GENERAL, "Writing to register at offset: 0x%08x. "
1304bc0f0674SLeonid Bloch                        "It is not fully implemented.\n", index<<2);
1305bc0f0674SLeonid Bloch             }
13066b59fc74Saurel32             macreg_writeops[index](s, index, val);
1307bc0f0674SLeonid Bloch         } else {    /* "flag needed" bit is set, but the flag is not active */
1308bc0f0674SLeonid Bloch             DBGOUT(MMIO, "MMIO write attempt to disabled reg. addr=0x%08x\n",
1309bc0f0674SLeonid Bloch                    index<<2);
1310bc0f0674SLeonid Bloch         }
131143ad7e3eSJes Sorensen     } else if (index < NREADOPS && macreg_readops[index]) {
1312bc0f0674SLeonid Bloch         DBGOUT(MMIO, "e1000_mmio_writel RO %x: 0x%04"PRIx64"\n",
1313bc0f0674SLeonid Bloch                index<<2, val);
131443ad7e3eSJes Sorensen     } else {
1315ad00a9b9SAvi Kivity         DBGOUT(UNKNOWN, "MMIO unknown write addr=0x%08x,val=0x%08"PRIx64"\n",
13167c23b892Sbalrog                index<<2, val);
13177c23b892Sbalrog     }
131843ad7e3eSJes Sorensen }
13197c23b892Sbalrog 
1320ad00a9b9SAvi Kivity static uint64_t
1321a8170e5eSAvi Kivity e1000_mmio_read(void *opaque, hwaddr addr, unsigned size)
13227c23b892Sbalrog {
13237c23b892Sbalrog     E1000State *s = opaque;
13248da3ff18Spbrook     unsigned int index = (addr & 0x1ffff) >> 2;
13257c23b892Sbalrog 
1326bc0f0674SLeonid Bloch     if (index < NREADOPS && macreg_readops[index]) {
1327bc0f0674SLeonid Bloch         if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED)
1328bc0f0674SLeonid Bloch             || (s->compat_flags & (mac_reg_access[index] >> 2))) {
1329bc0f0674SLeonid Bloch             if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
1330bc0f0674SLeonid Bloch                 DBGOUT(GENERAL, "Reading register at offset: 0x%08x. "
1331bc0f0674SLeonid Bloch                        "It is not fully implemented.\n", index<<2);
13326b59fc74Saurel32             }
1333bc0f0674SLeonid Bloch             return macreg_readops[index](s, index);
1334bc0f0674SLeonid Bloch         } else {    /* "flag needed" bit is set, but the flag is not active */
1335bc0f0674SLeonid Bloch             DBGOUT(MMIO, "MMIO read attempt of disabled reg. addr=0x%08x\n",
1336bc0f0674SLeonid Bloch                    index<<2);
1337bc0f0674SLeonid Bloch         }
1338bc0f0674SLeonid Bloch     } else {
13397c23b892Sbalrog         DBGOUT(UNKNOWN, "MMIO unknown read addr=0x%08x\n", index<<2);
1340bc0f0674SLeonid Bloch     }
13417c23b892Sbalrog     return 0;
13427c23b892Sbalrog }
13437c23b892Sbalrog 
1344ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_mmio_ops = {
1345ad00a9b9SAvi Kivity     .read = e1000_mmio_read,
1346ad00a9b9SAvi Kivity     .write = e1000_mmio_write,
1347ad00a9b9SAvi Kivity     .endianness = DEVICE_LITTLE_ENDIAN,
1348ad00a9b9SAvi Kivity     .impl = {
1349ad00a9b9SAvi Kivity         .min_access_size = 4,
1350ad00a9b9SAvi Kivity         .max_access_size = 4,
1351ad00a9b9SAvi Kivity     },
1352ad00a9b9SAvi Kivity };
1353ad00a9b9SAvi Kivity 
1354a8170e5eSAvi Kivity static uint64_t e1000_io_read(void *opaque, hwaddr addr,
1355ad00a9b9SAvi Kivity                               unsigned size)
13567c23b892Sbalrog {
1357ad00a9b9SAvi Kivity     E1000State *s = opaque;
1358ad00a9b9SAvi Kivity 
1359ad00a9b9SAvi Kivity     (void)s;
1360ad00a9b9SAvi Kivity     return 0;
13617c23b892Sbalrog }
13627c23b892Sbalrog 
1363a8170e5eSAvi Kivity static void e1000_io_write(void *opaque, hwaddr addr,
1364ad00a9b9SAvi Kivity                            uint64_t val, unsigned size)
13657c23b892Sbalrog {
1366ad00a9b9SAvi Kivity     E1000State *s = opaque;
1367ad00a9b9SAvi Kivity 
1368ad00a9b9SAvi Kivity     (void)s;
13697c23b892Sbalrog }
13707c23b892Sbalrog 
1371ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_io_ops = {
1372ad00a9b9SAvi Kivity     .read = e1000_io_read,
1373ad00a9b9SAvi Kivity     .write = e1000_io_write,
1374ad00a9b9SAvi Kivity     .endianness = DEVICE_LITTLE_ENDIAN,
1375ad00a9b9SAvi Kivity };
1376ad00a9b9SAvi Kivity 
1377e482dc3eSJuan Quintela static bool is_version_1(void *opaque, int version_id)
13787c23b892Sbalrog {
1379e482dc3eSJuan Quintela     return version_id == 1;
13807c23b892Sbalrog }
13817c23b892Sbalrog 
138244b1ff31SDr. David Alan Gilbert static int e1000_pre_save(void *opaque)
1383ddcb73b7SMichael S. Tsirkin {
1384ddcb73b7SMichael S. Tsirkin     E1000State *s = opaque;
1385ddcb73b7SMichael S. Tsirkin     NetClientState *nc = qemu_get_queue(s->nic);
13862af234e6SMichael S. Tsirkin 
1387ddcb73b7SMichael S. Tsirkin     /*
13886a2acedbSGabriel L. Somlo      * If link is down and auto-negotiation is supported and ongoing,
13896a2acedbSGabriel L. Somlo      * complete auto-negotiation immediately. This allows us to look
13906a2acedbSGabriel L. Somlo      * at MII_SR_AUTONEG_COMPLETE to infer link status on load.
1391ddcb73b7SMichael S. Tsirkin      */
1392d7a41552SGabriel L. Somlo     if (nc->link_down && have_autoneg(s)) {
1393ddcb73b7SMichael S. Tsirkin         s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
1394ddcb73b7SMichael S. Tsirkin     }
139544b1ff31SDr. David Alan Gilbert 
1396ff214d42SDr. David Alan Gilbert     /* Decide which set of props to migrate in the main structure */
1397ff214d42SDr. David Alan Gilbert     if (chkflag(TSO) || !s->use_tso_for_migration) {
1398ff214d42SDr. David Alan Gilbert         /* Either we're migrating with the extra subsection, in which
1399ff214d42SDr. David Alan Gilbert          * case the mig_props is always 'props' OR
1400ff214d42SDr. David Alan Gilbert          * we've not got the subsection, but 'props' was the last
1401ff214d42SDr. David Alan Gilbert          * updated.
1402ff214d42SDr. David Alan Gilbert          */
140359354484SDr. David Alan Gilbert         s->mig_props = s->tx.props;
1404ff214d42SDr. David Alan Gilbert     } else {
1405ff214d42SDr. David Alan Gilbert         /* We're not using the subsection, and 'tso_props' was
1406ff214d42SDr. David Alan Gilbert          * the last updated.
1407ff214d42SDr. David Alan Gilbert          */
1408ff214d42SDr. David Alan Gilbert         s->mig_props = s->tx.tso_props;
1409ff214d42SDr. David Alan Gilbert     }
141044b1ff31SDr. David Alan Gilbert     return 0;
1411ddcb73b7SMichael S. Tsirkin }
1412ddcb73b7SMichael S. Tsirkin 
1413e4b82364SAmos Kong static int e1000_post_load(void *opaque, int version_id)
1414e4b82364SAmos Kong {
1415e4b82364SAmos Kong     E1000State *s = opaque;
1416b356f76dSJason Wang     NetClientState *nc = qemu_get_queue(s->nic);
1417e4b82364SAmos Kong 
1418bc0f0674SLeonid Bloch     if (!chkflag(MIT)) {
1419e9845f09SVincenzo Maffione         s->mac_reg[ITR] = s->mac_reg[RDTR] = s->mac_reg[RADV] =
1420e9845f09SVincenzo Maffione             s->mac_reg[TADV] = 0;
1421e9845f09SVincenzo Maffione         s->mit_irq_level = false;
1422e9845f09SVincenzo Maffione     }
1423e9845f09SVincenzo Maffione     s->mit_ide = 0;
1424f46efa9bSJason Wang     s->mit_timer_on = true;
1425f46efa9bSJason Wang     timer_mod(s->mit_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 1);
1426e9845f09SVincenzo Maffione 
1427e4b82364SAmos Kong     /* nc.link_down can't be migrated, so infer link_down according
1428ddcb73b7SMichael S. Tsirkin      * to link status bit in mac_reg[STATUS].
1429ddcb73b7SMichael S. Tsirkin      * Alternatively, restart link negotiation if it was in progress. */
1430b356f76dSJason Wang     nc->link_down = (s->mac_reg[STATUS] & E1000_STATUS_LU) == 0;
14312af234e6SMichael S. Tsirkin 
1432d7a41552SGabriel L. Somlo     if (have_autoneg(s) &&
1433ddcb73b7SMichael S. Tsirkin         !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
1434ddcb73b7SMichael S. Tsirkin         nc->link_down = false;
1435d7a41552SGabriel L. Somlo         timer_mod(s->autoneg_timer,
1436d7a41552SGabriel L. Somlo                   qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
1437ddcb73b7SMichael S. Tsirkin     }
1438e4b82364SAmos Kong 
143959354484SDr. David Alan Gilbert     s->tx.props = s->mig_props;
14403c4053c5SDr. David Alan Gilbert     if (!s->received_tx_tso) {
14413c4053c5SDr. David Alan Gilbert         /* We received only one set of offload data (tx.props)
14423c4053c5SDr. David Alan Gilbert          * and haven't got tx.tso_props.  The best we can do
14433c4053c5SDr. David Alan Gilbert          * is dupe the data.
14443c4053c5SDr. David Alan Gilbert          */
144559354484SDr. David Alan Gilbert         s->tx.tso_props = s->mig_props;
14463c4053c5SDr. David Alan Gilbert     }
14473c4053c5SDr. David Alan Gilbert     return 0;
14483c4053c5SDr. David Alan Gilbert }
14493c4053c5SDr. David Alan Gilbert 
14503c4053c5SDr. David Alan Gilbert static int e1000_tx_tso_post_load(void *opaque, int version_id)
14513c4053c5SDr. David Alan Gilbert {
14523c4053c5SDr. David Alan Gilbert     E1000State *s = opaque;
14533c4053c5SDr. David Alan Gilbert     s->received_tx_tso = true;
1454e4b82364SAmos Kong     return 0;
1455e4b82364SAmos Kong }
1456e4b82364SAmos Kong 
1457e9845f09SVincenzo Maffione static bool e1000_mit_state_needed(void *opaque)
1458e9845f09SVincenzo Maffione {
1459e9845f09SVincenzo Maffione     E1000State *s = opaque;
1460e9845f09SVincenzo Maffione 
1461bc0f0674SLeonid Bloch     return chkflag(MIT);
1462e9845f09SVincenzo Maffione }
1463e9845f09SVincenzo Maffione 
14649e117734SLeonid Bloch static bool e1000_full_mac_needed(void *opaque)
14659e117734SLeonid Bloch {
14669e117734SLeonid Bloch     E1000State *s = opaque;
14679e117734SLeonid Bloch 
1468bc0f0674SLeonid Bloch     return chkflag(MAC);
14699e117734SLeonid Bloch }
14709e117734SLeonid Bloch 
147146f2a9ecSDr. David Alan Gilbert static bool e1000_tso_state_needed(void *opaque)
147246f2a9ecSDr. David Alan Gilbert {
147346f2a9ecSDr. David Alan Gilbert     E1000State *s = opaque;
147446f2a9ecSDr. David Alan Gilbert 
147546f2a9ecSDr. David Alan Gilbert     return chkflag(TSO);
147646f2a9ecSDr. David Alan Gilbert }
147746f2a9ecSDr. David Alan Gilbert 
1478e9845f09SVincenzo Maffione static const VMStateDescription vmstate_e1000_mit_state = {
1479e9845f09SVincenzo Maffione     .name = "e1000/mit_state",
1480e9845f09SVincenzo Maffione     .version_id = 1,
1481e9845f09SVincenzo Maffione     .minimum_version_id = 1,
14825cd8cadaSJuan Quintela     .needed = e1000_mit_state_needed,
1483e9845f09SVincenzo Maffione     .fields = (VMStateField[]) {
1484e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[RDTR], E1000State),
1485e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[RADV], E1000State),
1486e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[TADV], E1000State),
1487e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[ITR], E1000State),
1488e9845f09SVincenzo Maffione         VMSTATE_BOOL(mit_irq_level, E1000State),
1489e9845f09SVincenzo Maffione         VMSTATE_END_OF_LIST()
1490e9845f09SVincenzo Maffione     }
1491e9845f09SVincenzo Maffione };
1492e9845f09SVincenzo Maffione 
14939e117734SLeonid Bloch static const VMStateDescription vmstate_e1000_full_mac_state = {
14949e117734SLeonid Bloch     .name = "e1000/full_mac_state",
14959e117734SLeonid Bloch     .version_id = 1,
14969e117734SLeonid Bloch     .minimum_version_id = 1,
14979e117734SLeonid Bloch     .needed = e1000_full_mac_needed,
14989e117734SLeonid Bloch     .fields = (VMStateField[]) {
14999e117734SLeonid Bloch         VMSTATE_UINT32_ARRAY(mac_reg, E1000State, 0x8000),
15009e117734SLeonid Bloch         VMSTATE_END_OF_LIST()
15019e117734SLeonid Bloch     }
15029e117734SLeonid Bloch };
15039e117734SLeonid Bloch 
15044ae4bf5bSDr. David Alan Gilbert static const VMStateDescription vmstate_e1000_tx_tso_state = {
15054ae4bf5bSDr. David Alan Gilbert     .name = "e1000/tx_tso_state",
15064ae4bf5bSDr. David Alan Gilbert     .version_id = 1,
15074ae4bf5bSDr. David Alan Gilbert     .minimum_version_id = 1,
150846f2a9ecSDr. David Alan Gilbert     .needed = e1000_tso_state_needed,
15093c4053c5SDr. David Alan Gilbert     .post_load = e1000_tx_tso_post_load,
15104ae4bf5bSDr. David Alan Gilbert     .fields = (VMStateField[]) {
15114ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT8(tx.tso_props.ipcss, E1000State),
15124ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT8(tx.tso_props.ipcso, E1000State),
15134ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT16(tx.tso_props.ipcse, E1000State),
15144ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT8(tx.tso_props.tucss, E1000State),
15154ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT8(tx.tso_props.tucso, E1000State),
15164ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT16(tx.tso_props.tucse, E1000State),
15174ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT32(tx.tso_props.paylen, E1000State),
15184ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT8(tx.tso_props.hdr_len, E1000State),
15194ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT16(tx.tso_props.mss, E1000State),
15204ae4bf5bSDr. David Alan Gilbert         VMSTATE_INT8(tx.tso_props.ip, E1000State),
15214ae4bf5bSDr. David Alan Gilbert         VMSTATE_INT8(tx.tso_props.tcp, E1000State),
15224ae4bf5bSDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
15234ae4bf5bSDr. David Alan Gilbert     }
15244ae4bf5bSDr. David Alan Gilbert };
15254ae4bf5bSDr. David Alan Gilbert 
1526e482dc3eSJuan Quintela static const VMStateDescription vmstate_e1000 = {
1527e482dc3eSJuan Quintela     .name = "e1000",
15284ae4bf5bSDr. David Alan Gilbert     .version_id = 2,
1529e482dc3eSJuan Quintela     .minimum_version_id = 1,
1530ddcb73b7SMichael S. Tsirkin     .pre_save = e1000_pre_save,
1531e4b82364SAmos Kong     .post_load = e1000_post_load,
1532e482dc3eSJuan Quintela     .fields = (VMStateField[]) {
1533b08340d5SAndreas Färber         VMSTATE_PCI_DEVICE(parent_obj, E1000State),
1534e482dc3eSJuan Quintela         VMSTATE_UNUSED_TEST(is_version_1, 4), /* was instance id */
1535e482dc3eSJuan Quintela         VMSTATE_UNUSED(4), /* Was mmio_base.  */
1536e482dc3eSJuan Quintela         VMSTATE_UINT32(rxbuf_size, E1000State),
1537e482dc3eSJuan Quintela         VMSTATE_UINT32(rxbuf_min_shift, E1000State),
1538e482dc3eSJuan Quintela         VMSTATE_UINT32(eecd_state.val_in, E1000State),
1539e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.bitnum_in, E1000State),
1540e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.bitnum_out, E1000State),
1541e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.reading, E1000State),
1542e482dc3eSJuan Quintela         VMSTATE_UINT32(eecd_state.old_eecd, E1000State),
154359354484SDr. David Alan Gilbert         VMSTATE_UINT8(mig_props.ipcss, E1000State),
154459354484SDr. David Alan Gilbert         VMSTATE_UINT8(mig_props.ipcso, E1000State),
154559354484SDr. David Alan Gilbert         VMSTATE_UINT16(mig_props.ipcse, E1000State),
154659354484SDr. David Alan Gilbert         VMSTATE_UINT8(mig_props.tucss, E1000State),
154759354484SDr. David Alan Gilbert         VMSTATE_UINT8(mig_props.tucso, E1000State),
154859354484SDr. David Alan Gilbert         VMSTATE_UINT16(mig_props.tucse, E1000State),
154959354484SDr. David Alan Gilbert         VMSTATE_UINT32(mig_props.paylen, E1000State),
155059354484SDr. David Alan Gilbert         VMSTATE_UINT8(mig_props.hdr_len, E1000State),
155159354484SDr. David Alan Gilbert         VMSTATE_UINT16(mig_props.mss, E1000State),
1552e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.size, E1000State),
1553e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.tso_frames, E1000State),
15547d08c73eSEd Swierk via Qemu-devel         VMSTATE_UINT8(tx.sum_needed, E1000State),
155559354484SDr. David Alan Gilbert         VMSTATE_INT8(mig_props.ip, E1000State),
155659354484SDr. David Alan Gilbert         VMSTATE_INT8(mig_props.tcp, E1000State),
1557e482dc3eSJuan Quintela         VMSTATE_BUFFER(tx.header, E1000State),
1558e482dc3eSJuan Quintela         VMSTATE_BUFFER(tx.data, E1000State),
1559e482dc3eSJuan Quintela         VMSTATE_UINT16_ARRAY(eeprom_data, E1000State, 64),
1560e482dc3eSJuan Quintela         VMSTATE_UINT16_ARRAY(phy_reg, E1000State, 0x20),
1561e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[CTRL], E1000State),
1562e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[EECD], E1000State),
1563e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[EERD], E1000State),
1564e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[GPRC], E1000State),
1565e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[GPTC], E1000State),
1566e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[ICR], E1000State),
1567e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[ICS], E1000State),
1568e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[IMC], E1000State),
1569e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[IMS], E1000State),
1570e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[LEDCTL], E1000State),
1571e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MANC], E1000State),
1572e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MDIC], E1000State),
1573e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MPC], E1000State),
1574e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[PBA], E1000State),
1575e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RCTL], E1000State),
1576e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDBAH], E1000State),
1577e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDBAL], E1000State),
1578e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDH], E1000State),
1579e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDLEN], E1000State),
1580e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDT], E1000State),
1581e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[STATUS], E1000State),
1582e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[SWSM], E1000State),
1583e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TCTL], E1000State),
1584e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDBAH], E1000State),
1585e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDBAL], E1000State),
1586e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDH], E1000State),
1587e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDLEN], E1000State),
1588e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDT], E1000State),
1589e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TORH], E1000State),
1590e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TORL], E1000State),
1591e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TOTH], E1000State),
1592e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TOTL], E1000State),
1593e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TPR], E1000State),
1594e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TPT], E1000State),
1595e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TXDCTL], E1000State),
1596e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[WUFC], E1000State),
1597e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[VET], E1000State),
1598e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, RA, 32),
1599e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, MTA, 128),
1600e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, VFTA, 128),
1601e482dc3eSJuan Quintela         VMSTATE_END_OF_LIST()
1602e9845f09SVincenzo Maffione     },
16035cd8cadaSJuan Quintela     .subsections = (const VMStateDescription*[]) {
16045cd8cadaSJuan Quintela         &vmstate_e1000_mit_state,
16059e117734SLeonid Bloch         &vmstate_e1000_full_mac_state,
16064ae4bf5bSDr. David Alan Gilbert         &vmstate_e1000_tx_tso_state,
16075cd8cadaSJuan Quintela         NULL
16087c23b892Sbalrog     }
1609e482dc3eSJuan Quintela };
16107c23b892Sbalrog 
16118597f2e1SGabriel L. Somlo /*
16128597f2e1SGabriel L. Somlo  * EEPROM contents documented in Tables 5-2 and 5-3, pp. 98-102.
161380867bdbSPhilippe Mathieu-Daudé  * Note: A valid DevId will be inserted during pci_e1000_realize().
16148597f2e1SGabriel L. Somlo  */
161588b4e9dbSblueswir1 static const uint16_t e1000_eeprom_template[64] = {
16167c23b892Sbalrog     0x0000, 0x0000, 0x0000, 0x0000,      0xffff, 0x0000,      0x0000, 0x0000,
16178597f2e1SGabriel L. Somlo     0x3000, 0x1000, 0x6403, 0 /*DevId*/, 0x8086, 0 /*DevId*/, 0x8086, 0x3040,
16187c23b892Sbalrog     0x0008, 0x2000, 0x7e14, 0x0048,      0x1000, 0x00d8,      0x0000, 0x2700,
16197c23b892Sbalrog     0x6cc9, 0x3150, 0x0722, 0x040b,      0x0984, 0x0000,      0xc000, 0x0706,
16207c23b892Sbalrog     0x1008, 0x0000, 0x0f04, 0x7fff,      0x4d01, 0xffff,      0xffff, 0xffff,
16217c23b892Sbalrog     0xffff, 0xffff, 0xffff, 0xffff,      0xffff, 0xffff,      0xffff, 0xffff,
16227c23b892Sbalrog     0x0100, 0x4000, 0x121c, 0xffff,      0xffff, 0xffff,      0xffff, 0xffff,
16237c23b892Sbalrog     0xffff, 0xffff, 0xffff, 0xffff,      0xffff, 0xffff,      0xffff, 0x0000,
16247c23b892Sbalrog };
16257c23b892Sbalrog 
16267c23b892Sbalrog /* PCI interface */
16277c23b892Sbalrog 
16287c23b892Sbalrog static void
1629ad00a9b9SAvi Kivity e1000_mmio_setup(E1000State *d)
16307c23b892Sbalrog {
1631f65ed4c1Saliguori     int i;
1632f65ed4c1Saliguori     const uint32_t excluded_regs[] = {
1633f65ed4c1Saliguori         E1000_MDIC, E1000_ICR, E1000_ICS, E1000_IMS,
1634f65ed4c1Saliguori         E1000_IMC, E1000_TCTL, E1000_TDT, PNPMMIO_SIZE
1635f65ed4c1Saliguori     };
1636f65ed4c1Saliguori 
1637eedfac6fSPaolo Bonzini     memory_region_init_io(&d->mmio, OBJECT(d), &e1000_mmio_ops, d,
1638eedfac6fSPaolo Bonzini                           "e1000-mmio", PNPMMIO_SIZE);
1639ad00a9b9SAvi Kivity     memory_region_add_coalescing(&d->mmio, 0, excluded_regs[0]);
1640f65ed4c1Saliguori     for (i = 0; excluded_regs[i] != PNPMMIO_SIZE; i++)
1641ad00a9b9SAvi Kivity         memory_region_add_coalescing(&d->mmio, excluded_regs[i] + 4,
1642ad00a9b9SAvi Kivity                                      excluded_regs[i+1] - excluded_regs[i] - 4);
1643eedfac6fSPaolo Bonzini     memory_region_init_io(&d->io, OBJECT(d), &e1000_io_ops, d, "e1000-io", IOPORT_SIZE);
16447c23b892Sbalrog }
16457c23b892Sbalrog 
1646b946a153Saliguori static void
16474b09be85Saliguori pci_e1000_uninit(PCIDevice *dev)
16484b09be85Saliguori {
1649567a3c9eSPeter Crosthwaite     E1000State *d = E1000(dev);
16504b09be85Saliguori 
1651bc72ad67SAlex Bligh     timer_del(d->autoneg_timer);
1652bc72ad67SAlex Bligh     timer_free(d->autoneg_timer);
1653e9845f09SVincenzo Maffione     timer_del(d->mit_timer);
1654e9845f09SVincenzo Maffione     timer_free(d->mit_timer);
1655157628d0Syuchenlin     timer_del(d->flush_queue_timer);
1656157628d0Syuchenlin     timer_free(d->flush_queue_timer);
1657948ecf21SJason Wang     qemu_del_nic(d->nic);
16584b09be85Saliguori }
16594b09be85Saliguori 
1660a03e2aecSMark McLoughlin static NetClientInfo net_e1000_info = {
1661f394b2e2SEric Blake     .type = NET_CLIENT_DRIVER_NIC,
1662a03e2aecSMark McLoughlin     .size = sizeof(NICState),
1663a03e2aecSMark McLoughlin     .can_receive = e1000_can_receive,
1664a03e2aecSMark McLoughlin     .receive = e1000_receive,
166597410ddeSVincenzo Maffione     .receive_iov = e1000_receive_iov,
1666a03e2aecSMark McLoughlin     .link_status_changed = e1000_set_link_status,
1667a03e2aecSMark McLoughlin };
1668a03e2aecSMark McLoughlin 
166920302e71SMichael S. Tsirkin static void e1000_write_config(PCIDevice *pci_dev, uint32_t address,
167020302e71SMichael S. Tsirkin                                 uint32_t val, int len)
167120302e71SMichael S. Tsirkin {
167220302e71SMichael S. Tsirkin     E1000State *s = E1000(pci_dev);
167320302e71SMichael S. Tsirkin 
167420302e71SMichael S. Tsirkin     pci_default_write_config(pci_dev, address, val, len);
167520302e71SMichael S. Tsirkin 
167620302e71SMichael S. Tsirkin     if (range_covers_byte(address, len, PCI_COMMAND) &&
167720302e71SMichael S. Tsirkin         (pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
167820302e71SMichael S. Tsirkin         qemu_flush_queued_packets(qemu_get_queue(s->nic));
167920302e71SMichael S. Tsirkin     }
168020302e71SMichael S. Tsirkin }
168120302e71SMichael S. Tsirkin 
16829af21dbeSMarkus Armbruster static void pci_e1000_realize(PCIDevice *pci_dev, Error **errp)
16837c23b892Sbalrog {
1684567a3c9eSPeter Crosthwaite     DeviceState *dev = DEVICE(pci_dev);
1685567a3c9eSPeter Crosthwaite     E1000State *d = E1000(pci_dev);
16867c23b892Sbalrog     uint8_t *pci_conf;
1687fbdaa002SGerd Hoffmann     uint8_t *macaddr;
1688aff427a1SChris Wright 
168920302e71SMichael S. Tsirkin     pci_dev->config_write = e1000_write_config;
169020302e71SMichael S. Tsirkin 
1691b08340d5SAndreas Färber     pci_conf = pci_dev->config;
16927c23b892Sbalrog 
1693a9cbacb0SMichael S. Tsirkin     /* TODO: RST# value should be 0, PCI spec 6.2.4 */
1694a9cbacb0SMichael S. Tsirkin     pci_conf[PCI_CACHE_LINE_SIZE] = 0x10;
16957c23b892Sbalrog 
1696817e0b6fSMichael S. Tsirkin     pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
16977c23b892Sbalrog 
1698ad00a9b9SAvi Kivity     e1000_mmio_setup(d);
16997c23b892Sbalrog 
1700b08340d5SAndreas Färber     pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
17017c23b892Sbalrog 
1702b08340d5SAndreas Färber     pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->io);
17037c23b892Sbalrog 
1704fbdaa002SGerd Hoffmann     qemu_macaddr_default_if_unset(&d->conf.macaddr);
1705fbdaa002SGerd Hoffmann     macaddr = d->conf.macaddr.a;
1706093454e2SDmitry Fleytman 
1707093454e2SDmitry Fleytman     e1000x_core_prepare_eeprom(d->eeprom_data,
1708093454e2SDmitry Fleytman                                e1000_eeprom_template,
1709093454e2SDmitry Fleytman                                sizeof(e1000_eeprom_template),
1710093454e2SDmitry Fleytman                                PCI_DEVICE_GET_CLASS(pci_dev)->device_id,
1711093454e2SDmitry Fleytman                                macaddr);
17127c23b892Sbalrog 
1713a03e2aecSMark McLoughlin     d->nic = qemu_new_nic(&net_e1000_info, &d->conf,
1714567a3c9eSPeter Crosthwaite                           object_get_typename(OBJECT(d)), dev->id, d);
17157c23b892Sbalrog 
1716b356f76dSJason Wang     qemu_format_nic_info_str(qemu_get_queue(d->nic), macaddr);
17171ca4d09aSGleb Natapov 
1718bc72ad67SAlex Bligh     d->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, e1000_autoneg_timer, d);
1719e9845f09SVincenzo Maffione     d->mit_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000_mit_timer, d);
1720157628d0Syuchenlin     d->flush_queue_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
1721157628d0Syuchenlin                                         e1000_flush_queue_timer, d);
17227c23b892Sbalrog }
17239d07d757SPaul Brook 
1724fbdaa002SGerd Hoffmann static void qdev_e1000_reset(DeviceState *dev)
1725fbdaa002SGerd Hoffmann {
1726567a3c9eSPeter Crosthwaite     E1000State *d = E1000(dev);
1727fbdaa002SGerd Hoffmann     e1000_reset(d);
1728fbdaa002SGerd Hoffmann }
1729fbdaa002SGerd Hoffmann 
173040021f08SAnthony Liguori static Property e1000_properties[] = {
1731fbdaa002SGerd Hoffmann     DEFINE_NIC_PROPERTIES(E1000State, conf),
17322af234e6SMichael S. Tsirkin     DEFINE_PROP_BIT("autonegotiation", E1000State,
17332af234e6SMichael S. Tsirkin                     compat_flags, E1000_FLAG_AUTONEG_BIT, true),
1734e9845f09SVincenzo Maffione     DEFINE_PROP_BIT("mitigation", E1000State,
1735e9845f09SVincenzo Maffione                     compat_flags, E1000_FLAG_MIT_BIT, true),
1736ba63ec85SLeonid Bloch     DEFINE_PROP_BIT("extra_mac_registers", E1000State,
1737ba63ec85SLeonid Bloch                     compat_flags, E1000_FLAG_MAC_BIT, true),
173846f2a9ecSDr. David Alan Gilbert     DEFINE_PROP_BIT("migrate_tso_props", E1000State,
173946f2a9ecSDr. David Alan Gilbert                     compat_flags, E1000_FLAG_TSO_BIT, true),
1740fbdaa002SGerd Hoffmann     DEFINE_PROP_END_OF_LIST(),
174140021f08SAnthony Liguori };
174240021f08SAnthony Liguori 
17438597f2e1SGabriel L. Somlo typedef struct E1000Info {
17448597f2e1SGabriel L. Somlo     const char *name;
17458597f2e1SGabriel L. Somlo     uint16_t   device_id;
17468597f2e1SGabriel L. Somlo     uint8_t    revision;
17478597f2e1SGabriel L. Somlo     uint16_t   phy_id2;
17488597f2e1SGabriel L. Somlo } E1000Info;
17498597f2e1SGabriel L. Somlo 
175040021f08SAnthony Liguori static void e1000_class_init(ObjectClass *klass, void *data)
175140021f08SAnthony Liguori {
175239bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
175340021f08SAnthony Liguori     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
17548597f2e1SGabriel L. Somlo     E1000BaseClass *e = E1000_DEVICE_CLASS(klass);
17558597f2e1SGabriel L. Somlo     const E1000Info *info = data;
175640021f08SAnthony Liguori 
17579af21dbeSMarkus Armbruster     k->realize = pci_e1000_realize;
175840021f08SAnthony Liguori     k->exit = pci_e1000_uninit;
1759c45e5b5bSGerd Hoffmann     k->romfile = "efi-e1000.rom";
176040021f08SAnthony Liguori     k->vendor_id = PCI_VENDOR_ID_INTEL;
17618597f2e1SGabriel L. Somlo     k->device_id = info->device_id;
17628597f2e1SGabriel L. Somlo     k->revision = info->revision;
17638597f2e1SGabriel L. Somlo     e->phy_id2 = info->phy_id2;
176440021f08SAnthony Liguori     k->class_id = PCI_CLASS_NETWORK_ETHERNET;
1765125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
176639bffca2SAnthony Liguori     dc->desc = "Intel Gigabit Ethernet";
176739bffca2SAnthony Liguori     dc->reset = qdev_e1000_reset;
176839bffca2SAnthony Liguori     dc->vmsd = &vmstate_e1000;
17694f67d30bSMarc-André Lureau     device_class_set_props(dc, e1000_properties);
1770fbdaa002SGerd Hoffmann }
177140021f08SAnthony Liguori 
17725df3bf62SGonglei static void e1000_instance_init(Object *obj)
17735df3bf62SGonglei {
17745df3bf62SGonglei     E1000State *n = E1000(obj);
17755df3bf62SGonglei     device_add_bootindex_property(obj, &n->conf.bootindex,
17765df3bf62SGonglei                                   "bootindex", "/ethernet-phy@0",
17775df3bf62SGonglei                                   DEVICE(n), NULL);
17785df3bf62SGonglei }
17795df3bf62SGonglei 
17808597f2e1SGabriel L. Somlo static const TypeInfo e1000_base_info = {
17818597f2e1SGabriel L. Somlo     .name          = TYPE_E1000_BASE,
178239bffca2SAnthony Liguori     .parent        = TYPE_PCI_DEVICE,
178339bffca2SAnthony Liguori     .instance_size = sizeof(E1000State),
17845df3bf62SGonglei     .instance_init = e1000_instance_init,
17858597f2e1SGabriel L. Somlo     .class_size    = sizeof(E1000BaseClass),
17868597f2e1SGabriel L. Somlo     .abstract      = true,
1787fd3b02c8SEduardo Habkost     .interfaces = (InterfaceInfo[]) {
1788fd3b02c8SEduardo Habkost         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1789fd3b02c8SEduardo Habkost         { },
1790fd3b02c8SEduardo Habkost     },
17918597f2e1SGabriel L. Somlo };
17928597f2e1SGabriel L. Somlo 
17938597f2e1SGabriel L. Somlo static const E1000Info e1000_devices[] = {
17948597f2e1SGabriel L. Somlo     {
179583044020SJason Wang         .name      = "e1000",
17968597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82540EM,
17978597f2e1SGabriel L. Somlo         .revision  = 0x03,
17988597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_8254xx_DEFAULT,
17998597f2e1SGabriel L. Somlo     },
18008597f2e1SGabriel L. Somlo     {
18018597f2e1SGabriel L. Somlo         .name      = "e1000-82544gc",
18028597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82544GC_COPPER,
18038597f2e1SGabriel L. Somlo         .revision  = 0x03,
18048597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_82544x,
18058597f2e1SGabriel L. Somlo     },
18068597f2e1SGabriel L. Somlo     {
18078597f2e1SGabriel L. Somlo         .name      = "e1000-82545em",
18088597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82545EM_COPPER,
18098597f2e1SGabriel L. Somlo         .revision  = 0x03,
18108597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_8254xx_DEFAULT,
18118597f2e1SGabriel L. Somlo     },
18128597f2e1SGabriel L. Somlo };
18138597f2e1SGabriel L. Somlo 
181483f7d43aSAndreas Färber static void e1000_register_types(void)
18159d07d757SPaul Brook {
18168597f2e1SGabriel L. Somlo     int i;
18178597f2e1SGabriel L. Somlo 
18188597f2e1SGabriel L. Somlo     type_register_static(&e1000_base_info);
18198597f2e1SGabriel L. Somlo     for (i = 0; i < ARRAY_SIZE(e1000_devices); i++) {
18208597f2e1SGabriel L. Somlo         const E1000Info *info = &e1000_devices[i];
18218597f2e1SGabriel L. Somlo         TypeInfo type_info = {};
18228597f2e1SGabriel L. Somlo 
18238597f2e1SGabriel L. Somlo         type_info.name = info->name;
18248597f2e1SGabriel L. Somlo         type_info.parent = TYPE_E1000_BASE;
18258597f2e1SGabriel L. Somlo         type_info.class_data = (void *)info;
18268597f2e1SGabriel L. Somlo         type_info.class_init = e1000_class_init;
18275df3bf62SGonglei         type_info.instance_init = e1000_instance_init;
18288597f2e1SGabriel L. Somlo 
18298597f2e1SGabriel L. Somlo         type_register(&type_info);
18308597f2e1SGabriel L. Somlo     }
18319d07d757SPaul Brook }
18329d07d757SPaul Brook 
183383f7d43aSAndreas Färber type_init(e1000_register_types)
1834