xref: /qemu/hw/net/e1000.c (revision ff214d427ec64d22b3d40b802efb96955c4b6b89)
17c23b892Sbalrog /*
27c23b892Sbalrog  * QEMU e1000 emulation
37c23b892Sbalrog  *
42758aa52SMichael S. Tsirkin  * Software developer's manual:
52758aa52SMichael S. Tsirkin  * http://download.intel.com/design/network/manuals/8254x_GBe_SDM.pdf
62758aa52SMichael S. Tsirkin  *
77c23b892Sbalrog  * Nir Peleg, Tutis Systems Ltd. for Qumranet Inc.
87c23b892Sbalrog  * Copyright (c) 2008 Qumranet
97c23b892Sbalrog  * Based on work done by:
107c23b892Sbalrog  * Copyright (c) 2007 Dan Aloni
117c23b892Sbalrog  * Copyright (c) 2004 Antony T Curtis
127c23b892Sbalrog  *
137c23b892Sbalrog  * This library is free software; you can redistribute it and/or
147c23b892Sbalrog  * modify it under the terms of the GNU Lesser General Public
157c23b892Sbalrog  * License as published by the Free Software Foundation; either
167c23b892Sbalrog  * version 2 of the License, or (at your option) any later version.
177c23b892Sbalrog  *
187c23b892Sbalrog  * This library is distributed in the hope that it will be useful,
197c23b892Sbalrog  * but WITHOUT ANY WARRANTY; without even the implied warranty of
207c23b892Sbalrog  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
217c23b892Sbalrog  * Lesser General Public License for more details.
227c23b892Sbalrog  *
237c23b892Sbalrog  * You should have received a copy of the GNU Lesser General Public
248167ee88SBlue Swirl  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
257c23b892Sbalrog  */
267c23b892Sbalrog 
277c23b892Sbalrog 
28e8d40465SPeter Maydell #include "qemu/osdep.h"
2983c9f4caSPaolo Bonzini #include "hw/hw.h"
3083c9f4caSPaolo Bonzini #include "hw/pci/pci.h"
311422e32dSPaolo Bonzini #include "net/net.h"
327200ac3cSMark McLoughlin #include "net/checksum.h"
339c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
349c17d615SPaolo Bonzini #include "sysemu/dma.h"
3597410ddeSVincenzo Maffione #include "qemu/iov.h"
3620302e71SMichael S. Tsirkin #include "qemu/range.h"
377c23b892Sbalrog 
38093454e2SDmitry Fleytman #include "e1000x_common.h"
397c23b892Sbalrog 
403b274301SLeonid Bloch static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
413b274301SLeonid Bloch 
42b4053c64SJason Wang /* #define E1000_DEBUG */
437c23b892Sbalrog 
4427124888SJes Sorensen #ifdef E1000_DEBUG
457c23b892Sbalrog enum {
467c23b892Sbalrog     DEBUG_GENERAL,      DEBUG_IO,       DEBUG_MMIO,     DEBUG_INTERRUPT,
477c23b892Sbalrog     DEBUG_RX,           DEBUG_TX,       DEBUG_MDIC,     DEBUG_EEPROM,
487c23b892Sbalrog     DEBUG_UNKNOWN,      DEBUG_TXSUM,    DEBUG_TXERR,    DEBUG_RXERR,
49f9c1cdf4SJason Wang     DEBUG_RXFILTER,     DEBUG_PHY,      DEBUG_NOTYET,
507c23b892Sbalrog };
517c23b892Sbalrog #define DBGBIT(x)    (1<<DEBUG_##x)
527c23b892Sbalrog static int debugflags = DBGBIT(TXERR) | DBGBIT(GENERAL);
537c23b892Sbalrog 
546c7f4b47SBlue Swirl #define DBGOUT(what, fmt, ...) do { \
557c23b892Sbalrog     if (debugflags & DBGBIT(what)) \
566c7f4b47SBlue Swirl         fprintf(stderr, "e1000: " fmt, ## __VA_ARGS__); \
577c23b892Sbalrog     } while (0)
587c23b892Sbalrog #else
596c7f4b47SBlue Swirl #define DBGOUT(what, fmt, ...) do {} while (0)
607c23b892Sbalrog #endif
617c23b892Sbalrog 
627c23b892Sbalrog #define IOPORT_SIZE       0x40
63e94bbefeSaurel32 #define PNPMMIO_SIZE      0x20000
6478aeb23eSStefan Hajnoczi #define MIN_BUF_SIZE      60 /* Min. octets in an ethernet frame sans FCS */
657c23b892Sbalrog 
6697410ddeSVincenzo Maffione #define MAXIMUM_ETHERNET_HDR_LEN (14+4)
6797410ddeSVincenzo Maffione 
687c23b892Sbalrog /*
697c23b892Sbalrog  * HW models:
708597f2e1SGabriel L. Somlo  *  E1000_DEV_ID_82540EM works with Windows, Linux, and OS X <= 10.8
717c23b892Sbalrog  *  E1000_DEV_ID_82544GC_COPPER appears to work; not well tested
728597f2e1SGabriel L. Somlo  *  E1000_DEV_ID_82545EM_COPPER works with Linux and OS X >= 10.6
737c23b892Sbalrog  *  Others never tested
747c23b892Sbalrog  */
757c23b892Sbalrog 
767c23b892Sbalrog typedef struct E1000State_st {
77b08340d5SAndreas Färber     /*< private >*/
78b08340d5SAndreas Färber     PCIDevice parent_obj;
79b08340d5SAndreas Färber     /*< public >*/
80b08340d5SAndreas Färber 
81a03e2aecSMark McLoughlin     NICState *nic;
82fbdaa002SGerd Hoffmann     NICConf conf;
83ad00a9b9SAvi Kivity     MemoryRegion mmio;
84ad00a9b9SAvi Kivity     MemoryRegion io;
857c23b892Sbalrog 
867c23b892Sbalrog     uint32_t mac_reg[0x8000];
877c23b892Sbalrog     uint16_t phy_reg[0x20];
887c23b892Sbalrog     uint16_t eeprom_data[64];
897c23b892Sbalrog 
907c23b892Sbalrog     uint32_t rxbuf_size;
917c23b892Sbalrog     uint32_t rxbuf_min_shift;
927c23b892Sbalrog     struct e1000_tx {
937c23b892Sbalrog         unsigned char header[256];
948f2e8d1fSaliguori         unsigned char vlan_header[4];
95b10fec9bSStefan Weil         /* Fields vlan and data must not be reordered or separated. */
968f2e8d1fSaliguori         unsigned char vlan[4];
977c23b892Sbalrog         unsigned char data[0x10000];
987c23b892Sbalrog         uint16_t size;
998f2e8d1fSaliguori         unsigned char vlan_needed;
1007d08c73eSEd Swierk via Qemu-devel         unsigned char sum_needed;
1017d08c73eSEd Swierk via Qemu-devel         bool cptse;
102093454e2SDmitry Fleytman         e1000x_txd_props props;
103d62644b4SEd Swierk via Qemu-devel         e1000x_txd_props tso_props;
1047c23b892Sbalrog         uint16_t tso_frames;
1057c23b892Sbalrog     } tx;
1067c23b892Sbalrog 
1077c23b892Sbalrog     struct {
10820f3e863SLeonid Bloch         uint32_t val_in;    /* shifted in from guest driver */
1097c23b892Sbalrog         uint16_t bitnum_in;
1107c23b892Sbalrog         uint16_t bitnum_out;
1117c23b892Sbalrog         uint16_t reading;
1127c23b892Sbalrog         uint32_t old_eecd;
1137c23b892Sbalrog     } eecd_state;
114b9d03e35SJason Wang 
115b9d03e35SJason Wang     QEMUTimer *autoneg_timer;
1162af234e6SMichael S. Tsirkin 
117e9845f09SVincenzo Maffione     QEMUTimer *mit_timer;      /* Mitigation timer. */
118e9845f09SVincenzo Maffione     bool mit_timer_on;         /* Mitigation timer is running. */
119e9845f09SVincenzo Maffione     bool mit_irq_level;        /* Tracks interrupt pin level. */
120e9845f09SVincenzo Maffione     uint32_t mit_ide;          /* Tracks E1000_TXD_CMD_IDE bit. */
121e9845f09SVincenzo Maffione 
1222af234e6SMichael S. Tsirkin /* Compatibility flags for migration to/from qemu 1.3.0 and older */
1232af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG_BIT 0
124e9845f09SVincenzo Maffione #define E1000_FLAG_MIT_BIT 1
1259e117734SLeonid Bloch #define E1000_FLAG_MAC_BIT 2
12646f2a9ecSDr. David Alan Gilbert #define E1000_FLAG_TSO_BIT 3
1272af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG (1 << E1000_FLAG_AUTONEG_BIT)
128e9845f09SVincenzo Maffione #define E1000_FLAG_MIT (1 << E1000_FLAG_MIT_BIT)
1299e117734SLeonid Bloch #define E1000_FLAG_MAC (1 << E1000_FLAG_MAC_BIT)
13046f2a9ecSDr. David Alan Gilbert #define E1000_FLAG_TSO (1 << E1000_FLAG_TSO_BIT)
1312af234e6SMichael S. Tsirkin     uint32_t compat_flags;
1323c4053c5SDr. David Alan Gilbert     bool received_tx_tso;
133*ff214d42SDr. David Alan Gilbert     bool use_tso_for_migration;
13459354484SDr. David Alan Gilbert     e1000x_txd_props mig_props;
1357c23b892Sbalrog } E1000State;
1367c23b892Sbalrog 
137bc0f0674SLeonid Bloch #define chkflag(x)     (s->compat_flags & E1000_FLAG_##x)
138bc0f0674SLeonid Bloch 
1398597f2e1SGabriel L. Somlo typedef struct E1000BaseClass {
1408597f2e1SGabriel L. Somlo     PCIDeviceClass parent_class;
1418597f2e1SGabriel L. Somlo     uint16_t phy_id2;
1428597f2e1SGabriel L. Somlo } E1000BaseClass;
1438597f2e1SGabriel L. Somlo 
1448597f2e1SGabriel L. Somlo #define TYPE_E1000_BASE "e1000-base"
145567a3c9eSPeter Crosthwaite 
146567a3c9eSPeter Crosthwaite #define E1000(obj) \
1478597f2e1SGabriel L. Somlo     OBJECT_CHECK(E1000State, (obj), TYPE_E1000_BASE)
1488597f2e1SGabriel L. Somlo 
1498597f2e1SGabriel L. Somlo #define E1000_DEVICE_CLASS(klass) \
1508597f2e1SGabriel L. Somlo      OBJECT_CLASS_CHECK(E1000BaseClass, (klass), TYPE_E1000_BASE)
1518597f2e1SGabriel L. Somlo #define E1000_DEVICE_GET_CLASS(obj) \
1528597f2e1SGabriel L. Somlo     OBJECT_GET_CLASS(E1000BaseClass, (obj), TYPE_E1000_BASE)
153567a3c9eSPeter Crosthwaite 
15471aadd3cSJason Wang static void
15571aadd3cSJason Wang e1000_link_up(E1000State *s)
15671aadd3cSJason Wang {
157093454e2SDmitry Fleytman     e1000x_update_regs_on_link_up(s->mac_reg, s->phy_reg);
158093454e2SDmitry Fleytman 
159093454e2SDmitry Fleytman     /* E1000_STATUS_LU is tested by e1000_can_receive() */
160093454e2SDmitry Fleytman     qemu_flush_queued_packets(qemu_get_queue(s->nic));
161093454e2SDmitry Fleytman }
162093454e2SDmitry Fleytman 
163093454e2SDmitry Fleytman static void
164093454e2SDmitry Fleytman e1000_autoneg_done(E1000State *s)
165093454e2SDmitry Fleytman {
166093454e2SDmitry Fleytman     e1000x_update_regs_on_autoneg_done(s->mac_reg, s->phy_reg);
1675df6a185SStefan Hajnoczi 
1685df6a185SStefan Hajnoczi     /* E1000_STATUS_LU is tested by e1000_can_receive() */
1695df6a185SStefan Hajnoczi     qemu_flush_queued_packets(qemu_get_queue(s->nic));
17071aadd3cSJason Wang }
17171aadd3cSJason Wang 
1721195fed9SGabriel L. Somlo static bool
1731195fed9SGabriel L. Somlo have_autoneg(E1000State *s)
1741195fed9SGabriel L. Somlo {
175bc0f0674SLeonid Bloch     return chkflag(AUTONEG) && (s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN);
1761195fed9SGabriel L. Somlo }
1771195fed9SGabriel L. Somlo 
178b9d03e35SJason Wang static void
179b9d03e35SJason Wang set_phy_ctrl(E1000State *s, int index, uint16_t val)
180b9d03e35SJason Wang {
1811195fed9SGabriel L. Somlo     /* bits 0-5 reserved; MII_CR_[RESTART_AUTO_NEG,RESET] are self clearing */
1821195fed9SGabriel L. Somlo     s->phy_reg[PHY_CTRL] = val & ~(0x3f |
1831195fed9SGabriel L. Somlo                                    MII_CR_RESET |
1841195fed9SGabriel L. Somlo                                    MII_CR_RESTART_AUTO_NEG);
1851195fed9SGabriel L. Somlo 
1862af234e6SMichael S. Tsirkin     /*
1872af234e6SMichael S. Tsirkin      * QEMU 1.3 does not support link auto-negotiation emulation, so if we
1882af234e6SMichael S. Tsirkin      * migrate during auto negotiation, after migration the link will be
1892af234e6SMichael S. Tsirkin      * down.
1902af234e6SMichael S. Tsirkin      */
1911195fed9SGabriel L. Somlo     if (have_autoneg(s) && (val & MII_CR_RESTART_AUTO_NEG)) {
192093454e2SDmitry Fleytman         e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer);
193b9d03e35SJason Wang     }
194b9d03e35SJason Wang }
195b9d03e35SJason Wang 
196b9d03e35SJason Wang static void (*phyreg_writeops[])(E1000State *, int, uint16_t) = {
197b9d03e35SJason Wang     [PHY_CTRL] = set_phy_ctrl,
198b9d03e35SJason Wang };
199b9d03e35SJason Wang 
200b9d03e35SJason Wang enum { NPHYWRITEOPS = ARRAY_SIZE(phyreg_writeops) };
201b9d03e35SJason Wang 
2027c23b892Sbalrog enum { PHY_R = 1, PHY_W = 2, PHY_RW = PHY_R | PHY_W };
20388b4e9dbSblueswir1 static const char phy_regcap[0x20] = {
2047c23b892Sbalrog     [PHY_STATUS]      = PHY_R,     [M88E1000_EXT_PHY_SPEC_CTRL] = PHY_RW,
2057c23b892Sbalrog     [PHY_ID1]         = PHY_R,     [M88E1000_PHY_SPEC_CTRL]     = PHY_RW,
2067c23b892Sbalrog     [PHY_CTRL]        = PHY_RW,    [PHY_1000T_CTRL]             = PHY_RW,
2077c23b892Sbalrog     [PHY_LP_ABILITY]  = PHY_R,     [PHY_1000T_STATUS]           = PHY_R,
2087c23b892Sbalrog     [PHY_AUTONEG_ADV] = PHY_RW,    [M88E1000_RX_ERR_CNTR]       = PHY_R,
2096883b591SGabriel L. Somlo     [PHY_ID2]         = PHY_R,     [M88E1000_PHY_SPEC_STATUS]   = PHY_R,
2106883b591SGabriel L. Somlo     [PHY_AUTONEG_EXP] = PHY_R,
2117c23b892Sbalrog };
2127c23b892Sbalrog 
2138597f2e1SGabriel L. Somlo /* PHY_ID2 documented in 8254x_GBe_SDM.pdf, pp. 250 */
214814cd3acSMichael S. Tsirkin static const uint16_t phy_reg_init[] = {
2159616c290SGabriel L. Somlo     [PHY_CTRL]   = MII_CR_SPEED_SELECT_MSB |
2169616c290SGabriel L. Somlo                    MII_CR_FULL_DUPLEX |
2179616c290SGabriel L. Somlo                    MII_CR_AUTO_NEG_EN,
2189616c290SGabriel L. Somlo 
2199616c290SGabriel L. Somlo     [PHY_STATUS] = MII_SR_EXTENDED_CAPS |
2209616c290SGabriel L. Somlo                    MII_SR_LINK_STATUS |   /* link initially up */
2219616c290SGabriel L. Somlo                    MII_SR_AUTONEG_CAPS |
2229616c290SGabriel L. Somlo                    /* MII_SR_AUTONEG_COMPLETE: initially NOT completed */
2239616c290SGabriel L. Somlo                    MII_SR_PREAMBLE_SUPPRESS |
2249616c290SGabriel L. Somlo                    MII_SR_EXTENDED_STATUS |
2259616c290SGabriel L. Somlo                    MII_SR_10T_HD_CAPS |
2269616c290SGabriel L. Somlo                    MII_SR_10T_FD_CAPS |
2279616c290SGabriel L. Somlo                    MII_SR_100X_HD_CAPS |
2289616c290SGabriel L. Somlo                    MII_SR_100X_FD_CAPS,
2299616c290SGabriel L. Somlo 
2309616c290SGabriel L. Somlo     [PHY_ID1] = 0x141,
2319616c290SGabriel L. Somlo     /* [PHY_ID2] configured per DevId, from e1000_reset() */
2329616c290SGabriel L. Somlo     [PHY_AUTONEG_ADV] = 0xde1,
2339616c290SGabriel L. Somlo     [PHY_LP_ABILITY] = 0x1e0,
2349616c290SGabriel L. Somlo     [PHY_1000T_CTRL] = 0x0e00,
2359616c290SGabriel L. Somlo     [PHY_1000T_STATUS] = 0x3c00,
2369616c290SGabriel L. Somlo     [M88E1000_PHY_SPEC_CTRL] = 0x360,
237814cd3acSMichael S. Tsirkin     [M88E1000_PHY_SPEC_STATUS] = 0xac00,
2389616c290SGabriel L. Somlo     [M88E1000_EXT_PHY_SPEC_CTRL] = 0x0d60,
239814cd3acSMichael S. Tsirkin };
240814cd3acSMichael S. Tsirkin 
241814cd3acSMichael S. Tsirkin static const uint32_t mac_reg_init[] = {
242814cd3acSMichael S. Tsirkin     [PBA]     = 0x00100030,
243814cd3acSMichael S. Tsirkin     [LEDCTL]  = 0x602,
244814cd3acSMichael S. Tsirkin     [CTRL]    = E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN0 |
245814cd3acSMichael S. Tsirkin                 E1000_CTRL_SPD_1000 | E1000_CTRL_SLU,
246814cd3acSMichael S. Tsirkin     [STATUS]  = 0x80000000 | E1000_STATUS_GIO_MASTER_ENABLE |
247814cd3acSMichael S. Tsirkin                 E1000_STATUS_ASDV | E1000_STATUS_MTXCKOK |
248814cd3acSMichael S. Tsirkin                 E1000_STATUS_SPEED_1000 | E1000_STATUS_FD |
249814cd3acSMichael S. Tsirkin                 E1000_STATUS_LU,
250814cd3acSMichael S. Tsirkin     [MANC]    = E1000_MANC_EN_MNG2HOST | E1000_MANC_RCV_TCO_EN |
251814cd3acSMichael S. Tsirkin                 E1000_MANC_ARP_EN | E1000_MANC_0298_EN |
252814cd3acSMichael S. Tsirkin                 E1000_MANC_RMCP_EN,
253814cd3acSMichael S. Tsirkin };
254814cd3acSMichael S. Tsirkin 
255e9845f09SVincenzo Maffione /* Helper function, *curr == 0 means the value is not set */
256e9845f09SVincenzo Maffione static inline void
257e9845f09SVincenzo Maffione mit_update_delay(uint32_t *curr, uint32_t value)
258e9845f09SVincenzo Maffione {
259e9845f09SVincenzo Maffione     if (value && (*curr == 0 || value < *curr)) {
260e9845f09SVincenzo Maffione         *curr = value;
261e9845f09SVincenzo Maffione     }
262e9845f09SVincenzo Maffione }
263e9845f09SVincenzo Maffione 
2647c23b892Sbalrog static void
2657c23b892Sbalrog set_interrupt_cause(E1000State *s, int index, uint32_t val)
2667c23b892Sbalrog {
267b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
268e9845f09SVincenzo Maffione     uint32_t pending_ints;
269e9845f09SVincenzo Maffione     uint32_t mit_delay;
270b08340d5SAndreas Färber 
2717c23b892Sbalrog     s->mac_reg[ICR] = val;
272a52a8841SMichael S. Tsirkin 
273a52a8841SMichael S. Tsirkin     /*
274a52a8841SMichael S. Tsirkin      * Make sure ICR and ICS registers have the same value.
275a52a8841SMichael S. Tsirkin      * The spec says that the ICS register is write-only.  However in practice,
276a52a8841SMichael S. Tsirkin      * on real hardware ICS is readable, and for reads it has the same value as
277a52a8841SMichael S. Tsirkin      * ICR (except that ICS does not have the clear on read behaviour of ICR).
278a52a8841SMichael S. Tsirkin      *
279a52a8841SMichael S. Tsirkin      * The VxWorks PRO/1000 driver uses this behaviour.
280a52a8841SMichael S. Tsirkin      */
281b1332393SBill Paul     s->mac_reg[ICS] = val;
282a52a8841SMichael S. Tsirkin 
283e9845f09SVincenzo Maffione     pending_ints = (s->mac_reg[IMS] & s->mac_reg[ICR]);
284e9845f09SVincenzo Maffione     if (!s->mit_irq_level && pending_ints) {
285e9845f09SVincenzo Maffione         /*
286e9845f09SVincenzo Maffione          * Here we detect a potential raising edge. We postpone raising the
287e9845f09SVincenzo Maffione          * interrupt line if we are inside the mitigation delay window
288e9845f09SVincenzo Maffione          * (s->mit_timer_on == 1).
289e9845f09SVincenzo Maffione          * We provide a partial implementation of interrupt mitigation,
290e9845f09SVincenzo Maffione          * emulating only RADV, TADV and ITR (lower 16 bits, 1024ns units for
291e9845f09SVincenzo Maffione          * RADV and TADV, 256ns units for ITR). RDTR is only used to enable
292e9845f09SVincenzo Maffione          * RADV; relative timers based on TIDV and RDTR are not implemented.
293e9845f09SVincenzo Maffione          */
294e9845f09SVincenzo Maffione         if (s->mit_timer_on) {
295e9845f09SVincenzo Maffione             return;
296e9845f09SVincenzo Maffione         }
297bc0f0674SLeonid Bloch         if (chkflag(MIT)) {
298e9845f09SVincenzo Maffione             /* Compute the next mitigation delay according to pending
299e9845f09SVincenzo Maffione              * interrupts and the current values of RADV (provided
300e9845f09SVincenzo Maffione              * RDTR!=0), TADV and ITR.
301e9845f09SVincenzo Maffione              * Then rearm the timer.
302e9845f09SVincenzo Maffione              */
303e9845f09SVincenzo Maffione             mit_delay = 0;
304e9845f09SVincenzo Maffione             if (s->mit_ide &&
305e9845f09SVincenzo Maffione                     (pending_ints & (E1000_ICR_TXQE | E1000_ICR_TXDW))) {
306e9845f09SVincenzo Maffione                 mit_update_delay(&mit_delay, s->mac_reg[TADV] * 4);
307e9845f09SVincenzo Maffione             }
308e9845f09SVincenzo Maffione             if (s->mac_reg[RDTR] && (pending_ints & E1000_ICS_RXT0)) {
309e9845f09SVincenzo Maffione                 mit_update_delay(&mit_delay, s->mac_reg[RADV] * 4);
310e9845f09SVincenzo Maffione             }
311e9845f09SVincenzo Maffione             mit_update_delay(&mit_delay, s->mac_reg[ITR]);
312e9845f09SVincenzo Maffione 
31374004e8cSSameeh Jubran             /*
31474004e8cSSameeh Jubran              * According to e1000 SPEC, the Ethernet controller guarantees
31574004e8cSSameeh Jubran              * a maximum observable interrupt rate of 7813 interrupts/sec.
31674004e8cSSameeh Jubran              * Thus if mit_delay < 500 then the delay should be set to the
31774004e8cSSameeh Jubran              * minimum delay possible which is 500.
31874004e8cSSameeh Jubran              */
31974004e8cSSameeh Jubran             mit_delay = (mit_delay < 500) ? 500 : mit_delay;
32074004e8cSSameeh Jubran 
321e9845f09SVincenzo Maffione             s->mit_timer_on = 1;
322e9845f09SVincenzo Maffione             timer_mod(s->mit_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
323e9845f09SVincenzo Maffione                       mit_delay * 256);
324e9845f09SVincenzo Maffione             s->mit_ide = 0;
325e9845f09SVincenzo Maffione         }
326e9845f09SVincenzo Maffione     }
327e9845f09SVincenzo Maffione 
328e9845f09SVincenzo Maffione     s->mit_irq_level = (pending_ints != 0);
3299e64f8a3SMarcel Apfelbaum     pci_set_irq(d, s->mit_irq_level);
330e9845f09SVincenzo Maffione }
331e9845f09SVincenzo Maffione 
332e9845f09SVincenzo Maffione static void
333e9845f09SVincenzo Maffione e1000_mit_timer(void *opaque)
334e9845f09SVincenzo Maffione {
335e9845f09SVincenzo Maffione     E1000State *s = opaque;
336e9845f09SVincenzo Maffione 
337e9845f09SVincenzo Maffione     s->mit_timer_on = 0;
338e9845f09SVincenzo Maffione     /* Call set_interrupt_cause to update the irq level (if necessary). */
339e9845f09SVincenzo Maffione     set_interrupt_cause(s, 0, s->mac_reg[ICR]);
3407c23b892Sbalrog }
3417c23b892Sbalrog 
3427c23b892Sbalrog static void
3437c23b892Sbalrog set_ics(E1000State *s, int index, uint32_t val)
3447c23b892Sbalrog {
3457c23b892Sbalrog     DBGOUT(INTERRUPT, "set_ics %x, ICR %x, IMR %x\n", val, s->mac_reg[ICR],
3467c23b892Sbalrog         s->mac_reg[IMS]);
3477c23b892Sbalrog     set_interrupt_cause(s, 0, val | s->mac_reg[ICR]);
3487c23b892Sbalrog }
3497c23b892Sbalrog 
350d52aec95SGabriel L. Somlo static void
351d52aec95SGabriel L. Somlo e1000_autoneg_timer(void *opaque)
352d52aec95SGabriel L. Somlo {
353d52aec95SGabriel L. Somlo     E1000State *s = opaque;
354d52aec95SGabriel L. Somlo     if (!qemu_get_queue(s->nic)->link_down) {
355093454e2SDmitry Fleytman         e1000_autoneg_done(s);
356d52aec95SGabriel L. Somlo         set_ics(s, 0, E1000_ICS_LSC); /* signal link status change to guest */
357d52aec95SGabriel L. Somlo     }
358d52aec95SGabriel L. Somlo }
359d52aec95SGabriel L. Somlo 
360814cd3acSMichael S. Tsirkin static void e1000_reset(void *opaque)
361814cd3acSMichael S. Tsirkin {
362814cd3acSMichael S. Tsirkin     E1000State *d = opaque;
3638597f2e1SGabriel L. Somlo     E1000BaseClass *edc = E1000_DEVICE_GET_CLASS(d);
364372254c6SGabriel L. Somlo     uint8_t *macaddr = d->conf.macaddr.a;
365814cd3acSMichael S. Tsirkin 
366bc72ad67SAlex Bligh     timer_del(d->autoneg_timer);
367e9845f09SVincenzo Maffione     timer_del(d->mit_timer);
368e9845f09SVincenzo Maffione     d->mit_timer_on = 0;
369e9845f09SVincenzo Maffione     d->mit_irq_level = 0;
370e9845f09SVincenzo Maffione     d->mit_ide = 0;
371814cd3acSMichael S. Tsirkin     memset(d->phy_reg, 0, sizeof d->phy_reg);
372814cd3acSMichael S. Tsirkin     memmove(d->phy_reg, phy_reg_init, sizeof phy_reg_init);
3738597f2e1SGabriel L. Somlo     d->phy_reg[PHY_ID2] = edc->phy_id2;
374814cd3acSMichael S. Tsirkin     memset(d->mac_reg, 0, sizeof d->mac_reg);
375814cd3acSMichael S. Tsirkin     memmove(d->mac_reg, mac_reg_init, sizeof mac_reg_init);
376814cd3acSMichael S. Tsirkin     d->rxbuf_min_shift = 1;
377814cd3acSMichael S. Tsirkin     memset(&d->tx, 0, sizeof d->tx);
378814cd3acSMichael S. Tsirkin 
379b356f76dSJason Wang     if (qemu_get_queue(d->nic)->link_down) {
380093454e2SDmitry Fleytman         e1000x_update_regs_on_link_down(d->mac_reg, d->phy_reg);
381814cd3acSMichael S. Tsirkin     }
382372254c6SGabriel L. Somlo 
383093454e2SDmitry Fleytman     e1000x_reset_mac_addr(d->nic, d->mac_reg, macaddr);
384814cd3acSMichael S. Tsirkin }
385814cd3acSMichael S. Tsirkin 
3867c23b892Sbalrog static void
387cab3c825SKevin Wolf set_ctrl(E1000State *s, int index, uint32_t val)
388cab3c825SKevin Wolf {
389cab3c825SKevin Wolf     /* RST is self clearing */
390cab3c825SKevin Wolf     s->mac_reg[CTRL] = val & ~E1000_CTRL_RST;
391cab3c825SKevin Wolf }
392cab3c825SKevin Wolf 
393cab3c825SKevin Wolf static void
3947c23b892Sbalrog set_rx_control(E1000State *s, int index, uint32_t val)
3957c23b892Sbalrog {
3967c23b892Sbalrog     s->mac_reg[RCTL] = val;
397093454e2SDmitry Fleytman     s->rxbuf_size = e1000x_rxbufsize(val);
3987c23b892Sbalrog     s->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1;
3997c23b892Sbalrog     DBGOUT(RX, "RCTL: %d, mac_reg[RCTL] = 0x%x\n", s->mac_reg[RDT],
4007c23b892Sbalrog            s->mac_reg[RCTL]);
401b356f76dSJason Wang     qemu_flush_queued_packets(qemu_get_queue(s->nic));
4027c23b892Sbalrog }
4037c23b892Sbalrog 
4047c23b892Sbalrog static void
4057c23b892Sbalrog set_mdic(E1000State *s, int index, uint32_t val)
4067c23b892Sbalrog {
4077c23b892Sbalrog     uint32_t data = val & E1000_MDIC_DATA_MASK;
4087c23b892Sbalrog     uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT);
4097c23b892Sbalrog 
4107c23b892Sbalrog     if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) // phy #
4117c23b892Sbalrog         val = s->mac_reg[MDIC] | E1000_MDIC_ERROR;
4127c23b892Sbalrog     else if (val & E1000_MDIC_OP_READ) {
4137c23b892Sbalrog         DBGOUT(MDIC, "MDIC read reg 0x%x\n", addr);
4147c23b892Sbalrog         if (!(phy_regcap[addr] & PHY_R)) {
4157c23b892Sbalrog             DBGOUT(MDIC, "MDIC read reg %x unhandled\n", addr);
4167c23b892Sbalrog             val |= E1000_MDIC_ERROR;
4177c23b892Sbalrog         } else
4187c23b892Sbalrog             val = (val ^ data) | s->phy_reg[addr];
4197c23b892Sbalrog     } else if (val & E1000_MDIC_OP_WRITE) {
4207c23b892Sbalrog         DBGOUT(MDIC, "MDIC write reg 0x%x, value 0x%x\n", addr, data);
4217c23b892Sbalrog         if (!(phy_regcap[addr] & PHY_W)) {
4227c23b892Sbalrog             DBGOUT(MDIC, "MDIC write reg %x unhandled\n", addr);
4237c23b892Sbalrog             val |= E1000_MDIC_ERROR;
424b9d03e35SJason Wang         } else {
425b9d03e35SJason Wang             if (addr < NPHYWRITEOPS && phyreg_writeops[addr]) {
426b9d03e35SJason Wang                 phyreg_writeops[addr](s, index, data);
4271195fed9SGabriel L. Somlo             } else {
4287c23b892Sbalrog                 s->phy_reg[addr] = data;
4297c23b892Sbalrog             }
430b9d03e35SJason Wang         }
4311195fed9SGabriel L. Somlo     }
4327c23b892Sbalrog     s->mac_reg[MDIC] = val | E1000_MDIC_READY;
43317fbbb0bSJason Wang 
43417fbbb0bSJason Wang     if (val & E1000_MDIC_INT_EN) {
4357c23b892Sbalrog         set_ics(s, 0, E1000_ICR_MDAC);
4367c23b892Sbalrog     }
43717fbbb0bSJason Wang }
4387c23b892Sbalrog 
4397c23b892Sbalrog static uint32_t
4407c23b892Sbalrog get_eecd(E1000State *s, int index)
4417c23b892Sbalrog {
4427c23b892Sbalrog     uint32_t ret = E1000_EECD_PRES|E1000_EECD_GNT | s->eecd_state.old_eecd;
4437c23b892Sbalrog 
4447c23b892Sbalrog     DBGOUT(EEPROM, "reading eeprom bit %d (reading %d)\n",
4457c23b892Sbalrog            s->eecd_state.bitnum_out, s->eecd_state.reading);
4467c23b892Sbalrog     if (!s->eecd_state.reading ||
4477c23b892Sbalrog         ((s->eeprom_data[(s->eecd_state.bitnum_out >> 4) & 0x3f] >>
4487c23b892Sbalrog           ((s->eecd_state.bitnum_out & 0xf) ^ 0xf))) & 1)
4497c23b892Sbalrog         ret |= E1000_EECD_DO;
4507c23b892Sbalrog     return ret;
4517c23b892Sbalrog }
4527c23b892Sbalrog 
4537c23b892Sbalrog static void
4547c23b892Sbalrog set_eecd(E1000State *s, int index, uint32_t val)
4557c23b892Sbalrog {
4567c23b892Sbalrog     uint32_t oldval = s->eecd_state.old_eecd;
4577c23b892Sbalrog 
4587c23b892Sbalrog     s->eecd_state.old_eecd = val & (E1000_EECD_SK | E1000_EECD_CS |
4597c23b892Sbalrog             E1000_EECD_DI|E1000_EECD_FWE_MASK|E1000_EECD_REQ);
46020f3e863SLeonid Bloch     if (!(E1000_EECD_CS & val)) {            /* CS inactive; nothing to do */
4619651ac55SIzumi Tsutsui         return;
46220f3e863SLeonid Bloch     }
46320f3e863SLeonid Bloch     if (E1000_EECD_CS & (val ^ oldval)) {    /* CS rise edge; reset state */
4649651ac55SIzumi Tsutsui         s->eecd_state.val_in = 0;
4659651ac55SIzumi Tsutsui         s->eecd_state.bitnum_in = 0;
4669651ac55SIzumi Tsutsui         s->eecd_state.bitnum_out = 0;
4679651ac55SIzumi Tsutsui         s->eecd_state.reading = 0;
4689651ac55SIzumi Tsutsui     }
46920f3e863SLeonid Bloch     if (!(E1000_EECD_SK & (val ^ oldval))) {    /* no clock edge */
4707c23b892Sbalrog         return;
47120f3e863SLeonid Bloch     }
47220f3e863SLeonid Bloch     if (!(E1000_EECD_SK & val)) {               /* falling edge */
4737c23b892Sbalrog         s->eecd_state.bitnum_out++;
4747c23b892Sbalrog         return;
4757c23b892Sbalrog     }
4767c23b892Sbalrog     s->eecd_state.val_in <<= 1;
4777c23b892Sbalrog     if (val & E1000_EECD_DI)
4787c23b892Sbalrog         s->eecd_state.val_in |= 1;
4797c23b892Sbalrog     if (++s->eecd_state.bitnum_in == 9 && !s->eecd_state.reading) {
4807c23b892Sbalrog         s->eecd_state.bitnum_out = ((s->eecd_state.val_in & 0x3f)<<4)-1;
4817c23b892Sbalrog         s->eecd_state.reading = (((s->eecd_state.val_in >> 6) & 7) ==
4827c23b892Sbalrog             EEPROM_READ_OPCODE_MICROWIRE);
4837c23b892Sbalrog     }
4847c23b892Sbalrog     DBGOUT(EEPROM, "eeprom bitnum in %d out %d, reading %d\n",
4857c23b892Sbalrog            s->eecd_state.bitnum_in, s->eecd_state.bitnum_out,
4867c23b892Sbalrog            s->eecd_state.reading);
4877c23b892Sbalrog }
4887c23b892Sbalrog 
4897c23b892Sbalrog static uint32_t
4907c23b892Sbalrog flash_eerd_read(E1000State *s, int x)
4917c23b892Sbalrog {
4927c23b892Sbalrog     unsigned int index, r = s->mac_reg[EERD] & ~E1000_EEPROM_RW_REG_START;
4937c23b892Sbalrog 
494b1332393SBill Paul     if ((s->mac_reg[EERD] & E1000_EEPROM_RW_REG_START) == 0)
495b1332393SBill Paul         return (s->mac_reg[EERD]);
496b1332393SBill Paul 
4977c23b892Sbalrog     if ((index = r >> E1000_EEPROM_RW_ADDR_SHIFT) > EEPROM_CHECKSUM_REG)
498b1332393SBill Paul         return (E1000_EEPROM_RW_REG_DONE | r);
499b1332393SBill Paul 
500b1332393SBill Paul     return ((s->eeprom_data[index] << E1000_EEPROM_RW_REG_DATA) |
501b1332393SBill Paul            E1000_EEPROM_RW_REG_DONE | r);
5027c23b892Sbalrog }
5037c23b892Sbalrog 
5047c23b892Sbalrog static void
5057c23b892Sbalrog putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t css, uint32_t cse)
5067c23b892Sbalrog {
507c6a6a5e3Saliguori     uint32_t sum;
508c6a6a5e3Saliguori 
5097c23b892Sbalrog     if (cse && cse < n)
5107c23b892Sbalrog         n = cse + 1;
511c6a6a5e3Saliguori     if (sloc < n-1) {
512c6a6a5e3Saliguori         sum = net_checksum_add(n-css, data+css);
5130dacea92SEd Swierk         stw_be_p(data + sloc, net_checksum_finish_nozero(sum));
514c6a6a5e3Saliguori     }
5157c23b892Sbalrog }
5167c23b892Sbalrog 
5171f67f92cSLeonid Bloch static inline void
5183b274301SLeonid Bloch inc_tx_bcast_or_mcast_count(E1000State *s, const unsigned char *arr)
5193b274301SLeonid Bloch {
5203b274301SLeonid Bloch     if (!memcmp(arr, bcast, sizeof bcast)) {
521093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, BPTC);
5223b274301SLeonid Bloch     } else if (arr[0] & 1) {
523093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, MPTC);
5243b274301SLeonid Bloch     }
5253b274301SLeonid Bloch }
5263b274301SLeonid Bloch 
52745e93764SLeonid Bloch static void
52893e37d76SJason Wang e1000_send_packet(E1000State *s, const uint8_t *buf, int size)
52993e37d76SJason Wang {
5303b274301SLeonid Bloch     static const int PTCregs[6] = { PTC64, PTC127, PTC255, PTC511,
5313b274301SLeonid Bloch                                     PTC1023, PTC1522 };
5323b274301SLeonid Bloch 
533b356f76dSJason Wang     NetClientState *nc = qemu_get_queue(s->nic);
53493e37d76SJason Wang     if (s->phy_reg[PHY_CTRL] & MII_CR_LOOPBACK) {
535b356f76dSJason Wang         nc->info->receive(nc, buf, size);
53693e37d76SJason Wang     } else {
537b356f76dSJason Wang         qemu_send_packet(nc, buf, size);
53893e37d76SJason Wang     }
5393b274301SLeonid Bloch     inc_tx_bcast_or_mcast_count(s, buf);
540093454e2SDmitry Fleytman     e1000x_increase_size_stats(s->mac_reg, PTCregs, size);
54193e37d76SJason Wang }
54293e37d76SJason Wang 
54393e37d76SJason Wang static void
5447c23b892Sbalrog xmit_seg(E1000State *s)
5457c23b892Sbalrog {
54614e60aaeSPeter Maydell     uint16_t len;
54745e93764SLeonid Bloch     unsigned int frames = s->tx.tso_frames, css, sofar;
5487c23b892Sbalrog     struct e1000_tx *tp = &s->tx;
549d62644b4SEd Swierk via Qemu-devel     struct e1000x_txd_props *props = tp->cptse ? &tp->tso_props : &tp->props;
5507c23b892Sbalrog 
551d62644b4SEd Swierk via Qemu-devel     if (tp->cptse) {
552d62644b4SEd Swierk via Qemu-devel         css = props->ipcss;
5537c23b892Sbalrog         DBGOUT(TXSUM, "frames %d size %d ipcss %d\n",
5547c23b892Sbalrog                frames, tp->size, css);
555d62644b4SEd Swierk via Qemu-devel         if (props->ip) {    /* IPv4 */
556d8ee2591SPeter Maydell             stw_be_p(tp->data+css+2, tp->size - css);
557d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4,
55814e60aaeSPeter Maydell                      lduw_be_p(tp->data + css + 4) + frames);
55920f3e863SLeonid Bloch         } else {         /* IPv6 */
560d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4, tp->size - css);
56120f3e863SLeonid Bloch         }
562d62644b4SEd Swierk via Qemu-devel         css = props->tucss;
5637c23b892Sbalrog         len = tp->size - css;
564d62644b4SEd Swierk via Qemu-devel         DBGOUT(TXSUM, "tcp %d tucss %d len %d\n", props->tcp, css, len);
565d62644b4SEd Swierk via Qemu-devel         if (props->tcp) {
566d62644b4SEd Swierk via Qemu-devel             sofar = frames * props->mss;
5676bd194abSPeter Maydell             stl_be_p(tp->data+css+4, ldl_be_p(tp->data+css+4)+sofar); /* seq */
568d62644b4SEd Swierk via Qemu-devel             if (props->paylen - sofar > props->mss) {
56920f3e863SLeonid Bloch                 tp->data[css + 13] &= ~9;    /* PSH, FIN */
5703b274301SLeonid Bloch             } else if (frames) {
571093454e2SDmitry Fleytman                 e1000x_inc_reg_if_not_full(s->mac_reg, TSCTC);
5723b274301SLeonid Bloch             }
573d62644b4SEd Swierk via Qemu-devel         } else {    /* UDP */
574d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4, len);
575d62644b4SEd Swierk via Qemu-devel         }
5767d08c73eSEd Swierk via Qemu-devel         if (tp->sum_needed & E1000_TXD_POPTS_TXSM) {
577e685b4ebSAlex Williamson             unsigned int phsum;
5787c23b892Sbalrog             // add pseudo-header length before checksum calculation
579d62644b4SEd Swierk via Qemu-devel             void *sp = tp->data + props->tucso;
58014e60aaeSPeter Maydell 
58114e60aaeSPeter Maydell             phsum = lduw_be_p(sp) + len;
582e685b4ebSAlex Williamson             phsum = (phsum >> 16) + (phsum & 0xffff);
583d8ee2591SPeter Maydell             stw_be_p(sp, phsum);
5847c23b892Sbalrog         }
5857c23b892Sbalrog         tp->tso_frames++;
5867c23b892Sbalrog     }
5877c23b892Sbalrog 
5887d08c73eSEd Swierk via Qemu-devel     if (tp->sum_needed & E1000_TXD_POPTS_TXSM) {
589d62644b4SEd Swierk via Qemu-devel         putsum(tp->data, tp->size, props->tucso, props->tucss, props->tucse);
590093454e2SDmitry Fleytman     }
5917d08c73eSEd Swierk via Qemu-devel     if (tp->sum_needed & E1000_TXD_POPTS_IXSM) {
592d62644b4SEd Swierk via Qemu-devel         putsum(tp->data, tp->size, props->ipcso, props->ipcss, props->ipcse);
593093454e2SDmitry Fleytman     }
5948f2e8d1fSaliguori     if (tp->vlan_needed) {
595b10fec9bSStefan Weil         memmove(tp->vlan, tp->data, 4);
596b10fec9bSStefan Weil         memmove(tp->data, tp->data + 4, 8);
5978f2e8d1fSaliguori         memcpy(tp->data + 8, tp->vlan_header, 4);
59893e37d76SJason Wang         e1000_send_packet(s, tp->vlan, tp->size + 4);
59920f3e863SLeonid Bloch     } else {
60093e37d76SJason Wang         e1000_send_packet(s, tp->data, tp->size);
60120f3e863SLeonid Bloch     }
60220f3e863SLeonid Bloch 
603093454e2SDmitry Fleytman     e1000x_inc_reg_if_not_full(s->mac_reg, TPT);
604093454e2SDmitry Fleytman     e1000x_grow_8reg_if_not_full(s->mac_reg, TOTL, s->tx.size);
6051f67f92cSLeonid Bloch     s->mac_reg[GPTC] = s->mac_reg[TPT];
6063b274301SLeonid Bloch     s->mac_reg[GOTCL] = s->mac_reg[TOTL];
6073b274301SLeonid Bloch     s->mac_reg[GOTCH] = s->mac_reg[TOTH];
6087c23b892Sbalrog }
6097c23b892Sbalrog 
6107c23b892Sbalrog static void
6117c23b892Sbalrog process_tx_desc(E1000State *s, struct e1000_tx_desc *dp)
6127c23b892Sbalrog {
613b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
6147c23b892Sbalrog     uint32_t txd_lower = le32_to_cpu(dp->lower.data);
6157c23b892Sbalrog     uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D);
616093454e2SDmitry Fleytman     unsigned int split_size = txd_lower & 0xffff, bytes, sz;
617a0ae17a6SAndrew Jones     unsigned int msh = 0xfffff;
6187c23b892Sbalrog     uint64_t addr;
6197c23b892Sbalrog     struct e1000_context_desc *xp = (struct e1000_context_desc *)dp;
6207c23b892Sbalrog     struct e1000_tx *tp = &s->tx;
6217c23b892Sbalrog 
622e9845f09SVincenzo Maffione     s->mit_ide |= (txd_lower & E1000_TXD_CMD_IDE);
62320f3e863SLeonid Bloch     if (dtype == E1000_TXD_CMD_DEXT) {    /* context descriptor */
624d62644b4SEd Swierk via Qemu-devel         if (le32_to_cpu(xp->cmd_and_length) & E1000_TXD_CMD_TSE) {
625d62644b4SEd Swierk via Qemu-devel             e1000x_read_tx_ctx_descr(xp, &tp->tso_props);
626*ff214d42SDr. David Alan Gilbert             s->use_tso_for_migration = 1;
6277c23b892Sbalrog             tp->tso_frames = 0;
628d62644b4SEd Swierk via Qemu-devel         } else {
629d62644b4SEd Swierk via Qemu-devel             e1000x_read_tx_ctx_descr(xp, &tp->props);
630*ff214d42SDr. David Alan Gilbert             s->use_tso_for_migration = 0;
6317c23b892Sbalrog         }
6327c23b892Sbalrog         return;
6331b0009dbSbalrog     } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) {
6341b0009dbSbalrog         // data descriptor
635735e77ecSStefan Hajnoczi         if (tp->size == 0) {
6367d08c73eSEd Swierk via Qemu-devel             tp->sum_needed = le32_to_cpu(dp->upper.data) >> 8;
637735e77ecSStefan Hajnoczi         }
6387d08c73eSEd Swierk via Qemu-devel         tp->cptse = (txd_lower & E1000_TXD_CMD_TSE) ? 1 : 0;
63943ad7e3eSJes Sorensen     } else {
6401b0009dbSbalrog         // legacy descriptor
6417d08c73eSEd Swierk via Qemu-devel         tp->cptse = 0;
64243ad7e3eSJes Sorensen     }
6437c23b892Sbalrog 
644093454e2SDmitry Fleytman     if (e1000x_vlan_enabled(s->mac_reg) &&
645093454e2SDmitry Fleytman         e1000x_is_vlan_txd(txd_lower) &&
6467d08c73eSEd Swierk via Qemu-devel         (tp->cptse || txd_lower & E1000_TXD_CMD_EOP)) {
6478f2e8d1fSaliguori         tp->vlan_needed = 1;
648d8ee2591SPeter Maydell         stw_be_p(tp->vlan_header,
6494e60a250SShannon Zhao                       le16_to_cpu(s->mac_reg[VET]));
650d8ee2591SPeter Maydell         stw_be_p(tp->vlan_header + 2,
6518f2e8d1fSaliguori                       le16_to_cpu(dp->upper.fields.special));
6528f2e8d1fSaliguori     }
6538f2e8d1fSaliguori 
6547c23b892Sbalrog     addr = le64_to_cpu(dp->buffer_addr);
655d62644b4SEd Swierk via Qemu-devel     if (tp->cptse) {
656d62644b4SEd Swierk via Qemu-devel         msh = tp->tso_props.hdr_len + tp->tso_props.mss;
6577c23b892Sbalrog         do {
6587c23b892Sbalrog             bytes = split_size;
6597c23b892Sbalrog             if (tp->size + bytes > msh)
6607c23b892Sbalrog                 bytes = msh - tp->size;
66165f82df0SAnthony Liguori 
66265f82df0SAnthony Liguori             bytes = MIN(sizeof(tp->data) - tp->size, bytes);
663b08340d5SAndreas Färber             pci_dma_read(d, addr, tp->data + tp->size, bytes);
664a0ae17a6SAndrew Jones             sz = tp->size + bytes;
665d62644b4SEd Swierk via Qemu-devel             if (sz >= tp->tso_props.hdr_len
666d62644b4SEd Swierk via Qemu-devel                 && tp->size < tp->tso_props.hdr_len) {
667d62644b4SEd Swierk via Qemu-devel                 memmove(tp->header, tp->data, tp->tso_props.hdr_len);
668a0ae17a6SAndrew Jones             }
6697c23b892Sbalrog             tp->size = sz;
6707c23b892Sbalrog             addr += bytes;
6717c23b892Sbalrog             if (sz == msh) {
6727c23b892Sbalrog                 xmit_seg(s);
673d62644b4SEd Swierk via Qemu-devel                 memmove(tp->data, tp->header, tp->tso_props.hdr_len);
674d62644b4SEd Swierk via Qemu-devel                 tp->size = tp->tso_props.hdr_len;
6757c23b892Sbalrog             }
676b947ac2bSP J P             split_size -= bytes;
677b947ac2bSP J P         } while (bytes && split_size);
6781b0009dbSbalrog     } else {
67965f82df0SAnthony Liguori         split_size = MIN(sizeof(tp->data) - tp->size, split_size);
680b08340d5SAndreas Färber         pci_dma_read(d, addr, tp->data + tp->size, split_size);
6811b0009dbSbalrog         tp->size += split_size;
6821b0009dbSbalrog     }
6837c23b892Sbalrog 
6847c23b892Sbalrog     if (!(txd_lower & E1000_TXD_CMD_EOP))
6857c23b892Sbalrog         return;
686d62644b4SEd Swierk via Qemu-devel     if (!(tp->cptse && tp->size < tp->tso_props.hdr_len)) {
6877c23b892Sbalrog         xmit_seg(s);
688a0ae17a6SAndrew Jones     }
6897c23b892Sbalrog     tp->tso_frames = 0;
6907d08c73eSEd Swierk via Qemu-devel     tp->sum_needed = 0;
6918f2e8d1fSaliguori     tp->vlan_needed = 0;
6927c23b892Sbalrog     tp->size = 0;
6937d08c73eSEd Swierk via Qemu-devel     tp->cptse = 0;
6947c23b892Sbalrog }
6957c23b892Sbalrog 
6967c23b892Sbalrog static uint32_t
69762ecbd35SEduard - Gabriel Munteanu txdesc_writeback(E1000State *s, dma_addr_t base, struct e1000_tx_desc *dp)
6987c23b892Sbalrog {
699b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
7007c23b892Sbalrog     uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data);
7017c23b892Sbalrog 
7027c23b892Sbalrog     if (!(txd_lower & (E1000_TXD_CMD_RS|E1000_TXD_CMD_RPS)))
7037c23b892Sbalrog         return 0;
7047c23b892Sbalrog     txd_upper = (le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD) &
7057c23b892Sbalrog                 ~(E1000_TXD_STAT_EC | E1000_TXD_STAT_LC | E1000_TXD_STAT_TU);
7067c23b892Sbalrog     dp->upper.data = cpu_to_le32(txd_upper);
707b08340d5SAndreas Färber     pci_dma_write(d, base + ((char *)&dp->upper - (char *)dp),
70800c3a05bSDavid Gibson                   &dp->upper, sizeof(dp->upper));
7097c23b892Sbalrog     return E1000_ICR_TXDW;
7107c23b892Sbalrog }
7117c23b892Sbalrog 
712d17161f6SKevin Wolf static uint64_t tx_desc_base(E1000State *s)
713d17161f6SKevin Wolf {
714d17161f6SKevin Wolf     uint64_t bah = s->mac_reg[TDBAH];
715d17161f6SKevin Wolf     uint64_t bal = s->mac_reg[TDBAL] & ~0xf;
716d17161f6SKevin Wolf 
717d17161f6SKevin Wolf     return (bah << 32) + bal;
718d17161f6SKevin Wolf }
719d17161f6SKevin Wolf 
7207c23b892Sbalrog static void
7217c23b892Sbalrog start_xmit(E1000State *s)
7227c23b892Sbalrog {
723b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
72462ecbd35SEduard - Gabriel Munteanu     dma_addr_t base;
7257c23b892Sbalrog     struct e1000_tx_desc desc;
7267c23b892Sbalrog     uint32_t tdh_start = s->mac_reg[TDH], cause = E1000_ICS_TXQE;
7277c23b892Sbalrog 
7287c23b892Sbalrog     if (!(s->mac_reg[TCTL] & E1000_TCTL_EN)) {
7297c23b892Sbalrog         DBGOUT(TX, "tx disabled\n");
7307c23b892Sbalrog         return;
7317c23b892Sbalrog     }
7327c23b892Sbalrog 
7337c23b892Sbalrog     while (s->mac_reg[TDH] != s->mac_reg[TDT]) {
734d17161f6SKevin Wolf         base = tx_desc_base(s) +
7357c23b892Sbalrog                sizeof(struct e1000_tx_desc) * s->mac_reg[TDH];
736b08340d5SAndreas Färber         pci_dma_read(d, base, &desc, sizeof(desc));
7377c23b892Sbalrog 
7387c23b892Sbalrog         DBGOUT(TX, "index %d: %p : %x %x\n", s->mac_reg[TDH],
7396106075bSths                (void *)(intptr_t)desc.buffer_addr, desc.lower.data,
7407c23b892Sbalrog                desc.upper.data);
7417c23b892Sbalrog 
7427c23b892Sbalrog         process_tx_desc(s, &desc);
74362ecbd35SEduard - Gabriel Munteanu         cause |= txdesc_writeback(s, base, &desc);
7447c23b892Sbalrog 
7457c23b892Sbalrog         if (++s->mac_reg[TDH] * sizeof(desc) >= s->mac_reg[TDLEN])
7467c23b892Sbalrog             s->mac_reg[TDH] = 0;
7477c23b892Sbalrog         /*
7487c23b892Sbalrog          * the following could happen only if guest sw assigns
7497c23b892Sbalrog          * bogus values to TDT/TDLEN.
7507c23b892Sbalrog          * there's nothing too intelligent we could do about this.
7517c23b892Sbalrog          */
752dd793a74SLaszlo Ersek         if (s->mac_reg[TDH] == tdh_start ||
753dd793a74SLaszlo Ersek             tdh_start >= s->mac_reg[TDLEN] / sizeof(desc)) {
7547c23b892Sbalrog             DBGOUT(TXERR, "TDH wraparound @%x, TDT %x, TDLEN %x\n",
7557c23b892Sbalrog                    tdh_start, s->mac_reg[TDT], s->mac_reg[TDLEN]);
7567c23b892Sbalrog             break;
7577c23b892Sbalrog         }
7587c23b892Sbalrog     }
7597c23b892Sbalrog     set_ics(s, 0, cause);
7607c23b892Sbalrog }
7617c23b892Sbalrog 
7627c23b892Sbalrog static int
7637c23b892Sbalrog receive_filter(E1000State *s, const uint8_t *buf, int size)
7647c23b892Sbalrog {
765093454e2SDmitry Fleytman     uint32_t rctl = s->mac_reg[RCTL];
7664aeea330SLeonid Bloch     int isbcast = !memcmp(buf, bcast, sizeof bcast), ismcast = (buf[0] & 1);
7677c23b892Sbalrog 
768093454e2SDmitry Fleytman     if (e1000x_is_vlan_packet(buf, le16_to_cpu(s->mac_reg[VET])) &&
769093454e2SDmitry Fleytman         e1000x_vlan_rx_filter_enabled(s->mac_reg)) {
77014e60aaeSPeter Maydell         uint16_t vid = lduw_be_p(buf + 14);
77114e60aaeSPeter Maydell         uint32_t vfta = ldl_le_p((uint32_t*)(s->mac_reg + VFTA) +
7728f2e8d1fSaliguori                                  ((vid >> 5) & 0x7f));
7738f2e8d1fSaliguori         if ((vfta & (1 << (vid & 0x1f))) == 0)
7748f2e8d1fSaliguori             return 0;
7758f2e8d1fSaliguori     }
7768f2e8d1fSaliguori 
7774aeea330SLeonid Bloch     if (!isbcast && !ismcast && (rctl & E1000_RCTL_UPE)) { /* promiscuous ucast */
7787c23b892Sbalrog         return 1;
7794aeea330SLeonid Bloch     }
7807c23b892Sbalrog 
7814aeea330SLeonid Bloch     if (ismcast && (rctl & E1000_RCTL_MPE)) {          /* promiscuous mcast */
782093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, MPRC);
7837c23b892Sbalrog         return 1;
7844aeea330SLeonid Bloch     }
7857c23b892Sbalrog 
7864aeea330SLeonid Bloch     if (isbcast && (rctl & E1000_RCTL_BAM)) {          /* broadcast enabled */
787093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, BPRC);
7887c23b892Sbalrog         return 1;
7894aeea330SLeonid Bloch     }
7907c23b892Sbalrog 
791093454e2SDmitry Fleytman     return e1000x_rx_group_filter(s->mac_reg, buf);
7927c23b892Sbalrog }
7937c23b892Sbalrog 
79499ed7e30Saliguori static void
7954e68f7a0SStefan Hajnoczi e1000_set_link_status(NetClientState *nc)
79699ed7e30Saliguori {
797cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
79899ed7e30Saliguori     uint32_t old_status = s->mac_reg[STATUS];
79999ed7e30Saliguori 
800d4044c2aSBjørn Mork     if (nc->link_down) {
801093454e2SDmitry Fleytman         e1000x_update_regs_on_link_down(s->mac_reg, s->phy_reg);
802d4044c2aSBjørn Mork     } else {
803d7a41552SGabriel L. Somlo         if (have_autoneg(s) &&
8046a2acedbSGabriel L. Somlo             !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
805093454e2SDmitry Fleytman             e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer);
8066a2acedbSGabriel L. Somlo         } else {
80771aadd3cSJason Wang             e1000_link_up(s);
808d4044c2aSBjørn Mork         }
8096a2acedbSGabriel L. Somlo     }
81099ed7e30Saliguori 
81199ed7e30Saliguori     if (s->mac_reg[STATUS] != old_status)
81299ed7e30Saliguori         set_ics(s, 0, E1000_ICR_LSC);
81399ed7e30Saliguori }
81499ed7e30Saliguori 
815322fd48aSMichael S. Tsirkin static bool e1000_has_rxbufs(E1000State *s, size_t total_size)
816322fd48aSMichael S. Tsirkin {
817322fd48aSMichael S. Tsirkin     int bufs;
818322fd48aSMichael S. Tsirkin     /* Fast-path short packets */
819322fd48aSMichael S. Tsirkin     if (total_size <= s->rxbuf_size) {
820e5b8b0d4SDmitry Fleytman         return s->mac_reg[RDH] != s->mac_reg[RDT];
821322fd48aSMichael S. Tsirkin     }
822322fd48aSMichael S. Tsirkin     if (s->mac_reg[RDH] < s->mac_reg[RDT]) {
823322fd48aSMichael S. Tsirkin         bufs = s->mac_reg[RDT] - s->mac_reg[RDH];
824e5b8b0d4SDmitry Fleytman     } else if (s->mac_reg[RDH] > s->mac_reg[RDT]) {
825322fd48aSMichael S. Tsirkin         bufs = s->mac_reg[RDLEN] /  sizeof(struct e1000_rx_desc) +
826322fd48aSMichael S. Tsirkin             s->mac_reg[RDT] - s->mac_reg[RDH];
827322fd48aSMichael S. Tsirkin     } else {
828322fd48aSMichael S. Tsirkin         return false;
829322fd48aSMichael S. Tsirkin     }
830322fd48aSMichael S. Tsirkin     return total_size <= bufs * s->rxbuf_size;
831322fd48aSMichael S. Tsirkin }
832322fd48aSMichael S. Tsirkin 
8336cdfab28SMichael S. Tsirkin static int
8344e68f7a0SStefan Hajnoczi e1000_can_receive(NetClientState *nc)
8356cdfab28SMichael S. Tsirkin {
836cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
8376cdfab28SMichael S. Tsirkin 
838093454e2SDmitry Fleytman     return e1000x_rx_ready(&s->parent_obj, s->mac_reg) &&
83920302e71SMichael S. Tsirkin         e1000_has_rxbufs(s, 1);
8406cdfab28SMichael S. Tsirkin }
8416cdfab28SMichael S. Tsirkin 
842d17161f6SKevin Wolf static uint64_t rx_desc_base(E1000State *s)
843d17161f6SKevin Wolf {
844d17161f6SKevin Wolf     uint64_t bah = s->mac_reg[RDBAH];
845d17161f6SKevin Wolf     uint64_t bal = s->mac_reg[RDBAL] & ~0xf;
846d17161f6SKevin Wolf 
847d17161f6SKevin Wolf     return (bah << 32) + bal;
848d17161f6SKevin Wolf }
849d17161f6SKevin Wolf 
8504f1c942bSMark McLoughlin static ssize_t
85197410ddeSVincenzo Maffione e1000_receive_iov(NetClientState *nc, const struct iovec *iov, int iovcnt)
8527c23b892Sbalrog {
853cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
854b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
8557c23b892Sbalrog     struct e1000_rx_desc desc;
85662ecbd35SEduard - Gabriel Munteanu     dma_addr_t base;
8577c23b892Sbalrog     unsigned int n, rdt;
8587c23b892Sbalrog     uint32_t rdh_start;
8598f2e8d1fSaliguori     uint16_t vlan_special = 0;
86097410ddeSVincenzo Maffione     uint8_t vlan_status = 0;
86178aeb23eSStefan Hajnoczi     uint8_t min_buf[MIN_BUF_SIZE];
86297410ddeSVincenzo Maffione     struct iovec min_iov;
86397410ddeSVincenzo Maffione     uint8_t *filter_buf = iov->iov_base;
86497410ddeSVincenzo Maffione     size_t size = iov_size(iov, iovcnt);
86597410ddeSVincenzo Maffione     size_t iov_ofs = 0;
866b19487e2SMichael S. Tsirkin     size_t desc_offset;
867b19487e2SMichael S. Tsirkin     size_t desc_size;
868b19487e2SMichael S. Tsirkin     size_t total_size;
8697c23b892Sbalrog 
870093454e2SDmitry Fleytman     if (!e1000x_hw_rx_enabled(s->mac_reg)) {
871ddcb73b7SMichael S. Tsirkin         return -1;
872ddcb73b7SMichael S. Tsirkin     }
8737c23b892Sbalrog 
87478aeb23eSStefan Hajnoczi     /* Pad to minimum Ethernet frame length */
87578aeb23eSStefan Hajnoczi     if (size < sizeof(min_buf)) {
87697410ddeSVincenzo Maffione         iov_to_buf(iov, iovcnt, 0, min_buf, size);
87778aeb23eSStefan Hajnoczi         memset(&min_buf[size], 0, sizeof(min_buf) - size);
878093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, RUC);
87997410ddeSVincenzo Maffione         min_iov.iov_base = filter_buf = min_buf;
88097410ddeSVincenzo Maffione         min_iov.iov_len = size = sizeof(min_buf);
88197410ddeSVincenzo Maffione         iovcnt = 1;
88297410ddeSVincenzo Maffione         iov = &min_iov;
88397410ddeSVincenzo Maffione     } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) {
88497410ddeSVincenzo Maffione         /* This is very unlikely, but may happen. */
88597410ddeSVincenzo Maffione         iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN);
88697410ddeSVincenzo Maffione         filter_buf = min_buf;
88778aeb23eSStefan Hajnoczi     }
88878aeb23eSStefan Hajnoczi 
889b0d9ffcdSMichael Contreras     /* Discard oversized packets if !LPE and !SBP. */
890093454e2SDmitry Fleytman     if (e1000x_is_oversized(s->mac_reg, size)) {
891b0d9ffcdSMichael Contreras         return size;
892b0d9ffcdSMichael Contreras     }
893b0d9ffcdSMichael Contreras 
89497410ddeSVincenzo Maffione     if (!receive_filter(s, filter_buf, size)) {
8954f1c942bSMark McLoughlin         return size;
89697410ddeSVincenzo Maffione     }
8977c23b892Sbalrog 
898093454e2SDmitry Fleytman     if (e1000x_vlan_enabled(s->mac_reg) &&
899093454e2SDmitry Fleytman         e1000x_is_vlan_packet(filter_buf, le16_to_cpu(s->mac_reg[VET]))) {
90014e60aaeSPeter Maydell         vlan_special = cpu_to_le16(lduw_be_p(filter_buf + 14));
90197410ddeSVincenzo Maffione         iov_ofs = 4;
90297410ddeSVincenzo Maffione         if (filter_buf == iov->iov_base) {
90397410ddeSVincenzo Maffione             memmove(filter_buf + 4, filter_buf, 12);
90497410ddeSVincenzo Maffione         } else {
90597410ddeSVincenzo Maffione             iov_from_buf(iov, iovcnt, 4, filter_buf, 12);
90697410ddeSVincenzo Maffione             while (iov->iov_len <= iov_ofs) {
90797410ddeSVincenzo Maffione                 iov_ofs -= iov->iov_len;
90897410ddeSVincenzo Maffione                 iov++;
90997410ddeSVincenzo Maffione             }
91097410ddeSVincenzo Maffione         }
9118f2e8d1fSaliguori         vlan_status = E1000_RXD_STAT_VP;
9128f2e8d1fSaliguori         size -= 4;
9138f2e8d1fSaliguori     }
9148f2e8d1fSaliguori 
9157c23b892Sbalrog     rdh_start = s->mac_reg[RDH];
916b19487e2SMichael S. Tsirkin     desc_offset = 0;
917093454e2SDmitry Fleytman     total_size = size + e1000x_fcs_len(s->mac_reg);
918322fd48aSMichael S. Tsirkin     if (!e1000_has_rxbufs(s, total_size)) {
919322fd48aSMichael S. Tsirkin             set_ics(s, 0, E1000_ICS_RXO);
920322fd48aSMichael S. Tsirkin             return -1;
921322fd48aSMichael S. Tsirkin     }
9227c23b892Sbalrog     do {
923b19487e2SMichael S. Tsirkin         desc_size = total_size - desc_offset;
924b19487e2SMichael S. Tsirkin         if (desc_size > s->rxbuf_size) {
925b19487e2SMichael S. Tsirkin             desc_size = s->rxbuf_size;
926b19487e2SMichael S. Tsirkin         }
927d17161f6SKevin Wolf         base = rx_desc_base(s) + sizeof(desc) * s->mac_reg[RDH];
928b08340d5SAndreas Färber         pci_dma_read(d, base, &desc, sizeof(desc));
9298f2e8d1fSaliguori         desc.special = vlan_special;
9308f2e8d1fSaliguori         desc.status |= (vlan_status | E1000_RXD_STAT_DD);
9317c23b892Sbalrog         if (desc.buffer_addr) {
932b19487e2SMichael S. Tsirkin             if (desc_offset < size) {
93397410ddeSVincenzo Maffione                 size_t iov_copy;
93497410ddeSVincenzo Maffione                 hwaddr ba = le64_to_cpu(desc.buffer_addr);
935b19487e2SMichael S. Tsirkin                 size_t copy_size = size - desc_offset;
936b19487e2SMichael S. Tsirkin                 if (copy_size > s->rxbuf_size) {
937b19487e2SMichael S. Tsirkin                     copy_size = s->rxbuf_size;
938b19487e2SMichael S. Tsirkin                 }
93997410ddeSVincenzo Maffione                 do {
94097410ddeSVincenzo Maffione                     iov_copy = MIN(copy_size, iov->iov_len - iov_ofs);
94197410ddeSVincenzo Maffione                     pci_dma_write(d, ba, iov->iov_base + iov_ofs, iov_copy);
94297410ddeSVincenzo Maffione                     copy_size -= iov_copy;
94397410ddeSVincenzo Maffione                     ba += iov_copy;
94497410ddeSVincenzo Maffione                     iov_ofs += iov_copy;
94597410ddeSVincenzo Maffione                     if (iov_ofs == iov->iov_len) {
94697410ddeSVincenzo Maffione                         iov++;
94797410ddeSVincenzo Maffione                         iov_ofs = 0;
94897410ddeSVincenzo Maffione                     }
94997410ddeSVincenzo Maffione                 } while (copy_size);
950b19487e2SMichael S. Tsirkin             }
951b19487e2SMichael S. Tsirkin             desc_offset += desc_size;
952b19487e2SMichael S. Tsirkin             desc.length = cpu_to_le16(desc_size);
953ee912ccfSMichael S. Tsirkin             if (desc_offset >= total_size) {
9547c23b892Sbalrog                 desc.status |= E1000_RXD_STAT_EOP | E1000_RXD_STAT_IXSM;
955b19487e2SMichael S. Tsirkin             } else {
956ee912ccfSMichael S. Tsirkin                 /* Guest zeroing out status is not a hardware requirement.
957ee912ccfSMichael S. Tsirkin                    Clear EOP in case guest didn't do it. */
958ee912ccfSMichael S. Tsirkin                 desc.status &= ~E1000_RXD_STAT_EOP;
959b19487e2SMichael S. Tsirkin             }
96043ad7e3eSJes Sorensen         } else { // as per intel docs; skip descriptors with null buf addr
9617c23b892Sbalrog             DBGOUT(RX, "Null RX descriptor!!\n");
96243ad7e3eSJes Sorensen         }
963b08340d5SAndreas Färber         pci_dma_write(d, base, &desc, sizeof(desc));
9647c23b892Sbalrog 
9657c23b892Sbalrog         if (++s->mac_reg[RDH] * sizeof(desc) >= s->mac_reg[RDLEN])
9667c23b892Sbalrog             s->mac_reg[RDH] = 0;
9677c23b892Sbalrog         /* see comment in start_xmit; same here */
968dd793a74SLaszlo Ersek         if (s->mac_reg[RDH] == rdh_start ||
969dd793a74SLaszlo Ersek             rdh_start >= s->mac_reg[RDLEN] / sizeof(desc)) {
9707c23b892Sbalrog             DBGOUT(RXERR, "RDH wraparound @%x, RDT %x, RDLEN %x\n",
9717c23b892Sbalrog                    rdh_start, s->mac_reg[RDT], s->mac_reg[RDLEN]);
9727c23b892Sbalrog             set_ics(s, 0, E1000_ICS_RXO);
9734f1c942bSMark McLoughlin             return -1;
9747c23b892Sbalrog         }
975b19487e2SMichael S. Tsirkin     } while (desc_offset < total_size);
9767c23b892Sbalrog 
977093454e2SDmitry Fleytman     e1000x_update_rx_total_stats(s->mac_reg, size, total_size);
9787c23b892Sbalrog 
9797c23b892Sbalrog     n = E1000_ICS_RXT0;
9807c23b892Sbalrog     if ((rdt = s->mac_reg[RDT]) < s->mac_reg[RDH])
9817c23b892Sbalrog         rdt += s->mac_reg[RDLEN] / sizeof(desc);
982bf16cc8fSaliguori     if (((rdt - s->mac_reg[RDH]) * sizeof(desc)) <= s->mac_reg[RDLEN] >>
983bf16cc8fSaliguori         s->rxbuf_min_shift)
9847c23b892Sbalrog         n |= E1000_ICS_RXDMT0;
9857c23b892Sbalrog 
9867c23b892Sbalrog     set_ics(s, 0, n);
9874f1c942bSMark McLoughlin 
9884f1c942bSMark McLoughlin     return size;
9897c23b892Sbalrog }
9907c23b892Sbalrog 
99197410ddeSVincenzo Maffione static ssize_t
99297410ddeSVincenzo Maffione e1000_receive(NetClientState *nc, const uint8_t *buf, size_t size)
99397410ddeSVincenzo Maffione {
99497410ddeSVincenzo Maffione     const struct iovec iov = {
99597410ddeSVincenzo Maffione         .iov_base = (uint8_t *)buf,
99697410ddeSVincenzo Maffione         .iov_len = size
99797410ddeSVincenzo Maffione     };
99897410ddeSVincenzo Maffione 
99997410ddeSVincenzo Maffione     return e1000_receive_iov(nc, &iov, 1);
100097410ddeSVincenzo Maffione }
100197410ddeSVincenzo Maffione 
10027c23b892Sbalrog static uint32_t
10037c23b892Sbalrog mac_readreg(E1000State *s, int index)
10047c23b892Sbalrog {
10057c23b892Sbalrog     return s->mac_reg[index];
10067c23b892Sbalrog }
10077c23b892Sbalrog 
10087c23b892Sbalrog static uint32_t
100972ea771cSLeonid Bloch mac_low4_read(E1000State *s, int index)
101072ea771cSLeonid Bloch {
101172ea771cSLeonid Bloch     return s->mac_reg[index] & 0xf;
101272ea771cSLeonid Bloch }
101372ea771cSLeonid Bloch 
101472ea771cSLeonid Bloch static uint32_t
101572ea771cSLeonid Bloch mac_low11_read(E1000State *s, int index)
101672ea771cSLeonid Bloch {
101772ea771cSLeonid Bloch     return s->mac_reg[index] & 0x7ff;
101872ea771cSLeonid Bloch }
101972ea771cSLeonid Bloch 
102072ea771cSLeonid Bloch static uint32_t
102172ea771cSLeonid Bloch mac_low13_read(E1000State *s, int index)
102272ea771cSLeonid Bloch {
102372ea771cSLeonid Bloch     return s->mac_reg[index] & 0x1fff;
102472ea771cSLeonid Bloch }
102572ea771cSLeonid Bloch 
102672ea771cSLeonid Bloch static uint32_t
102772ea771cSLeonid Bloch mac_low16_read(E1000State *s, int index)
102872ea771cSLeonid Bloch {
102972ea771cSLeonid Bloch     return s->mac_reg[index] & 0xffff;
103072ea771cSLeonid Bloch }
103172ea771cSLeonid Bloch 
103272ea771cSLeonid Bloch static uint32_t
10337c23b892Sbalrog mac_icr_read(E1000State *s, int index)
10347c23b892Sbalrog {
10357c23b892Sbalrog     uint32_t ret = s->mac_reg[ICR];
10367c23b892Sbalrog 
10377c23b892Sbalrog     DBGOUT(INTERRUPT, "ICR read: %x\n", ret);
10387c23b892Sbalrog     set_interrupt_cause(s, 0, 0);
10397c23b892Sbalrog     return ret;
10407c23b892Sbalrog }
10417c23b892Sbalrog 
10427c23b892Sbalrog static uint32_t
10437c23b892Sbalrog mac_read_clr4(E1000State *s, int index)
10447c23b892Sbalrog {
10457c23b892Sbalrog     uint32_t ret = s->mac_reg[index];
10467c23b892Sbalrog 
10477c23b892Sbalrog     s->mac_reg[index] = 0;
10487c23b892Sbalrog     return ret;
10497c23b892Sbalrog }
10507c23b892Sbalrog 
10517c23b892Sbalrog static uint32_t
10527c23b892Sbalrog mac_read_clr8(E1000State *s, int index)
10537c23b892Sbalrog {
10547c23b892Sbalrog     uint32_t ret = s->mac_reg[index];
10557c23b892Sbalrog 
10567c23b892Sbalrog     s->mac_reg[index] = 0;
10577c23b892Sbalrog     s->mac_reg[index-1] = 0;
10587c23b892Sbalrog     return ret;
10597c23b892Sbalrog }
10607c23b892Sbalrog 
10617c23b892Sbalrog static void
10627c23b892Sbalrog mac_writereg(E1000State *s, int index, uint32_t val)
10637c23b892Sbalrog {
10647c36507cSAmos Kong     uint32_t macaddr[2];
10657c36507cSAmos Kong 
10667c23b892Sbalrog     s->mac_reg[index] = val;
10677c36507cSAmos Kong 
106890d131fbSMichael S. Tsirkin     if (index == RA + 1) {
10697c36507cSAmos Kong         macaddr[0] = cpu_to_le32(s->mac_reg[RA]);
10707c36507cSAmos Kong         macaddr[1] = cpu_to_le32(s->mac_reg[RA + 1]);
10717c36507cSAmos Kong         qemu_format_nic_info_str(qemu_get_queue(s->nic), (uint8_t *)macaddr);
10727c36507cSAmos Kong     }
10737c23b892Sbalrog }
10747c23b892Sbalrog 
10757c23b892Sbalrog static void
10767c23b892Sbalrog set_rdt(E1000State *s, int index, uint32_t val)
10777c23b892Sbalrog {
10787c23b892Sbalrog     s->mac_reg[index] = val & 0xffff;
1079e8b4c680SPaolo Bonzini     if (e1000_has_rxbufs(s, 1)) {
1080b356f76dSJason Wang         qemu_flush_queued_packets(qemu_get_queue(s->nic));
1081e8b4c680SPaolo Bonzini     }
10827c23b892Sbalrog }
10837c23b892Sbalrog 
10847c23b892Sbalrog static void
10857c23b892Sbalrog set_16bit(E1000State *s, int index, uint32_t val)
10867c23b892Sbalrog {
10877c23b892Sbalrog     s->mac_reg[index] = val & 0xffff;
10887c23b892Sbalrog }
10897c23b892Sbalrog 
10907c23b892Sbalrog static void
10917c23b892Sbalrog set_dlen(E1000State *s, int index, uint32_t val)
10927c23b892Sbalrog {
10937c23b892Sbalrog     s->mac_reg[index] = val & 0xfff80;
10947c23b892Sbalrog }
10957c23b892Sbalrog 
10967c23b892Sbalrog static void
10977c23b892Sbalrog set_tctl(E1000State *s, int index, uint32_t val)
10987c23b892Sbalrog {
10997c23b892Sbalrog     s->mac_reg[index] = val;
11007c23b892Sbalrog     s->mac_reg[TDT] &= 0xffff;
11017c23b892Sbalrog     start_xmit(s);
11027c23b892Sbalrog }
11037c23b892Sbalrog 
11047c23b892Sbalrog static void
11057c23b892Sbalrog set_icr(E1000State *s, int index, uint32_t val)
11067c23b892Sbalrog {
11077c23b892Sbalrog     DBGOUT(INTERRUPT, "set_icr %x\n", val);
11087c23b892Sbalrog     set_interrupt_cause(s, 0, s->mac_reg[ICR] & ~val);
11097c23b892Sbalrog }
11107c23b892Sbalrog 
11117c23b892Sbalrog static void
11127c23b892Sbalrog set_imc(E1000State *s, int index, uint32_t val)
11137c23b892Sbalrog {
11147c23b892Sbalrog     s->mac_reg[IMS] &= ~val;
11157c23b892Sbalrog     set_ics(s, 0, 0);
11167c23b892Sbalrog }
11177c23b892Sbalrog 
11187c23b892Sbalrog static void
11197c23b892Sbalrog set_ims(E1000State *s, int index, uint32_t val)
11207c23b892Sbalrog {
11217c23b892Sbalrog     s->mac_reg[IMS] |= val;
11227c23b892Sbalrog     set_ics(s, 0, 0);
11237c23b892Sbalrog }
11247c23b892Sbalrog 
11257c23b892Sbalrog #define getreg(x)    [x] = mac_readreg
11267c23b892Sbalrog static uint32_t (*macreg_readops[])(E1000State *, int) = {
11277c23b892Sbalrog     getreg(PBA),      getreg(RCTL),     getreg(TDH),      getreg(TXDCTL),
11287c23b892Sbalrog     getreg(WUFC),     getreg(TDT),      getreg(CTRL),     getreg(LEDCTL),
11297c23b892Sbalrog     getreg(MANC),     getreg(MDIC),     getreg(SWSM),     getreg(STATUS),
11307c23b892Sbalrog     getreg(TORL),     getreg(TOTL),     getreg(IMS),      getreg(TCTL),
1131b1332393SBill Paul     getreg(RDH),      getreg(RDT),      getreg(VET),      getreg(ICS),
1132a00b2335SKay Ackermann     getreg(TDBAL),    getreg(TDBAH),    getreg(RDBAH),    getreg(RDBAL),
1133e9845f09SVincenzo Maffione     getreg(TDLEN),    getreg(RDLEN),    getreg(RDTR),     getreg(RADV),
113472ea771cSLeonid Bloch     getreg(TADV),     getreg(ITR),      getreg(FCRUC),    getreg(IPAV),
113572ea771cSLeonid Bloch     getreg(WUC),      getreg(WUS),      getreg(SCC),      getreg(ECOL),
113672ea771cSLeonid Bloch     getreg(MCC),      getreg(LATECOL),  getreg(COLC),     getreg(DC),
1137757704f1SKamil Rytarowski     getreg(TNCRS),    getreg(SEQEC),    getreg(CEXTERR),  getreg(RLEC),
113872ea771cSLeonid Bloch     getreg(XONRXC),   getreg(XONTXC),   getreg(XOFFRXC),  getreg(XOFFTXC),
113972ea771cSLeonid Bloch     getreg(RFC),      getreg(RJC),      getreg(RNBC),     getreg(TSCTFC),
11403b274301SLeonid Bloch     getreg(MGTPRC),   getreg(MGTPDC),   getreg(MGTPTC),   getreg(GORCL),
11413b274301SLeonid Bloch     getreg(GOTCL),
11427c23b892Sbalrog 
114320f3e863SLeonid Bloch     [TOTH]    = mac_read_clr8,      [TORH]    = mac_read_clr8,
11443b274301SLeonid Bloch     [GOTCH]   = mac_read_clr8,      [GORCH]   = mac_read_clr8,
11453b274301SLeonid Bloch     [PRC64]   = mac_read_clr4,      [PRC127]  = mac_read_clr4,
11463b274301SLeonid Bloch     [PRC255]  = mac_read_clr4,      [PRC511]  = mac_read_clr4,
11473b274301SLeonid Bloch     [PRC1023] = mac_read_clr4,      [PRC1522] = mac_read_clr4,
11483b274301SLeonid Bloch     [PTC64]   = mac_read_clr4,      [PTC127]  = mac_read_clr4,
11493b274301SLeonid Bloch     [PTC255]  = mac_read_clr4,      [PTC511]  = mac_read_clr4,
11503b274301SLeonid Bloch     [PTC1023] = mac_read_clr4,      [PTC1522] = mac_read_clr4,
115120f3e863SLeonid Bloch     [GPRC]    = mac_read_clr4,      [GPTC]    = mac_read_clr4,
115220f3e863SLeonid Bloch     [TPT]     = mac_read_clr4,      [TPR]     = mac_read_clr4,
11533b274301SLeonid Bloch     [RUC]     = mac_read_clr4,      [ROC]     = mac_read_clr4,
11543b274301SLeonid Bloch     [BPRC]    = mac_read_clr4,      [MPRC]    = mac_read_clr4,
11553b274301SLeonid Bloch     [TSCTC]   = mac_read_clr4,      [BPTC]    = mac_read_clr4,
11563b274301SLeonid Bloch     [MPTC]    = mac_read_clr4,
115720f3e863SLeonid Bloch     [ICR]     = mac_icr_read,       [EECD]    = get_eecd,
115820f3e863SLeonid Bloch     [EERD]    = flash_eerd_read,
115972ea771cSLeonid Bloch     [RDFH]    = mac_low13_read,     [RDFT]    = mac_low13_read,
116072ea771cSLeonid Bloch     [RDFHS]   = mac_low13_read,     [RDFTS]   = mac_low13_read,
116172ea771cSLeonid Bloch     [RDFPC]   = mac_low13_read,
116272ea771cSLeonid Bloch     [TDFH]    = mac_low11_read,     [TDFT]    = mac_low11_read,
116372ea771cSLeonid Bloch     [TDFHS]   = mac_low13_read,     [TDFTS]   = mac_low13_read,
116472ea771cSLeonid Bloch     [TDFPC]   = mac_low13_read,
116572ea771cSLeonid Bloch     [AIT]     = mac_low16_read,
116620f3e863SLeonid Bloch 
11677c23b892Sbalrog     [CRCERRS ... MPC]   = &mac_readreg,
116872ea771cSLeonid Bloch     [IP6AT ... IP6AT+3] = &mac_readreg,    [IP4AT ... IP4AT+6] = &mac_readreg,
116972ea771cSLeonid Bloch     [FFLT ... FFLT+6]   = &mac_low11_read,
11707c23b892Sbalrog     [RA ... RA+31]      = &mac_readreg,
117172ea771cSLeonid Bloch     [WUPM ... WUPM+31]  = &mac_readreg,
11727c23b892Sbalrog     [MTA ... MTA+127]   = &mac_readreg,
11738f2e8d1fSaliguori     [VFTA ... VFTA+127] = &mac_readreg,
117472ea771cSLeonid Bloch     [FFMT ... FFMT+254] = &mac_low4_read,
117572ea771cSLeonid Bloch     [FFVT ... FFVT+254] = &mac_readreg,
117672ea771cSLeonid Bloch     [PBM ... PBM+16383] = &mac_readreg,
11777c23b892Sbalrog };
1178b1503cdaSmalc enum { NREADOPS = ARRAY_SIZE(macreg_readops) };
11797c23b892Sbalrog 
11807c23b892Sbalrog #define putreg(x)    [x] = mac_writereg
11817c23b892Sbalrog static void (*macreg_writeops[])(E1000State *, int, uint32_t) = {
11827c23b892Sbalrog     putreg(PBA),      putreg(EERD),     putreg(SWSM),     putreg(WUFC),
11837c23b892Sbalrog     putreg(TDBAL),    putreg(TDBAH),    putreg(TXDCTL),   putreg(RDBAH),
118472ea771cSLeonid Bloch     putreg(RDBAL),    putreg(LEDCTL),   putreg(VET),      putreg(FCRUC),
118572ea771cSLeonid Bloch     putreg(TDFH),     putreg(TDFT),     putreg(TDFHS),    putreg(TDFTS),
118672ea771cSLeonid Bloch     putreg(TDFPC),    putreg(RDFH),     putreg(RDFT),     putreg(RDFHS),
118772ea771cSLeonid Bloch     putreg(RDFTS),    putreg(RDFPC),    putreg(IPAV),     putreg(WUC),
118872ea771cSLeonid Bloch     putreg(WUS),      putreg(AIT),
118920f3e863SLeonid Bloch 
11907c23b892Sbalrog     [TDLEN]  = set_dlen,   [RDLEN]  = set_dlen,       [TCTL] = set_tctl,
11917c23b892Sbalrog     [TDT]    = set_tctl,   [MDIC]   = set_mdic,       [ICS]  = set_ics,
11927c23b892Sbalrog     [TDH]    = set_16bit,  [RDH]    = set_16bit,      [RDT]  = set_rdt,
11937c23b892Sbalrog     [IMC]    = set_imc,    [IMS]    = set_ims,        [ICR]  = set_icr,
1194cab3c825SKevin Wolf     [EECD]   = set_eecd,   [RCTL]   = set_rx_control, [CTRL] = set_ctrl,
1195e9845f09SVincenzo Maffione     [RDTR]   = set_16bit,  [RADV]   = set_16bit,      [TADV] = set_16bit,
1196e9845f09SVincenzo Maffione     [ITR]    = set_16bit,
119720f3e863SLeonid Bloch 
119872ea771cSLeonid Bloch     [IP6AT ... IP6AT+3] = &mac_writereg, [IP4AT ... IP4AT+6] = &mac_writereg,
119972ea771cSLeonid Bloch     [FFLT ... FFLT+6]   = &mac_writereg,
12007c23b892Sbalrog     [RA ... RA+31]      = &mac_writereg,
120172ea771cSLeonid Bloch     [WUPM ... WUPM+31]  = &mac_writereg,
12027c23b892Sbalrog     [MTA ... MTA+127]   = &mac_writereg,
12038f2e8d1fSaliguori     [VFTA ... VFTA+127] = &mac_writereg,
120472ea771cSLeonid Bloch     [FFMT ... FFMT+254] = &mac_writereg, [FFVT ... FFVT+254] = &mac_writereg,
120572ea771cSLeonid Bloch     [PBM ... PBM+16383] = &mac_writereg,
12067c23b892Sbalrog };
1207b9d03e35SJason Wang 
1208b1503cdaSmalc enum { NWRITEOPS = ARRAY_SIZE(macreg_writeops) };
12097c23b892Sbalrog 
1210bc0f0674SLeonid Bloch enum { MAC_ACCESS_PARTIAL = 1, MAC_ACCESS_FLAG_NEEDED = 2 };
1211bc0f0674SLeonid Bloch 
1212bc0f0674SLeonid Bloch #define markflag(x)    ((E1000_FLAG_##x << 2) | MAC_ACCESS_FLAG_NEEDED)
1213bc0f0674SLeonid Bloch /* In the array below the meaning of the bits is: [f|f|f|f|f|f|n|p]
1214bc0f0674SLeonid Bloch  * f - flag bits (up to 6 possible flags)
1215bc0f0674SLeonid Bloch  * n - flag needed
1216bc0f0674SLeonid Bloch  * p - partially implenented */
1217bc0f0674SLeonid Bloch static const uint8_t mac_reg_access[0x8000] = {
1218bc0f0674SLeonid Bloch     [RDTR]    = markflag(MIT),    [TADV]    = markflag(MIT),
1219bc0f0674SLeonid Bloch     [RADV]    = markflag(MIT),    [ITR]     = markflag(MIT),
122072ea771cSLeonid Bloch 
122172ea771cSLeonid Bloch     [IPAV]    = markflag(MAC),    [WUC]     = markflag(MAC),
122272ea771cSLeonid Bloch     [IP6AT]   = markflag(MAC),    [IP4AT]   = markflag(MAC),
122372ea771cSLeonid Bloch     [FFVT]    = markflag(MAC),    [WUPM]    = markflag(MAC),
122472ea771cSLeonid Bloch     [ECOL]    = markflag(MAC),    [MCC]     = markflag(MAC),
122572ea771cSLeonid Bloch     [DC]      = markflag(MAC),    [TNCRS]   = markflag(MAC),
122672ea771cSLeonid Bloch     [RLEC]    = markflag(MAC),    [XONRXC]  = markflag(MAC),
122772ea771cSLeonid Bloch     [XOFFTXC] = markflag(MAC),    [RFC]     = markflag(MAC),
122872ea771cSLeonid Bloch     [TSCTFC]  = markflag(MAC),    [MGTPRC]  = markflag(MAC),
122972ea771cSLeonid Bloch     [WUS]     = markflag(MAC),    [AIT]     = markflag(MAC),
123072ea771cSLeonid Bloch     [FFLT]    = markflag(MAC),    [FFMT]    = markflag(MAC),
123172ea771cSLeonid Bloch     [SCC]     = markflag(MAC),    [FCRUC]   = markflag(MAC),
123272ea771cSLeonid Bloch     [LATECOL] = markflag(MAC),    [COLC]    = markflag(MAC),
1233757704f1SKamil Rytarowski     [SEQEC]   = markflag(MAC),    [CEXTERR] = markflag(MAC),
123472ea771cSLeonid Bloch     [XONTXC]  = markflag(MAC),    [XOFFRXC] = markflag(MAC),
123572ea771cSLeonid Bloch     [RJC]     = markflag(MAC),    [RNBC]    = markflag(MAC),
123672ea771cSLeonid Bloch     [MGTPDC]  = markflag(MAC),    [MGTPTC]  = markflag(MAC),
12373b274301SLeonid Bloch     [RUC]     = markflag(MAC),    [ROC]     = markflag(MAC),
12383b274301SLeonid Bloch     [GORCL]   = markflag(MAC),    [GORCH]   = markflag(MAC),
12393b274301SLeonid Bloch     [GOTCL]   = markflag(MAC),    [GOTCH]   = markflag(MAC),
12403b274301SLeonid Bloch     [BPRC]    = markflag(MAC),    [MPRC]    = markflag(MAC),
12413b274301SLeonid Bloch     [TSCTC]   = markflag(MAC),    [PRC64]   = markflag(MAC),
12423b274301SLeonid Bloch     [PRC127]  = markflag(MAC),    [PRC255]  = markflag(MAC),
12433b274301SLeonid Bloch     [PRC511]  = markflag(MAC),    [PRC1023] = markflag(MAC),
12443b274301SLeonid Bloch     [PRC1522] = markflag(MAC),    [PTC64]   = markflag(MAC),
12453b274301SLeonid Bloch     [PTC127]  = markflag(MAC),    [PTC255]  = markflag(MAC),
12463b274301SLeonid Bloch     [PTC511]  = markflag(MAC),    [PTC1023] = markflag(MAC),
12473b274301SLeonid Bloch     [PTC1522] = markflag(MAC),    [MPTC]    = markflag(MAC),
12483b274301SLeonid Bloch     [BPTC]    = markflag(MAC),
124972ea771cSLeonid Bloch 
125072ea771cSLeonid Bloch     [TDFH]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
125172ea771cSLeonid Bloch     [TDFT]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
125272ea771cSLeonid Bloch     [TDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
125372ea771cSLeonid Bloch     [TDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
125472ea771cSLeonid Bloch     [TDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL,
125572ea771cSLeonid Bloch     [RDFH]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
125672ea771cSLeonid Bloch     [RDFT]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
125772ea771cSLeonid Bloch     [RDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
125872ea771cSLeonid Bloch     [RDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
125972ea771cSLeonid Bloch     [RDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL,
126072ea771cSLeonid Bloch     [PBM]   = markflag(MAC) | MAC_ACCESS_PARTIAL,
1261bc0f0674SLeonid Bloch };
1262bc0f0674SLeonid Bloch 
12637c23b892Sbalrog static void
1264a8170e5eSAvi Kivity e1000_mmio_write(void *opaque, hwaddr addr, uint64_t val,
1265ad00a9b9SAvi Kivity                  unsigned size)
12667c23b892Sbalrog {
12677c23b892Sbalrog     E1000State *s = opaque;
12688da3ff18Spbrook     unsigned int index = (addr & 0x1ffff) >> 2;
12697c23b892Sbalrog 
127043ad7e3eSJes Sorensen     if (index < NWRITEOPS && macreg_writeops[index]) {
1271bc0f0674SLeonid Bloch         if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED)
1272bc0f0674SLeonid Bloch             || (s->compat_flags & (mac_reg_access[index] >> 2))) {
1273bc0f0674SLeonid Bloch             if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
1274bc0f0674SLeonid Bloch                 DBGOUT(GENERAL, "Writing to register at offset: 0x%08x. "
1275bc0f0674SLeonid Bloch                        "It is not fully implemented.\n", index<<2);
1276bc0f0674SLeonid Bloch             }
12776b59fc74Saurel32             macreg_writeops[index](s, index, val);
1278bc0f0674SLeonid Bloch         } else {    /* "flag needed" bit is set, but the flag is not active */
1279bc0f0674SLeonid Bloch             DBGOUT(MMIO, "MMIO write attempt to disabled reg. addr=0x%08x\n",
1280bc0f0674SLeonid Bloch                    index<<2);
1281bc0f0674SLeonid Bloch         }
128243ad7e3eSJes Sorensen     } else if (index < NREADOPS && macreg_readops[index]) {
1283bc0f0674SLeonid Bloch         DBGOUT(MMIO, "e1000_mmio_writel RO %x: 0x%04"PRIx64"\n",
1284bc0f0674SLeonid Bloch                index<<2, val);
128543ad7e3eSJes Sorensen     } else {
1286ad00a9b9SAvi Kivity         DBGOUT(UNKNOWN, "MMIO unknown write addr=0x%08x,val=0x%08"PRIx64"\n",
12877c23b892Sbalrog                index<<2, val);
12887c23b892Sbalrog     }
128943ad7e3eSJes Sorensen }
12907c23b892Sbalrog 
1291ad00a9b9SAvi Kivity static uint64_t
1292a8170e5eSAvi Kivity e1000_mmio_read(void *opaque, hwaddr addr, unsigned size)
12937c23b892Sbalrog {
12947c23b892Sbalrog     E1000State *s = opaque;
12958da3ff18Spbrook     unsigned int index = (addr & 0x1ffff) >> 2;
12967c23b892Sbalrog 
1297bc0f0674SLeonid Bloch     if (index < NREADOPS && macreg_readops[index]) {
1298bc0f0674SLeonid Bloch         if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED)
1299bc0f0674SLeonid Bloch             || (s->compat_flags & (mac_reg_access[index] >> 2))) {
1300bc0f0674SLeonid Bloch             if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
1301bc0f0674SLeonid Bloch                 DBGOUT(GENERAL, "Reading register at offset: 0x%08x. "
1302bc0f0674SLeonid Bloch                        "It is not fully implemented.\n", index<<2);
13036b59fc74Saurel32             }
1304bc0f0674SLeonid Bloch             return macreg_readops[index](s, index);
1305bc0f0674SLeonid Bloch         } else {    /* "flag needed" bit is set, but the flag is not active */
1306bc0f0674SLeonid Bloch             DBGOUT(MMIO, "MMIO read attempt of disabled reg. addr=0x%08x\n",
1307bc0f0674SLeonid Bloch                    index<<2);
1308bc0f0674SLeonid Bloch         }
1309bc0f0674SLeonid Bloch     } else {
13107c23b892Sbalrog         DBGOUT(UNKNOWN, "MMIO unknown read addr=0x%08x\n", index<<2);
1311bc0f0674SLeonid Bloch     }
13127c23b892Sbalrog     return 0;
13137c23b892Sbalrog }
13147c23b892Sbalrog 
1315ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_mmio_ops = {
1316ad00a9b9SAvi Kivity     .read = e1000_mmio_read,
1317ad00a9b9SAvi Kivity     .write = e1000_mmio_write,
1318ad00a9b9SAvi Kivity     .endianness = DEVICE_LITTLE_ENDIAN,
1319ad00a9b9SAvi Kivity     .impl = {
1320ad00a9b9SAvi Kivity         .min_access_size = 4,
1321ad00a9b9SAvi Kivity         .max_access_size = 4,
1322ad00a9b9SAvi Kivity     },
1323ad00a9b9SAvi Kivity };
1324ad00a9b9SAvi Kivity 
1325a8170e5eSAvi Kivity static uint64_t e1000_io_read(void *opaque, hwaddr addr,
1326ad00a9b9SAvi Kivity                               unsigned size)
13277c23b892Sbalrog {
1328ad00a9b9SAvi Kivity     E1000State *s = opaque;
1329ad00a9b9SAvi Kivity 
1330ad00a9b9SAvi Kivity     (void)s;
1331ad00a9b9SAvi Kivity     return 0;
13327c23b892Sbalrog }
13337c23b892Sbalrog 
1334a8170e5eSAvi Kivity static void e1000_io_write(void *opaque, hwaddr addr,
1335ad00a9b9SAvi Kivity                            uint64_t val, unsigned size)
13367c23b892Sbalrog {
1337ad00a9b9SAvi Kivity     E1000State *s = opaque;
1338ad00a9b9SAvi Kivity 
1339ad00a9b9SAvi Kivity     (void)s;
13407c23b892Sbalrog }
13417c23b892Sbalrog 
1342ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_io_ops = {
1343ad00a9b9SAvi Kivity     .read = e1000_io_read,
1344ad00a9b9SAvi Kivity     .write = e1000_io_write,
1345ad00a9b9SAvi Kivity     .endianness = DEVICE_LITTLE_ENDIAN,
1346ad00a9b9SAvi Kivity };
1347ad00a9b9SAvi Kivity 
1348e482dc3eSJuan Quintela static bool is_version_1(void *opaque, int version_id)
13497c23b892Sbalrog {
1350e482dc3eSJuan Quintela     return version_id == 1;
13517c23b892Sbalrog }
13527c23b892Sbalrog 
135344b1ff31SDr. David Alan Gilbert static int e1000_pre_save(void *opaque)
1354ddcb73b7SMichael S. Tsirkin {
1355ddcb73b7SMichael S. Tsirkin     E1000State *s = opaque;
1356ddcb73b7SMichael S. Tsirkin     NetClientState *nc = qemu_get_queue(s->nic);
13572af234e6SMichael S. Tsirkin 
1358e9845f09SVincenzo Maffione     /* If the mitigation timer is active, emulate a timeout now. */
1359e9845f09SVincenzo Maffione     if (s->mit_timer_on) {
1360e9845f09SVincenzo Maffione         e1000_mit_timer(s);
1361e9845f09SVincenzo Maffione     }
1362e9845f09SVincenzo Maffione 
1363ddcb73b7SMichael S. Tsirkin     /*
13646a2acedbSGabriel L. Somlo      * If link is down and auto-negotiation is supported and ongoing,
13656a2acedbSGabriel L. Somlo      * complete auto-negotiation immediately. This allows us to look
13666a2acedbSGabriel L. Somlo      * at MII_SR_AUTONEG_COMPLETE to infer link status on load.
1367ddcb73b7SMichael S. Tsirkin      */
1368d7a41552SGabriel L. Somlo     if (nc->link_down && have_autoneg(s)) {
1369ddcb73b7SMichael S. Tsirkin         s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
1370ddcb73b7SMichael S. Tsirkin     }
137144b1ff31SDr. David Alan Gilbert 
1372*ff214d42SDr. David Alan Gilbert     /* Decide which set of props to migrate in the main structure */
1373*ff214d42SDr. David Alan Gilbert     if (chkflag(TSO) || !s->use_tso_for_migration) {
1374*ff214d42SDr. David Alan Gilbert         /* Either we're migrating with the extra subsection, in which
1375*ff214d42SDr. David Alan Gilbert          * case the mig_props is always 'props' OR
1376*ff214d42SDr. David Alan Gilbert          * we've not got the subsection, but 'props' was the last
1377*ff214d42SDr. David Alan Gilbert          * updated.
1378*ff214d42SDr. David Alan Gilbert          */
137959354484SDr. David Alan Gilbert         s->mig_props = s->tx.props;
1380*ff214d42SDr. David Alan Gilbert     } else {
1381*ff214d42SDr. David Alan Gilbert         /* We're not using the subsection, and 'tso_props' was
1382*ff214d42SDr. David Alan Gilbert          * the last updated.
1383*ff214d42SDr. David Alan Gilbert          */
1384*ff214d42SDr. David Alan Gilbert         s->mig_props = s->tx.tso_props;
1385*ff214d42SDr. David Alan Gilbert     }
138644b1ff31SDr. David Alan Gilbert     return 0;
1387ddcb73b7SMichael S. Tsirkin }
1388ddcb73b7SMichael S. Tsirkin 
1389e4b82364SAmos Kong static int e1000_post_load(void *opaque, int version_id)
1390e4b82364SAmos Kong {
1391e4b82364SAmos Kong     E1000State *s = opaque;
1392b356f76dSJason Wang     NetClientState *nc = qemu_get_queue(s->nic);
1393e4b82364SAmos Kong 
1394bc0f0674SLeonid Bloch     if (!chkflag(MIT)) {
1395e9845f09SVincenzo Maffione         s->mac_reg[ITR] = s->mac_reg[RDTR] = s->mac_reg[RADV] =
1396e9845f09SVincenzo Maffione             s->mac_reg[TADV] = 0;
1397e9845f09SVincenzo Maffione         s->mit_irq_level = false;
1398e9845f09SVincenzo Maffione     }
1399e9845f09SVincenzo Maffione     s->mit_ide = 0;
1400e9845f09SVincenzo Maffione     s->mit_timer_on = false;
1401e9845f09SVincenzo Maffione 
1402e4b82364SAmos Kong     /* nc.link_down can't be migrated, so infer link_down according
1403ddcb73b7SMichael S. Tsirkin      * to link status bit in mac_reg[STATUS].
1404ddcb73b7SMichael S. Tsirkin      * Alternatively, restart link negotiation if it was in progress. */
1405b356f76dSJason Wang     nc->link_down = (s->mac_reg[STATUS] & E1000_STATUS_LU) == 0;
14062af234e6SMichael S. Tsirkin 
1407d7a41552SGabriel L. Somlo     if (have_autoneg(s) &&
1408ddcb73b7SMichael S. Tsirkin         !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
1409ddcb73b7SMichael S. Tsirkin         nc->link_down = false;
1410d7a41552SGabriel L. Somlo         timer_mod(s->autoneg_timer,
1411d7a41552SGabriel L. Somlo                   qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
1412ddcb73b7SMichael S. Tsirkin     }
1413e4b82364SAmos Kong 
141459354484SDr. David Alan Gilbert     s->tx.props = s->mig_props;
14153c4053c5SDr. David Alan Gilbert     if (!s->received_tx_tso) {
14163c4053c5SDr. David Alan Gilbert         /* We received only one set of offload data (tx.props)
14173c4053c5SDr. David Alan Gilbert          * and haven't got tx.tso_props.  The best we can do
14183c4053c5SDr. David Alan Gilbert          * is dupe the data.
14193c4053c5SDr. David Alan Gilbert          */
142059354484SDr. David Alan Gilbert         s->tx.tso_props = s->mig_props;
14213c4053c5SDr. David Alan Gilbert     }
14223c4053c5SDr. David Alan Gilbert     return 0;
14233c4053c5SDr. David Alan Gilbert }
14243c4053c5SDr. David Alan Gilbert 
14253c4053c5SDr. David Alan Gilbert static int e1000_tx_tso_post_load(void *opaque, int version_id)
14263c4053c5SDr. David Alan Gilbert {
14273c4053c5SDr. David Alan Gilbert     E1000State *s = opaque;
14283c4053c5SDr. David Alan Gilbert     s->received_tx_tso = true;
1429e4b82364SAmos Kong     return 0;
1430e4b82364SAmos Kong }
1431e4b82364SAmos Kong 
1432e9845f09SVincenzo Maffione static bool e1000_mit_state_needed(void *opaque)
1433e9845f09SVincenzo Maffione {
1434e9845f09SVincenzo Maffione     E1000State *s = opaque;
1435e9845f09SVincenzo Maffione 
1436bc0f0674SLeonid Bloch     return chkflag(MIT);
1437e9845f09SVincenzo Maffione }
1438e9845f09SVincenzo Maffione 
14399e117734SLeonid Bloch static bool e1000_full_mac_needed(void *opaque)
14409e117734SLeonid Bloch {
14419e117734SLeonid Bloch     E1000State *s = opaque;
14429e117734SLeonid Bloch 
1443bc0f0674SLeonid Bloch     return chkflag(MAC);
14449e117734SLeonid Bloch }
14459e117734SLeonid Bloch 
144646f2a9ecSDr. David Alan Gilbert static bool e1000_tso_state_needed(void *opaque)
144746f2a9ecSDr. David Alan Gilbert {
144846f2a9ecSDr. David Alan Gilbert     E1000State *s = opaque;
144946f2a9ecSDr. David Alan Gilbert 
145046f2a9ecSDr. David Alan Gilbert     return chkflag(TSO);
145146f2a9ecSDr. David Alan Gilbert }
145246f2a9ecSDr. David Alan Gilbert 
1453e9845f09SVincenzo Maffione static const VMStateDescription vmstate_e1000_mit_state = {
1454e9845f09SVincenzo Maffione     .name = "e1000/mit_state",
1455e9845f09SVincenzo Maffione     .version_id = 1,
1456e9845f09SVincenzo Maffione     .minimum_version_id = 1,
14575cd8cadaSJuan Quintela     .needed = e1000_mit_state_needed,
1458e9845f09SVincenzo Maffione     .fields = (VMStateField[]) {
1459e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[RDTR], E1000State),
1460e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[RADV], E1000State),
1461e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[TADV], E1000State),
1462e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[ITR], E1000State),
1463e9845f09SVincenzo Maffione         VMSTATE_BOOL(mit_irq_level, E1000State),
1464e9845f09SVincenzo Maffione         VMSTATE_END_OF_LIST()
1465e9845f09SVincenzo Maffione     }
1466e9845f09SVincenzo Maffione };
1467e9845f09SVincenzo Maffione 
14689e117734SLeonid Bloch static const VMStateDescription vmstate_e1000_full_mac_state = {
14699e117734SLeonid Bloch     .name = "e1000/full_mac_state",
14709e117734SLeonid Bloch     .version_id = 1,
14719e117734SLeonid Bloch     .minimum_version_id = 1,
14729e117734SLeonid Bloch     .needed = e1000_full_mac_needed,
14739e117734SLeonid Bloch     .fields = (VMStateField[]) {
14749e117734SLeonid Bloch         VMSTATE_UINT32_ARRAY(mac_reg, E1000State, 0x8000),
14759e117734SLeonid Bloch         VMSTATE_END_OF_LIST()
14769e117734SLeonid Bloch     }
14779e117734SLeonid Bloch };
14789e117734SLeonid Bloch 
14794ae4bf5bSDr. David Alan Gilbert static const VMStateDescription vmstate_e1000_tx_tso_state = {
14804ae4bf5bSDr. David Alan Gilbert     .name = "e1000/tx_tso_state",
14814ae4bf5bSDr. David Alan Gilbert     .version_id = 1,
14824ae4bf5bSDr. David Alan Gilbert     .minimum_version_id = 1,
148346f2a9ecSDr. David Alan Gilbert     .needed = e1000_tso_state_needed,
14843c4053c5SDr. David Alan Gilbert     .post_load = e1000_tx_tso_post_load,
14854ae4bf5bSDr. David Alan Gilbert     .fields = (VMStateField[]) {
14864ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT8(tx.tso_props.ipcss, E1000State),
14874ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT8(tx.tso_props.ipcso, E1000State),
14884ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT16(tx.tso_props.ipcse, E1000State),
14894ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT8(tx.tso_props.tucss, E1000State),
14904ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT8(tx.tso_props.tucso, E1000State),
14914ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT16(tx.tso_props.tucse, E1000State),
14924ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT32(tx.tso_props.paylen, E1000State),
14934ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT8(tx.tso_props.hdr_len, E1000State),
14944ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT16(tx.tso_props.mss, E1000State),
14954ae4bf5bSDr. David Alan Gilbert         VMSTATE_INT8(tx.tso_props.ip, E1000State),
14964ae4bf5bSDr. David Alan Gilbert         VMSTATE_INT8(tx.tso_props.tcp, E1000State),
14974ae4bf5bSDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
14984ae4bf5bSDr. David Alan Gilbert     }
14994ae4bf5bSDr. David Alan Gilbert };
15004ae4bf5bSDr. David Alan Gilbert 
1501e482dc3eSJuan Quintela static const VMStateDescription vmstate_e1000 = {
1502e482dc3eSJuan Quintela     .name = "e1000",
15034ae4bf5bSDr. David Alan Gilbert     .version_id = 2,
1504e482dc3eSJuan Quintela     .minimum_version_id = 1,
1505ddcb73b7SMichael S. Tsirkin     .pre_save = e1000_pre_save,
1506e4b82364SAmos Kong     .post_load = e1000_post_load,
1507e482dc3eSJuan Quintela     .fields = (VMStateField[]) {
1508b08340d5SAndreas Färber         VMSTATE_PCI_DEVICE(parent_obj, E1000State),
1509e482dc3eSJuan Quintela         VMSTATE_UNUSED_TEST(is_version_1, 4), /* was instance id */
1510e482dc3eSJuan Quintela         VMSTATE_UNUSED(4), /* Was mmio_base.  */
1511e482dc3eSJuan Quintela         VMSTATE_UINT32(rxbuf_size, E1000State),
1512e482dc3eSJuan Quintela         VMSTATE_UINT32(rxbuf_min_shift, E1000State),
1513e482dc3eSJuan Quintela         VMSTATE_UINT32(eecd_state.val_in, E1000State),
1514e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.bitnum_in, E1000State),
1515e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.bitnum_out, E1000State),
1516e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.reading, E1000State),
1517e482dc3eSJuan Quintela         VMSTATE_UINT32(eecd_state.old_eecd, E1000State),
151859354484SDr. David Alan Gilbert         VMSTATE_UINT8(mig_props.ipcss, E1000State),
151959354484SDr. David Alan Gilbert         VMSTATE_UINT8(mig_props.ipcso, E1000State),
152059354484SDr. David Alan Gilbert         VMSTATE_UINT16(mig_props.ipcse, E1000State),
152159354484SDr. David Alan Gilbert         VMSTATE_UINT8(mig_props.tucss, E1000State),
152259354484SDr. David Alan Gilbert         VMSTATE_UINT8(mig_props.tucso, E1000State),
152359354484SDr. David Alan Gilbert         VMSTATE_UINT16(mig_props.tucse, E1000State),
152459354484SDr. David Alan Gilbert         VMSTATE_UINT32(mig_props.paylen, E1000State),
152559354484SDr. David Alan Gilbert         VMSTATE_UINT8(mig_props.hdr_len, E1000State),
152659354484SDr. David Alan Gilbert         VMSTATE_UINT16(mig_props.mss, E1000State),
1527e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.size, E1000State),
1528e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.tso_frames, E1000State),
15297d08c73eSEd Swierk via Qemu-devel         VMSTATE_UINT8(tx.sum_needed, E1000State),
153059354484SDr. David Alan Gilbert         VMSTATE_INT8(mig_props.ip, E1000State),
153159354484SDr. David Alan Gilbert         VMSTATE_INT8(mig_props.tcp, E1000State),
1532e482dc3eSJuan Quintela         VMSTATE_BUFFER(tx.header, E1000State),
1533e482dc3eSJuan Quintela         VMSTATE_BUFFER(tx.data, E1000State),
1534e482dc3eSJuan Quintela         VMSTATE_UINT16_ARRAY(eeprom_data, E1000State, 64),
1535e482dc3eSJuan Quintela         VMSTATE_UINT16_ARRAY(phy_reg, E1000State, 0x20),
1536e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[CTRL], E1000State),
1537e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[EECD], E1000State),
1538e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[EERD], E1000State),
1539e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[GPRC], E1000State),
1540e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[GPTC], E1000State),
1541e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[ICR], E1000State),
1542e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[ICS], E1000State),
1543e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[IMC], E1000State),
1544e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[IMS], E1000State),
1545e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[LEDCTL], E1000State),
1546e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MANC], E1000State),
1547e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MDIC], E1000State),
1548e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MPC], E1000State),
1549e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[PBA], E1000State),
1550e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RCTL], E1000State),
1551e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDBAH], E1000State),
1552e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDBAL], E1000State),
1553e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDH], E1000State),
1554e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDLEN], E1000State),
1555e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDT], E1000State),
1556e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[STATUS], E1000State),
1557e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[SWSM], E1000State),
1558e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TCTL], E1000State),
1559e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDBAH], E1000State),
1560e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDBAL], E1000State),
1561e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDH], E1000State),
1562e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDLEN], E1000State),
1563e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDT], E1000State),
1564e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TORH], E1000State),
1565e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TORL], E1000State),
1566e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TOTH], E1000State),
1567e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TOTL], E1000State),
1568e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TPR], E1000State),
1569e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TPT], E1000State),
1570e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TXDCTL], E1000State),
1571e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[WUFC], E1000State),
1572e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[VET], E1000State),
1573e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, RA, 32),
1574e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, MTA, 128),
1575e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, VFTA, 128),
1576e482dc3eSJuan Quintela         VMSTATE_END_OF_LIST()
1577e9845f09SVincenzo Maffione     },
15785cd8cadaSJuan Quintela     .subsections = (const VMStateDescription*[]) {
15795cd8cadaSJuan Quintela         &vmstate_e1000_mit_state,
15809e117734SLeonid Bloch         &vmstate_e1000_full_mac_state,
15814ae4bf5bSDr. David Alan Gilbert         &vmstate_e1000_tx_tso_state,
15825cd8cadaSJuan Quintela         NULL
15837c23b892Sbalrog     }
1584e482dc3eSJuan Quintela };
15857c23b892Sbalrog 
15868597f2e1SGabriel L. Somlo /*
15878597f2e1SGabriel L. Somlo  * EEPROM contents documented in Tables 5-2 and 5-3, pp. 98-102.
15888597f2e1SGabriel L. Somlo  * Note: A valid DevId will be inserted during pci_e1000_init().
15898597f2e1SGabriel L. Somlo  */
159088b4e9dbSblueswir1 static const uint16_t e1000_eeprom_template[64] = {
15917c23b892Sbalrog     0x0000, 0x0000, 0x0000, 0x0000,      0xffff, 0x0000,      0x0000, 0x0000,
15928597f2e1SGabriel L. Somlo     0x3000, 0x1000, 0x6403, 0 /*DevId*/, 0x8086, 0 /*DevId*/, 0x8086, 0x3040,
15937c23b892Sbalrog     0x0008, 0x2000, 0x7e14, 0x0048,      0x1000, 0x00d8,      0x0000, 0x2700,
15947c23b892Sbalrog     0x6cc9, 0x3150, 0x0722, 0x040b,      0x0984, 0x0000,      0xc000, 0x0706,
15957c23b892Sbalrog     0x1008, 0x0000, 0x0f04, 0x7fff,      0x4d01, 0xffff,      0xffff, 0xffff,
15967c23b892Sbalrog     0xffff, 0xffff, 0xffff, 0xffff,      0xffff, 0xffff,      0xffff, 0xffff,
15977c23b892Sbalrog     0x0100, 0x4000, 0x121c, 0xffff,      0xffff, 0xffff,      0xffff, 0xffff,
15987c23b892Sbalrog     0xffff, 0xffff, 0xffff, 0xffff,      0xffff, 0xffff,      0xffff, 0x0000,
15997c23b892Sbalrog };
16007c23b892Sbalrog 
16017c23b892Sbalrog /* PCI interface */
16027c23b892Sbalrog 
16037c23b892Sbalrog static void
1604ad00a9b9SAvi Kivity e1000_mmio_setup(E1000State *d)
16057c23b892Sbalrog {
1606f65ed4c1Saliguori     int i;
1607f65ed4c1Saliguori     const uint32_t excluded_regs[] = {
1608f65ed4c1Saliguori         E1000_MDIC, E1000_ICR, E1000_ICS, E1000_IMS,
1609f65ed4c1Saliguori         E1000_IMC, E1000_TCTL, E1000_TDT, PNPMMIO_SIZE
1610f65ed4c1Saliguori     };
1611f65ed4c1Saliguori 
1612eedfac6fSPaolo Bonzini     memory_region_init_io(&d->mmio, OBJECT(d), &e1000_mmio_ops, d,
1613eedfac6fSPaolo Bonzini                           "e1000-mmio", PNPMMIO_SIZE);
1614ad00a9b9SAvi Kivity     memory_region_add_coalescing(&d->mmio, 0, excluded_regs[0]);
1615f65ed4c1Saliguori     for (i = 0; excluded_regs[i] != PNPMMIO_SIZE; i++)
1616ad00a9b9SAvi Kivity         memory_region_add_coalescing(&d->mmio, excluded_regs[i] + 4,
1617ad00a9b9SAvi Kivity                                      excluded_regs[i+1] - excluded_regs[i] - 4);
1618eedfac6fSPaolo Bonzini     memory_region_init_io(&d->io, OBJECT(d), &e1000_io_ops, d, "e1000-io", IOPORT_SIZE);
16197c23b892Sbalrog }
16207c23b892Sbalrog 
1621b946a153Saliguori static void
16224b09be85Saliguori pci_e1000_uninit(PCIDevice *dev)
16234b09be85Saliguori {
1624567a3c9eSPeter Crosthwaite     E1000State *d = E1000(dev);
16254b09be85Saliguori 
1626bc72ad67SAlex Bligh     timer_del(d->autoneg_timer);
1627bc72ad67SAlex Bligh     timer_free(d->autoneg_timer);
1628e9845f09SVincenzo Maffione     timer_del(d->mit_timer);
1629e9845f09SVincenzo Maffione     timer_free(d->mit_timer);
1630948ecf21SJason Wang     qemu_del_nic(d->nic);
16314b09be85Saliguori }
16324b09be85Saliguori 
1633a03e2aecSMark McLoughlin static NetClientInfo net_e1000_info = {
1634f394b2e2SEric Blake     .type = NET_CLIENT_DRIVER_NIC,
1635a03e2aecSMark McLoughlin     .size = sizeof(NICState),
1636a03e2aecSMark McLoughlin     .can_receive = e1000_can_receive,
1637a03e2aecSMark McLoughlin     .receive = e1000_receive,
163897410ddeSVincenzo Maffione     .receive_iov = e1000_receive_iov,
1639a03e2aecSMark McLoughlin     .link_status_changed = e1000_set_link_status,
1640a03e2aecSMark McLoughlin };
1641a03e2aecSMark McLoughlin 
164220302e71SMichael S. Tsirkin static void e1000_write_config(PCIDevice *pci_dev, uint32_t address,
164320302e71SMichael S. Tsirkin                                 uint32_t val, int len)
164420302e71SMichael S. Tsirkin {
164520302e71SMichael S. Tsirkin     E1000State *s = E1000(pci_dev);
164620302e71SMichael S. Tsirkin 
164720302e71SMichael S. Tsirkin     pci_default_write_config(pci_dev, address, val, len);
164820302e71SMichael S. Tsirkin 
164920302e71SMichael S. Tsirkin     if (range_covers_byte(address, len, PCI_COMMAND) &&
165020302e71SMichael S. Tsirkin         (pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
165120302e71SMichael S. Tsirkin         qemu_flush_queued_packets(qemu_get_queue(s->nic));
165220302e71SMichael S. Tsirkin     }
165320302e71SMichael S. Tsirkin }
165420302e71SMichael S. Tsirkin 
16559af21dbeSMarkus Armbruster static void pci_e1000_realize(PCIDevice *pci_dev, Error **errp)
16567c23b892Sbalrog {
1657567a3c9eSPeter Crosthwaite     DeviceState *dev = DEVICE(pci_dev);
1658567a3c9eSPeter Crosthwaite     E1000State *d = E1000(pci_dev);
16597c23b892Sbalrog     uint8_t *pci_conf;
1660fbdaa002SGerd Hoffmann     uint8_t *macaddr;
1661aff427a1SChris Wright 
166220302e71SMichael S. Tsirkin     pci_dev->config_write = e1000_write_config;
166320302e71SMichael S. Tsirkin 
1664b08340d5SAndreas Färber     pci_conf = pci_dev->config;
16657c23b892Sbalrog 
1666a9cbacb0SMichael S. Tsirkin     /* TODO: RST# value should be 0, PCI spec 6.2.4 */
1667a9cbacb0SMichael S. Tsirkin     pci_conf[PCI_CACHE_LINE_SIZE] = 0x10;
16687c23b892Sbalrog 
1669817e0b6fSMichael S. Tsirkin     pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
16707c23b892Sbalrog 
1671ad00a9b9SAvi Kivity     e1000_mmio_setup(d);
16727c23b892Sbalrog 
1673b08340d5SAndreas Färber     pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
16747c23b892Sbalrog 
1675b08340d5SAndreas Färber     pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->io);
16767c23b892Sbalrog 
1677fbdaa002SGerd Hoffmann     qemu_macaddr_default_if_unset(&d->conf.macaddr);
1678fbdaa002SGerd Hoffmann     macaddr = d->conf.macaddr.a;
1679093454e2SDmitry Fleytman 
1680093454e2SDmitry Fleytman     e1000x_core_prepare_eeprom(d->eeprom_data,
1681093454e2SDmitry Fleytman                                e1000_eeprom_template,
1682093454e2SDmitry Fleytman                                sizeof(e1000_eeprom_template),
1683093454e2SDmitry Fleytman                                PCI_DEVICE_GET_CLASS(pci_dev)->device_id,
1684093454e2SDmitry Fleytman                                macaddr);
16857c23b892Sbalrog 
1686a03e2aecSMark McLoughlin     d->nic = qemu_new_nic(&net_e1000_info, &d->conf,
1687567a3c9eSPeter Crosthwaite                           object_get_typename(OBJECT(d)), dev->id, d);
16887c23b892Sbalrog 
1689b356f76dSJason Wang     qemu_format_nic_info_str(qemu_get_queue(d->nic), macaddr);
16901ca4d09aSGleb Natapov 
1691bc72ad67SAlex Bligh     d->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, e1000_autoneg_timer, d);
1692e9845f09SVincenzo Maffione     d->mit_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000_mit_timer, d);
16937c23b892Sbalrog }
16949d07d757SPaul Brook 
1695fbdaa002SGerd Hoffmann static void qdev_e1000_reset(DeviceState *dev)
1696fbdaa002SGerd Hoffmann {
1697567a3c9eSPeter Crosthwaite     E1000State *d = E1000(dev);
1698fbdaa002SGerd Hoffmann     e1000_reset(d);
1699fbdaa002SGerd Hoffmann }
1700fbdaa002SGerd Hoffmann 
170140021f08SAnthony Liguori static Property e1000_properties[] = {
1702fbdaa002SGerd Hoffmann     DEFINE_NIC_PROPERTIES(E1000State, conf),
17032af234e6SMichael S. Tsirkin     DEFINE_PROP_BIT("autonegotiation", E1000State,
17042af234e6SMichael S. Tsirkin                     compat_flags, E1000_FLAG_AUTONEG_BIT, true),
1705e9845f09SVincenzo Maffione     DEFINE_PROP_BIT("mitigation", E1000State,
1706e9845f09SVincenzo Maffione                     compat_flags, E1000_FLAG_MIT_BIT, true),
1707ba63ec85SLeonid Bloch     DEFINE_PROP_BIT("extra_mac_registers", E1000State,
1708ba63ec85SLeonid Bloch                     compat_flags, E1000_FLAG_MAC_BIT, true),
170946f2a9ecSDr. David Alan Gilbert     DEFINE_PROP_BIT("migrate_tso_props", E1000State,
171046f2a9ecSDr. David Alan Gilbert                     compat_flags, E1000_FLAG_TSO_BIT, true),
1711fbdaa002SGerd Hoffmann     DEFINE_PROP_END_OF_LIST(),
171240021f08SAnthony Liguori };
171340021f08SAnthony Liguori 
17148597f2e1SGabriel L. Somlo typedef struct E1000Info {
17158597f2e1SGabriel L. Somlo     const char *name;
17168597f2e1SGabriel L. Somlo     uint16_t   device_id;
17178597f2e1SGabriel L. Somlo     uint8_t    revision;
17188597f2e1SGabriel L. Somlo     uint16_t   phy_id2;
17198597f2e1SGabriel L. Somlo } E1000Info;
17208597f2e1SGabriel L. Somlo 
172140021f08SAnthony Liguori static void e1000_class_init(ObjectClass *klass, void *data)
172240021f08SAnthony Liguori {
172339bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
172440021f08SAnthony Liguori     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
17258597f2e1SGabriel L. Somlo     E1000BaseClass *e = E1000_DEVICE_CLASS(klass);
17268597f2e1SGabriel L. Somlo     const E1000Info *info = data;
172740021f08SAnthony Liguori 
17289af21dbeSMarkus Armbruster     k->realize = pci_e1000_realize;
172940021f08SAnthony Liguori     k->exit = pci_e1000_uninit;
1730c45e5b5bSGerd Hoffmann     k->romfile = "efi-e1000.rom";
173140021f08SAnthony Liguori     k->vendor_id = PCI_VENDOR_ID_INTEL;
17328597f2e1SGabriel L. Somlo     k->device_id = info->device_id;
17338597f2e1SGabriel L. Somlo     k->revision = info->revision;
17348597f2e1SGabriel L. Somlo     e->phy_id2 = info->phy_id2;
173540021f08SAnthony Liguori     k->class_id = PCI_CLASS_NETWORK_ETHERNET;
1736125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
173739bffca2SAnthony Liguori     dc->desc = "Intel Gigabit Ethernet";
173839bffca2SAnthony Liguori     dc->reset = qdev_e1000_reset;
173939bffca2SAnthony Liguori     dc->vmsd = &vmstate_e1000;
174039bffca2SAnthony Liguori     dc->props = e1000_properties;
1741fbdaa002SGerd Hoffmann }
174240021f08SAnthony Liguori 
17435df3bf62SGonglei static void e1000_instance_init(Object *obj)
17445df3bf62SGonglei {
17455df3bf62SGonglei     E1000State *n = E1000(obj);
17465df3bf62SGonglei     device_add_bootindex_property(obj, &n->conf.bootindex,
17475df3bf62SGonglei                                   "bootindex", "/ethernet-phy@0",
17485df3bf62SGonglei                                   DEVICE(n), NULL);
17495df3bf62SGonglei }
17505df3bf62SGonglei 
17518597f2e1SGabriel L. Somlo static const TypeInfo e1000_base_info = {
17528597f2e1SGabriel L. Somlo     .name          = TYPE_E1000_BASE,
175339bffca2SAnthony Liguori     .parent        = TYPE_PCI_DEVICE,
175439bffca2SAnthony Liguori     .instance_size = sizeof(E1000State),
17555df3bf62SGonglei     .instance_init = e1000_instance_init,
17568597f2e1SGabriel L. Somlo     .class_size    = sizeof(E1000BaseClass),
17578597f2e1SGabriel L. Somlo     .abstract      = true,
1758fd3b02c8SEduardo Habkost     .interfaces = (InterfaceInfo[]) {
1759fd3b02c8SEduardo Habkost         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1760fd3b02c8SEduardo Habkost         { },
1761fd3b02c8SEduardo Habkost     },
17628597f2e1SGabriel L. Somlo };
17638597f2e1SGabriel L. Somlo 
17648597f2e1SGabriel L. Somlo static const E1000Info e1000_devices[] = {
17658597f2e1SGabriel L. Somlo     {
176683044020SJason Wang         .name      = "e1000",
17678597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82540EM,
17688597f2e1SGabriel L. Somlo         .revision  = 0x03,
17698597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_8254xx_DEFAULT,
17708597f2e1SGabriel L. Somlo     },
17718597f2e1SGabriel L. Somlo     {
17728597f2e1SGabriel L. Somlo         .name      = "e1000-82544gc",
17738597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82544GC_COPPER,
17748597f2e1SGabriel L. Somlo         .revision  = 0x03,
17758597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_82544x,
17768597f2e1SGabriel L. Somlo     },
17778597f2e1SGabriel L. Somlo     {
17788597f2e1SGabriel L. Somlo         .name      = "e1000-82545em",
17798597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82545EM_COPPER,
17808597f2e1SGabriel L. Somlo         .revision  = 0x03,
17818597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_8254xx_DEFAULT,
17828597f2e1SGabriel L. Somlo     },
17838597f2e1SGabriel L. Somlo };
17848597f2e1SGabriel L. Somlo 
178583f7d43aSAndreas Färber static void e1000_register_types(void)
17869d07d757SPaul Brook {
17878597f2e1SGabriel L. Somlo     int i;
17888597f2e1SGabriel L. Somlo 
17898597f2e1SGabriel L. Somlo     type_register_static(&e1000_base_info);
17908597f2e1SGabriel L. Somlo     for (i = 0; i < ARRAY_SIZE(e1000_devices); i++) {
17918597f2e1SGabriel L. Somlo         const E1000Info *info = &e1000_devices[i];
17928597f2e1SGabriel L. Somlo         TypeInfo type_info = {};
17938597f2e1SGabriel L. Somlo 
17948597f2e1SGabriel L. Somlo         type_info.name = info->name;
17958597f2e1SGabriel L. Somlo         type_info.parent = TYPE_E1000_BASE;
17968597f2e1SGabriel L. Somlo         type_info.class_data = (void *)info;
17978597f2e1SGabriel L. Somlo         type_info.class_init = e1000_class_init;
17985df3bf62SGonglei         type_info.instance_init = e1000_instance_init;
17998597f2e1SGabriel L. Somlo 
18008597f2e1SGabriel L. Somlo         type_register(&type_info);
18018597f2e1SGabriel L. Somlo     }
18029d07d757SPaul Brook }
18039d07d757SPaul Brook 
180483f7d43aSAndreas Färber type_init(e1000_register_types)
1805