xref: /qemu/hw/net/e1000.c (revision 0b8fa32f551e863bb548a11394239239270dd3dc)
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"
36*0b8fa32fSMarkus Armbruster #include "qemu/module.h"
3720302e71SMichael S. Tsirkin #include "qemu/range.h"
387c23b892Sbalrog 
39093454e2SDmitry Fleytman #include "e1000x_common.h"
401001cf45SJason Wang #include "trace.h"
417c23b892Sbalrog 
423b274301SLeonid Bloch static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
433b274301SLeonid Bloch 
44b4053c64SJason Wang /* #define E1000_DEBUG */
457c23b892Sbalrog 
4627124888SJes Sorensen #ifdef E1000_DEBUG
477c23b892Sbalrog enum {
487c23b892Sbalrog     DEBUG_GENERAL,      DEBUG_IO,       DEBUG_MMIO,     DEBUG_INTERRUPT,
497c23b892Sbalrog     DEBUG_RX,           DEBUG_TX,       DEBUG_MDIC,     DEBUG_EEPROM,
507c23b892Sbalrog     DEBUG_UNKNOWN,      DEBUG_TXSUM,    DEBUG_TXERR,    DEBUG_RXERR,
51f9c1cdf4SJason Wang     DEBUG_RXFILTER,     DEBUG_PHY,      DEBUG_NOTYET,
527c23b892Sbalrog };
537c23b892Sbalrog #define DBGBIT(x)    (1<<DEBUG_##x)
547c23b892Sbalrog static int debugflags = DBGBIT(TXERR) | DBGBIT(GENERAL);
557c23b892Sbalrog 
566c7f4b47SBlue Swirl #define DBGOUT(what, fmt, ...) do { \
577c23b892Sbalrog     if (debugflags & DBGBIT(what)) \
586c7f4b47SBlue Swirl         fprintf(stderr, "e1000: " fmt, ## __VA_ARGS__); \
597c23b892Sbalrog     } while (0)
607c23b892Sbalrog #else
616c7f4b47SBlue Swirl #define DBGOUT(what, fmt, ...) do {} while (0)
627c23b892Sbalrog #endif
637c23b892Sbalrog 
647c23b892Sbalrog #define IOPORT_SIZE       0x40
65e94bbefeSaurel32 #define PNPMMIO_SIZE      0x20000
6678aeb23eSStefan Hajnoczi #define MIN_BUF_SIZE      60 /* Min. octets in an ethernet frame sans FCS */
677c23b892Sbalrog 
6897410ddeSVincenzo Maffione #define MAXIMUM_ETHERNET_HDR_LEN (14+4)
6997410ddeSVincenzo Maffione 
707c23b892Sbalrog /*
717c23b892Sbalrog  * HW models:
728597f2e1SGabriel L. Somlo  *  E1000_DEV_ID_82540EM works with Windows, Linux, and OS X <= 10.8
737c23b892Sbalrog  *  E1000_DEV_ID_82544GC_COPPER appears to work; not well tested
748597f2e1SGabriel L. Somlo  *  E1000_DEV_ID_82545EM_COPPER works with Linux and OS X >= 10.6
757c23b892Sbalrog  *  Others never tested
767c23b892Sbalrog  */
777c23b892Sbalrog 
787c23b892Sbalrog typedef struct E1000State_st {
79b08340d5SAndreas Färber     /*< private >*/
80b08340d5SAndreas Färber     PCIDevice parent_obj;
81b08340d5SAndreas Färber     /*< public >*/
82b08340d5SAndreas Färber 
83a03e2aecSMark McLoughlin     NICState *nic;
84fbdaa002SGerd Hoffmann     NICConf conf;
85ad00a9b9SAvi Kivity     MemoryRegion mmio;
86ad00a9b9SAvi Kivity     MemoryRegion io;
877c23b892Sbalrog 
887c23b892Sbalrog     uint32_t mac_reg[0x8000];
897c23b892Sbalrog     uint16_t phy_reg[0x20];
907c23b892Sbalrog     uint16_t eeprom_data[64];
917c23b892Sbalrog 
927c23b892Sbalrog     uint32_t rxbuf_size;
937c23b892Sbalrog     uint32_t rxbuf_min_shift;
947c23b892Sbalrog     struct e1000_tx {
957c23b892Sbalrog         unsigned char header[256];
968f2e8d1fSaliguori         unsigned char vlan_header[4];
97b10fec9bSStefan Weil         /* Fields vlan and data must not be reordered or separated. */
988f2e8d1fSaliguori         unsigned char vlan[4];
997c23b892Sbalrog         unsigned char data[0x10000];
1007c23b892Sbalrog         uint16_t size;
1018f2e8d1fSaliguori         unsigned char vlan_needed;
1027d08c73eSEd Swierk via Qemu-devel         unsigned char sum_needed;
1037d08c73eSEd Swierk via Qemu-devel         bool cptse;
104093454e2SDmitry Fleytman         e1000x_txd_props props;
105d62644b4SEd Swierk via Qemu-devel         e1000x_txd_props tso_props;
1067c23b892Sbalrog         uint16_t tso_frames;
1077c23b892Sbalrog     } tx;
1087c23b892Sbalrog 
1097c23b892Sbalrog     struct {
11020f3e863SLeonid Bloch         uint32_t val_in;    /* shifted in from guest driver */
1117c23b892Sbalrog         uint16_t bitnum_in;
1127c23b892Sbalrog         uint16_t bitnum_out;
1137c23b892Sbalrog         uint16_t reading;
1147c23b892Sbalrog         uint32_t old_eecd;
1157c23b892Sbalrog     } eecd_state;
116b9d03e35SJason Wang 
117b9d03e35SJason Wang     QEMUTimer *autoneg_timer;
1182af234e6SMichael S. Tsirkin 
119e9845f09SVincenzo Maffione     QEMUTimer *mit_timer;      /* Mitigation timer. */
120e9845f09SVincenzo Maffione     bool mit_timer_on;         /* Mitigation timer is running. */
121e9845f09SVincenzo Maffione     bool mit_irq_level;        /* Tracks interrupt pin level. */
122e9845f09SVincenzo Maffione     uint32_t mit_ide;          /* Tracks E1000_TXD_CMD_IDE bit. */
123e9845f09SVincenzo Maffione 
124157628d0Syuchenlin     QEMUTimer *flush_queue_timer;
125157628d0Syuchenlin 
1262af234e6SMichael S. Tsirkin /* Compatibility flags for migration to/from qemu 1.3.0 and older */
1272af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG_BIT 0
128e9845f09SVincenzo Maffione #define E1000_FLAG_MIT_BIT 1
1299e117734SLeonid Bloch #define E1000_FLAG_MAC_BIT 2
13046f2a9ecSDr. David Alan Gilbert #define E1000_FLAG_TSO_BIT 3
1312af234e6SMichael S. Tsirkin #define E1000_FLAG_AUTONEG (1 << E1000_FLAG_AUTONEG_BIT)
132e9845f09SVincenzo Maffione #define E1000_FLAG_MIT (1 << E1000_FLAG_MIT_BIT)
1339e117734SLeonid Bloch #define E1000_FLAG_MAC (1 << E1000_FLAG_MAC_BIT)
13446f2a9ecSDr. David Alan Gilbert #define E1000_FLAG_TSO (1 << E1000_FLAG_TSO_BIT)
1352af234e6SMichael S. Tsirkin     uint32_t compat_flags;
1363c4053c5SDr. David Alan Gilbert     bool received_tx_tso;
137ff214d42SDr. David Alan Gilbert     bool use_tso_for_migration;
13859354484SDr. David Alan Gilbert     e1000x_txd_props mig_props;
1397c23b892Sbalrog } E1000State;
1407c23b892Sbalrog 
141bc0f0674SLeonid Bloch #define chkflag(x)     (s->compat_flags & E1000_FLAG_##x)
142bc0f0674SLeonid Bloch 
1438597f2e1SGabriel L. Somlo typedef struct E1000BaseClass {
1448597f2e1SGabriel L. Somlo     PCIDeviceClass parent_class;
1458597f2e1SGabriel L. Somlo     uint16_t phy_id2;
1468597f2e1SGabriel L. Somlo } E1000BaseClass;
1478597f2e1SGabriel L. Somlo 
1488597f2e1SGabriel L. Somlo #define TYPE_E1000_BASE "e1000-base"
149567a3c9eSPeter Crosthwaite 
150567a3c9eSPeter Crosthwaite #define E1000(obj) \
1518597f2e1SGabriel L. Somlo     OBJECT_CHECK(E1000State, (obj), TYPE_E1000_BASE)
1528597f2e1SGabriel L. Somlo 
1538597f2e1SGabriel L. Somlo #define E1000_DEVICE_CLASS(klass) \
1548597f2e1SGabriel L. Somlo      OBJECT_CLASS_CHECK(E1000BaseClass, (klass), TYPE_E1000_BASE)
1558597f2e1SGabriel L. Somlo #define E1000_DEVICE_GET_CLASS(obj) \
1568597f2e1SGabriel L. Somlo     OBJECT_GET_CLASS(E1000BaseClass, (obj), TYPE_E1000_BASE)
157567a3c9eSPeter Crosthwaite 
15871aadd3cSJason Wang static void
15971aadd3cSJason Wang e1000_link_up(E1000State *s)
16071aadd3cSJason Wang {
161093454e2SDmitry Fleytman     e1000x_update_regs_on_link_up(s->mac_reg, s->phy_reg);
162093454e2SDmitry Fleytman 
163093454e2SDmitry Fleytman     /* E1000_STATUS_LU is tested by e1000_can_receive() */
164093454e2SDmitry Fleytman     qemu_flush_queued_packets(qemu_get_queue(s->nic));
165093454e2SDmitry Fleytman }
166093454e2SDmitry Fleytman 
167093454e2SDmitry Fleytman static void
168093454e2SDmitry Fleytman e1000_autoneg_done(E1000State *s)
169093454e2SDmitry Fleytman {
170093454e2SDmitry Fleytman     e1000x_update_regs_on_autoneg_done(s->mac_reg, s->phy_reg);
1715df6a185SStefan Hajnoczi 
1725df6a185SStefan Hajnoczi     /* E1000_STATUS_LU is tested by e1000_can_receive() */
1735df6a185SStefan Hajnoczi     qemu_flush_queued_packets(qemu_get_queue(s->nic));
17471aadd3cSJason Wang }
17571aadd3cSJason Wang 
1761195fed9SGabriel L. Somlo static bool
1771195fed9SGabriel L. Somlo have_autoneg(E1000State *s)
1781195fed9SGabriel L. Somlo {
179bc0f0674SLeonid Bloch     return chkflag(AUTONEG) && (s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN);
1801195fed9SGabriel L. Somlo }
1811195fed9SGabriel L. Somlo 
182b9d03e35SJason Wang static void
183b9d03e35SJason Wang set_phy_ctrl(E1000State *s, int index, uint16_t val)
184b9d03e35SJason Wang {
1851195fed9SGabriel L. Somlo     /* bits 0-5 reserved; MII_CR_[RESTART_AUTO_NEG,RESET] are self clearing */
1861195fed9SGabriel L. Somlo     s->phy_reg[PHY_CTRL] = val & ~(0x3f |
1871195fed9SGabriel L. Somlo                                    MII_CR_RESET |
1881195fed9SGabriel L. Somlo                                    MII_CR_RESTART_AUTO_NEG);
1891195fed9SGabriel L. Somlo 
1902af234e6SMichael S. Tsirkin     /*
1912af234e6SMichael S. Tsirkin      * QEMU 1.3 does not support link auto-negotiation emulation, so if we
1922af234e6SMichael S. Tsirkin      * migrate during auto negotiation, after migration the link will be
1932af234e6SMichael S. Tsirkin      * down.
1942af234e6SMichael S. Tsirkin      */
1951195fed9SGabriel L. Somlo     if (have_autoneg(s) && (val & MII_CR_RESTART_AUTO_NEG)) {
196093454e2SDmitry Fleytman         e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer);
197b9d03e35SJason Wang     }
198b9d03e35SJason Wang }
199b9d03e35SJason Wang 
200b9d03e35SJason Wang static void (*phyreg_writeops[])(E1000State *, int, uint16_t) = {
201b9d03e35SJason Wang     [PHY_CTRL] = set_phy_ctrl,
202b9d03e35SJason Wang };
203b9d03e35SJason Wang 
204b9d03e35SJason Wang enum { NPHYWRITEOPS = ARRAY_SIZE(phyreg_writeops) };
205b9d03e35SJason Wang 
2067c23b892Sbalrog enum { PHY_R = 1, PHY_W = 2, PHY_RW = PHY_R | PHY_W };
20788b4e9dbSblueswir1 static const char phy_regcap[0x20] = {
2087c23b892Sbalrog     [PHY_STATUS]      = PHY_R,     [M88E1000_EXT_PHY_SPEC_CTRL] = PHY_RW,
2097c23b892Sbalrog     [PHY_ID1]         = PHY_R,     [M88E1000_PHY_SPEC_CTRL]     = PHY_RW,
2107c23b892Sbalrog     [PHY_CTRL]        = PHY_RW,    [PHY_1000T_CTRL]             = PHY_RW,
2117c23b892Sbalrog     [PHY_LP_ABILITY]  = PHY_R,     [PHY_1000T_STATUS]           = PHY_R,
2127c23b892Sbalrog     [PHY_AUTONEG_ADV] = PHY_RW,    [M88E1000_RX_ERR_CNTR]       = PHY_R,
2136883b591SGabriel L. Somlo     [PHY_ID2]         = PHY_R,     [M88E1000_PHY_SPEC_STATUS]   = PHY_R,
2146883b591SGabriel L. Somlo     [PHY_AUTONEG_EXP] = PHY_R,
2157c23b892Sbalrog };
2167c23b892Sbalrog 
2178597f2e1SGabriel L. Somlo /* PHY_ID2 documented in 8254x_GBe_SDM.pdf, pp. 250 */
218814cd3acSMichael S. Tsirkin static const uint16_t phy_reg_init[] = {
2199616c290SGabriel L. Somlo     [PHY_CTRL]   = MII_CR_SPEED_SELECT_MSB |
2209616c290SGabriel L. Somlo                    MII_CR_FULL_DUPLEX |
2219616c290SGabriel L. Somlo                    MII_CR_AUTO_NEG_EN,
2229616c290SGabriel L. Somlo 
2239616c290SGabriel L. Somlo     [PHY_STATUS] = MII_SR_EXTENDED_CAPS |
2249616c290SGabriel L. Somlo                    MII_SR_LINK_STATUS |   /* link initially up */
2259616c290SGabriel L. Somlo                    MII_SR_AUTONEG_CAPS |
2269616c290SGabriel L. Somlo                    /* MII_SR_AUTONEG_COMPLETE: initially NOT completed */
2279616c290SGabriel L. Somlo                    MII_SR_PREAMBLE_SUPPRESS |
2289616c290SGabriel L. Somlo                    MII_SR_EXTENDED_STATUS |
2299616c290SGabriel L. Somlo                    MII_SR_10T_HD_CAPS |
2309616c290SGabriel L. Somlo                    MII_SR_10T_FD_CAPS |
2319616c290SGabriel L. Somlo                    MII_SR_100X_HD_CAPS |
2329616c290SGabriel L. Somlo                    MII_SR_100X_FD_CAPS,
2339616c290SGabriel L. Somlo 
2349616c290SGabriel L. Somlo     [PHY_ID1] = 0x141,
2359616c290SGabriel L. Somlo     /* [PHY_ID2] configured per DevId, from e1000_reset() */
2369616c290SGabriel L. Somlo     [PHY_AUTONEG_ADV] = 0xde1,
2379616c290SGabriel L. Somlo     [PHY_LP_ABILITY] = 0x1e0,
2389616c290SGabriel L. Somlo     [PHY_1000T_CTRL] = 0x0e00,
2399616c290SGabriel L. Somlo     [PHY_1000T_STATUS] = 0x3c00,
2409616c290SGabriel L. Somlo     [M88E1000_PHY_SPEC_CTRL] = 0x360,
241814cd3acSMichael S. Tsirkin     [M88E1000_PHY_SPEC_STATUS] = 0xac00,
2429616c290SGabriel L. Somlo     [M88E1000_EXT_PHY_SPEC_CTRL] = 0x0d60,
243814cd3acSMichael S. Tsirkin };
244814cd3acSMichael S. Tsirkin 
245814cd3acSMichael S. Tsirkin static const uint32_t mac_reg_init[] = {
246814cd3acSMichael S. Tsirkin     [PBA]     = 0x00100030,
247814cd3acSMichael S. Tsirkin     [LEDCTL]  = 0x602,
248814cd3acSMichael S. Tsirkin     [CTRL]    = E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN0 |
249814cd3acSMichael S. Tsirkin                 E1000_CTRL_SPD_1000 | E1000_CTRL_SLU,
250814cd3acSMichael S. Tsirkin     [STATUS]  = 0x80000000 | E1000_STATUS_GIO_MASTER_ENABLE |
251814cd3acSMichael S. Tsirkin                 E1000_STATUS_ASDV | E1000_STATUS_MTXCKOK |
252814cd3acSMichael S. Tsirkin                 E1000_STATUS_SPEED_1000 | E1000_STATUS_FD |
253814cd3acSMichael S. Tsirkin                 E1000_STATUS_LU,
254814cd3acSMichael S. Tsirkin     [MANC]    = E1000_MANC_EN_MNG2HOST | E1000_MANC_RCV_TCO_EN |
255814cd3acSMichael S. Tsirkin                 E1000_MANC_ARP_EN | E1000_MANC_0298_EN |
256814cd3acSMichael S. Tsirkin                 E1000_MANC_RMCP_EN,
257814cd3acSMichael S. Tsirkin };
258814cd3acSMichael S. Tsirkin 
259e9845f09SVincenzo Maffione /* Helper function, *curr == 0 means the value is not set */
260e9845f09SVincenzo Maffione static inline void
261e9845f09SVincenzo Maffione mit_update_delay(uint32_t *curr, uint32_t value)
262e9845f09SVincenzo Maffione {
263e9845f09SVincenzo Maffione     if (value && (*curr == 0 || value < *curr)) {
264e9845f09SVincenzo Maffione         *curr = value;
265e9845f09SVincenzo Maffione     }
266e9845f09SVincenzo Maffione }
267e9845f09SVincenzo Maffione 
2687c23b892Sbalrog static void
2697c23b892Sbalrog set_interrupt_cause(E1000State *s, int index, uint32_t val)
2707c23b892Sbalrog {
271b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
272e9845f09SVincenzo Maffione     uint32_t pending_ints;
273e9845f09SVincenzo Maffione     uint32_t mit_delay;
274b08340d5SAndreas Färber 
2757c23b892Sbalrog     s->mac_reg[ICR] = val;
276a52a8841SMichael S. Tsirkin 
277a52a8841SMichael S. Tsirkin     /*
278a52a8841SMichael S. Tsirkin      * Make sure ICR and ICS registers have the same value.
279a52a8841SMichael S. Tsirkin      * The spec says that the ICS register is write-only.  However in practice,
280a52a8841SMichael S. Tsirkin      * on real hardware ICS is readable, and for reads it has the same value as
281a52a8841SMichael S. Tsirkin      * ICR (except that ICS does not have the clear on read behaviour of ICR).
282a52a8841SMichael S. Tsirkin      *
283a52a8841SMichael S. Tsirkin      * The VxWorks PRO/1000 driver uses this behaviour.
284a52a8841SMichael S. Tsirkin      */
285b1332393SBill Paul     s->mac_reg[ICS] = val;
286a52a8841SMichael S. Tsirkin 
287e9845f09SVincenzo Maffione     pending_ints = (s->mac_reg[IMS] & s->mac_reg[ICR]);
288e9845f09SVincenzo Maffione     if (!s->mit_irq_level && pending_ints) {
289e9845f09SVincenzo Maffione         /*
290e9845f09SVincenzo Maffione          * Here we detect a potential raising edge. We postpone raising the
291e9845f09SVincenzo Maffione          * interrupt line if we are inside the mitigation delay window
292e9845f09SVincenzo Maffione          * (s->mit_timer_on == 1).
293e9845f09SVincenzo Maffione          * We provide a partial implementation of interrupt mitigation,
294e9845f09SVincenzo Maffione          * emulating only RADV, TADV and ITR (lower 16 bits, 1024ns units for
295e9845f09SVincenzo Maffione          * RADV and TADV, 256ns units for ITR). RDTR is only used to enable
296e9845f09SVincenzo Maffione          * RADV; relative timers based on TIDV and RDTR are not implemented.
297e9845f09SVincenzo Maffione          */
298e9845f09SVincenzo Maffione         if (s->mit_timer_on) {
299e9845f09SVincenzo Maffione             return;
300e9845f09SVincenzo Maffione         }
301bc0f0674SLeonid Bloch         if (chkflag(MIT)) {
302e9845f09SVincenzo Maffione             /* Compute the next mitigation delay according to pending
303e9845f09SVincenzo Maffione              * interrupts and the current values of RADV (provided
304e9845f09SVincenzo Maffione              * RDTR!=0), TADV and ITR.
305e9845f09SVincenzo Maffione              * Then rearm the timer.
306e9845f09SVincenzo Maffione              */
307e9845f09SVincenzo Maffione             mit_delay = 0;
308e9845f09SVincenzo Maffione             if (s->mit_ide &&
309e9845f09SVincenzo Maffione                     (pending_ints & (E1000_ICR_TXQE | E1000_ICR_TXDW))) {
310e9845f09SVincenzo Maffione                 mit_update_delay(&mit_delay, s->mac_reg[TADV] * 4);
311e9845f09SVincenzo Maffione             }
312e9845f09SVincenzo Maffione             if (s->mac_reg[RDTR] && (pending_ints & E1000_ICS_RXT0)) {
313e9845f09SVincenzo Maffione                 mit_update_delay(&mit_delay, s->mac_reg[RADV] * 4);
314e9845f09SVincenzo Maffione             }
315e9845f09SVincenzo Maffione             mit_update_delay(&mit_delay, s->mac_reg[ITR]);
316e9845f09SVincenzo Maffione 
31774004e8cSSameeh Jubran             /*
31874004e8cSSameeh Jubran              * According to e1000 SPEC, the Ethernet controller guarantees
31974004e8cSSameeh Jubran              * a maximum observable interrupt rate of 7813 interrupts/sec.
32074004e8cSSameeh Jubran              * Thus if mit_delay < 500 then the delay should be set to the
32174004e8cSSameeh Jubran              * minimum delay possible which is 500.
32274004e8cSSameeh Jubran              */
32374004e8cSSameeh Jubran             mit_delay = (mit_delay < 500) ? 500 : mit_delay;
32474004e8cSSameeh Jubran 
325e9845f09SVincenzo Maffione             s->mit_timer_on = 1;
326e9845f09SVincenzo Maffione             timer_mod(s->mit_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
327e9845f09SVincenzo Maffione                       mit_delay * 256);
328e9845f09SVincenzo Maffione             s->mit_ide = 0;
329e9845f09SVincenzo Maffione         }
330e9845f09SVincenzo Maffione     }
331e9845f09SVincenzo Maffione 
332e9845f09SVincenzo Maffione     s->mit_irq_level = (pending_ints != 0);
3339e64f8a3SMarcel Apfelbaum     pci_set_irq(d, s->mit_irq_level);
334e9845f09SVincenzo Maffione }
335e9845f09SVincenzo Maffione 
336e9845f09SVincenzo Maffione static void
337e9845f09SVincenzo Maffione e1000_mit_timer(void *opaque)
338e9845f09SVincenzo Maffione {
339e9845f09SVincenzo Maffione     E1000State *s = opaque;
340e9845f09SVincenzo Maffione 
341e9845f09SVincenzo Maffione     s->mit_timer_on = 0;
342e9845f09SVincenzo Maffione     /* Call set_interrupt_cause to update the irq level (if necessary). */
343e9845f09SVincenzo Maffione     set_interrupt_cause(s, 0, s->mac_reg[ICR]);
3447c23b892Sbalrog }
3457c23b892Sbalrog 
3467c23b892Sbalrog static void
3477c23b892Sbalrog set_ics(E1000State *s, int index, uint32_t val)
3487c23b892Sbalrog {
3497c23b892Sbalrog     DBGOUT(INTERRUPT, "set_ics %x, ICR %x, IMR %x\n", val, s->mac_reg[ICR],
3507c23b892Sbalrog         s->mac_reg[IMS]);
3517c23b892Sbalrog     set_interrupt_cause(s, 0, val | s->mac_reg[ICR]);
3527c23b892Sbalrog }
3537c23b892Sbalrog 
354d52aec95SGabriel L. Somlo static void
355d52aec95SGabriel L. Somlo e1000_autoneg_timer(void *opaque)
356d52aec95SGabriel L. Somlo {
357d52aec95SGabriel L. Somlo     E1000State *s = opaque;
358d52aec95SGabriel L. Somlo     if (!qemu_get_queue(s->nic)->link_down) {
359093454e2SDmitry Fleytman         e1000_autoneg_done(s);
360d52aec95SGabriel L. Somlo         set_ics(s, 0, E1000_ICS_LSC); /* signal link status change to guest */
361d52aec95SGabriel L. Somlo     }
362d52aec95SGabriel L. Somlo }
363d52aec95SGabriel L. Somlo 
364814cd3acSMichael S. Tsirkin static void e1000_reset(void *opaque)
365814cd3acSMichael S. Tsirkin {
366814cd3acSMichael S. Tsirkin     E1000State *d = opaque;
3678597f2e1SGabriel L. Somlo     E1000BaseClass *edc = E1000_DEVICE_GET_CLASS(d);
368372254c6SGabriel L. Somlo     uint8_t *macaddr = d->conf.macaddr.a;
369814cd3acSMichael S. Tsirkin 
370bc72ad67SAlex Bligh     timer_del(d->autoneg_timer);
371e9845f09SVincenzo Maffione     timer_del(d->mit_timer);
372157628d0Syuchenlin     timer_del(d->flush_queue_timer);
373e9845f09SVincenzo Maffione     d->mit_timer_on = 0;
374e9845f09SVincenzo Maffione     d->mit_irq_level = 0;
375e9845f09SVincenzo Maffione     d->mit_ide = 0;
376814cd3acSMichael S. Tsirkin     memset(d->phy_reg, 0, sizeof d->phy_reg);
377814cd3acSMichael S. Tsirkin     memmove(d->phy_reg, phy_reg_init, sizeof phy_reg_init);
3788597f2e1SGabriel L. Somlo     d->phy_reg[PHY_ID2] = edc->phy_id2;
379814cd3acSMichael S. Tsirkin     memset(d->mac_reg, 0, sizeof d->mac_reg);
380814cd3acSMichael S. Tsirkin     memmove(d->mac_reg, mac_reg_init, sizeof mac_reg_init);
381814cd3acSMichael S. Tsirkin     d->rxbuf_min_shift = 1;
382814cd3acSMichael S. Tsirkin     memset(&d->tx, 0, sizeof d->tx);
383814cd3acSMichael S. Tsirkin 
384b356f76dSJason Wang     if (qemu_get_queue(d->nic)->link_down) {
385093454e2SDmitry Fleytman         e1000x_update_regs_on_link_down(d->mac_reg, d->phy_reg);
386814cd3acSMichael S. Tsirkin     }
387372254c6SGabriel L. Somlo 
388093454e2SDmitry Fleytman     e1000x_reset_mac_addr(d->nic, d->mac_reg, macaddr);
389814cd3acSMichael S. Tsirkin }
390814cd3acSMichael S. Tsirkin 
3917c23b892Sbalrog static void
392cab3c825SKevin Wolf set_ctrl(E1000State *s, int index, uint32_t val)
393cab3c825SKevin Wolf {
394cab3c825SKevin Wolf     /* RST is self clearing */
395cab3c825SKevin Wolf     s->mac_reg[CTRL] = val & ~E1000_CTRL_RST;
396cab3c825SKevin Wolf }
397cab3c825SKevin Wolf 
398cab3c825SKevin Wolf static void
399157628d0Syuchenlin e1000_flush_queue_timer(void *opaque)
400157628d0Syuchenlin {
401157628d0Syuchenlin     E1000State *s = opaque;
402157628d0Syuchenlin 
403157628d0Syuchenlin     qemu_flush_queued_packets(qemu_get_queue(s->nic));
404157628d0Syuchenlin }
405157628d0Syuchenlin 
406157628d0Syuchenlin static void
4077c23b892Sbalrog set_rx_control(E1000State *s, int index, uint32_t val)
4087c23b892Sbalrog {
4097c23b892Sbalrog     s->mac_reg[RCTL] = val;
410093454e2SDmitry Fleytman     s->rxbuf_size = e1000x_rxbufsize(val);
4117c23b892Sbalrog     s->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1;
4127c23b892Sbalrog     DBGOUT(RX, "RCTL: %d, mac_reg[RCTL] = 0x%x\n", s->mac_reg[RDT],
4137c23b892Sbalrog            s->mac_reg[RCTL]);
414157628d0Syuchenlin     timer_mod(s->flush_queue_timer,
415157628d0Syuchenlin               qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1000);
4167c23b892Sbalrog }
4177c23b892Sbalrog 
4187c23b892Sbalrog static void
4197c23b892Sbalrog set_mdic(E1000State *s, int index, uint32_t val)
4207c23b892Sbalrog {
4217c23b892Sbalrog     uint32_t data = val & E1000_MDIC_DATA_MASK;
4227c23b892Sbalrog     uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT);
4237c23b892Sbalrog 
4247c23b892Sbalrog     if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) // phy #
4257c23b892Sbalrog         val = s->mac_reg[MDIC] | E1000_MDIC_ERROR;
4267c23b892Sbalrog     else if (val & E1000_MDIC_OP_READ) {
4277c23b892Sbalrog         DBGOUT(MDIC, "MDIC read reg 0x%x\n", addr);
4287c23b892Sbalrog         if (!(phy_regcap[addr] & PHY_R)) {
4297c23b892Sbalrog             DBGOUT(MDIC, "MDIC read reg %x unhandled\n", addr);
4307c23b892Sbalrog             val |= E1000_MDIC_ERROR;
4317c23b892Sbalrog         } else
4327c23b892Sbalrog             val = (val ^ data) | s->phy_reg[addr];
4337c23b892Sbalrog     } else if (val & E1000_MDIC_OP_WRITE) {
4347c23b892Sbalrog         DBGOUT(MDIC, "MDIC write reg 0x%x, value 0x%x\n", addr, data);
4357c23b892Sbalrog         if (!(phy_regcap[addr] & PHY_W)) {
4367c23b892Sbalrog             DBGOUT(MDIC, "MDIC write reg %x unhandled\n", addr);
4377c23b892Sbalrog             val |= E1000_MDIC_ERROR;
438b9d03e35SJason Wang         } else {
439b9d03e35SJason Wang             if (addr < NPHYWRITEOPS && phyreg_writeops[addr]) {
440b9d03e35SJason Wang                 phyreg_writeops[addr](s, index, data);
4411195fed9SGabriel L. Somlo             } else {
4427c23b892Sbalrog                 s->phy_reg[addr] = data;
4437c23b892Sbalrog             }
444b9d03e35SJason Wang         }
4451195fed9SGabriel L. Somlo     }
4467c23b892Sbalrog     s->mac_reg[MDIC] = val | E1000_MDIC_READY;
44717fbbb0bSJason Wang 
44817fbbb0bSJason Wang     if (val & E1000_MDIC_INT_EN) {
4497c23b892Sbalrog         set_ics(s, 0, E1000_ICR_MDAC);
4507c23b892Sbalrog     }
45117fbbb0bSJason Wang }
4527c23b892Sbalrog 
4537c23b892Sbalrog static uint32_t
4547c23b892Sbalrog get_eecd(E1000State *s, int index)
4557c23b892Sbalrog {
4567c23b892Sbalrog     uint32_t ret = E1000_EECD_PRES|E1000_EECD_GNT | s->eecd_state.old_eecd;
4577c23b892Sbalrog 
4587c23b892Sbalrog     DBGOUT(EEPROM, "reading eeprom bit %d (reading %d)\n",
4597c23b892Sbalrog            s->eecd_state.bitnum_out, s->eecd_state.reading);
4607c23b892Sbalrog     if (!s->eecd_state.reading ||
4617c23b892Sbalrog         ((s->eeprom_data[(s->eecd_state.bitnum_out >> 4) & 0x3f] >>
4627c23b892Sbalrog           ((s->eecd_state.bitnum_out & 0xf) ^ 0xf))) & 1)
4637c23b892Sbalrog         ret |= E1000_EECD_DO;
4647c23b892Sbalrog     return ret;
4657c23b892Sbalrog }
4667c23b892Sbalrog 
4677c23b892Sbalrog static void
4687c23b892Sbalrog set_eecd(E1000State *s, int index, uint32_t val)
4697c23b892Sbalrog {
4707c23b892Sbalrog     uint32_t oldval = s->eecd_state.old_eecd;
4717c23b892Sbalrog 
4727c23b892Sbalrog     s->eecd_state.old_eecd = val & (E1000_EECD_SK | E1000_EECD_CS |
4737c23b892Sbalrog             E1000_EECD_DI|E1000_EECD_FWE_MASK|E1000_EECD_REQ);
47420f3e863SLeonid Bloch     if (!(E1000_EECD_CS & val)) {            /* CS inactive; nothing to do */
4759651ac55SIzumi Tsutsui         return;
47620f3e863SLeonid Bloch     }
47720f3e863SLeonid Bloch     if (E1000_EECD_CS & (val ^ oldval)) {    /* CS rise edge; reset state */
4789651ac55SIzumi Tsutsui         s->eecd_state.val_in = 0;
4799651ac55SIzumi Tsutsui         s->eecd_state.bitnum_in = 0;
4809651ac55SIzumi Tsutsui         s->eecd_state.bitnum_out = 0;
4819651ac55SIzumi Tsutsui         s->eecd_state.reading = 0;
4829651ac55SIzumi Tsutsui     }
48320f3e863SLeonid Bloch     if (!(E1000_EECD_SK & (val ^ oldval))) {    /* no clock edge */
4847c23b892Sbalrog         return;
48520f3e863SLeonid Bloch     }
48620f3e863SLeonid Bloch     if (!(E1000_EECD_SK & val)) {               /* falling edge */
4877c23b892Sbalrog         s->eecd_state.bitnum_out++;
4887c23b892Sbalrog         return;
4897c23b892Sbalrog     }
4907c23b892Sbalrog     s->eecd_state.val_in <<= 1;
4917c23b892Sbalrog     if (val & E1000_EECD_DI)
4927c23b892Sbalrog         s->eecd_state.val_in |= 1;
4937c23b892Sbalrog     if (++s->eecd_state.bitnum_in == 9 && !s->eecd_state.reading) {
4947c23b892Sbalrog         s->eecd_state.bitnum_out = ((s->eecd_state.val_in & 0x3f)<<4)-1;
4957c23b892Sbalrog         s->eecd_state.reading = (((s->eecd_state.val_in >> 6) & 7) ==
4967c23b892Sbalrog             EEPROM_READ_OPCODE_MICROWIRE);
4977c23b892Sbalrog     }
4987c23b892Sbalrog     DBGOUT(EEPROM, "eeprom bitnum in %d out %d, reading %d\n",
4997c23b892Sbalrog            s->eecd_state.bitnum_in, s->eecd_state.bitnum_out,
5007c23b892Sbalrog            s->eecd_state.reading);
5017c23b892Sbalrog }
5027c23b892Sbalrog 
5037c23b892Sbalrog static uint32_t
5047c23b892Sbalrog flash_eerd_read(E1000State *s, int x)
5057c23b892Sbalrog {
5067c23b892Sbalrog     unsigned int index, r = s->mac_reg[EERD] & ~E1000_EEPROM_RW_REG_START;
5077c23b892Sbalrog 
508b1332393SBill Paul     if ((s->mac_reg[EERD] & E1000_EEPROM_RW_REG_START) == 0)
509b1332393SBill Paul         return (s->mac_reg[EERD]);
510b1332393SBill Paul 
5117c23b892Sbalrog     if ((index = r >> E1000_EEPROM_RW_ADDR_SHIFT) > EEPROM_CHECKSUM_REG)
512b1332393SBill Paul         return (E1000_EEPROM_RW_REG_DONE | r);
513b1332393SBill Paul 
514b1332393SBill Paul     return ((s->eeprom_data[index] << E1000_EEPROM_RW_REG_DATA) |
515b1332393SBill Paul            E1000_EEPROM_RW_REG_DONE | r);
5167c23b892Sbalrog }
5177c23b892Sbalrog 
5187c23b892Sbalrog static void
5197c23b892Sbalrog putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t css, uint32_t cse)
5207c23b892Sbalrog {
521c6a6a5e3Saliguori     uint32_t sum;
522c6a6a5e3Saliguori 
5237c23b892Sbalrog     if (cse && cse < n)
5247c23b892Sbalrog         n = cse + 1;
525c6a6a5e3Saliguori     if (sloc < n-1) {
526c6a6a5e3Saliguori         sum = net_checksum_add(n-css, data+css);
5270dacea92SEd Swierk         stw_be_p(data + sloc, net_checksum_finish_nozero(sum));
528c6a6a5e3Saliguori     }
5297c23b892Sbalrog }
5307c23b892Sbalrog 
5311f67f92cSLeonid Bloch static inline void
5323b274301SLeonid Bloch inc_tx_bcast_or_mcast_count(E1000State *s, const unsigned char *arr)
5333b274301SLeonid Bloch {
5343b274301SLeonid Bloch     if (!memcmp(arr, bcast, sizeof bcast)) {
535093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, BPTC);
5363b274301SLeonid Bloch     } else if (arr[0] & 1) {
537093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, MPTC);
5383b274301SLeonid Bloch     }
5393b274301SLeonid Bloch }
5403b274301SLeonid Bloch 
54145e93764SLeonid Bloch static void
54293e37d76SJason Wang e1000_send_packet(E1000State *s, const uint8_t *buf, int size)
54393e37d76SJason Wang {
5443b274301SLeonid Bloch     static const int PTCregs[6] = { PTC64, PTC127, PTC255, PTC511,
5453b274301SLeonid Bloch                                     PTC1023, PTC1522 };
5463b274301SLeonid Bloch 
547b356f76dSJason Wang     NetClientState *nc = qemu_get_queue(s->nic);
54893e37d76SJason Wang     if (s->phy_reg[PHY_CTRL] & MII_CR_LOOPBACK) {
549b356f76dSJason Wang         nc->info->receive(nc, buf, size);
55093e37d76SJason Wang     } else {
551b356f76dSJason Wang         qemu_send_packet(nc, buf, size);
55293e37d76SJason Wang     }
5533b274301SLeonid Bloch     inc_tx_bcast_or_mcast_count(s, buf);
554093454e2SDmitry Fleytman     e1000x_increase_size_stats(s->mac_reg, PTCregs, size);
55593e37d76SJason Wang }
55693e37d76SJason Wang 
55793e37d76SJason Wang static void
5587c23b892Sbalrog xmit_seg(E1000State *s)
5597c23b892Sbalrog {
56014e60aaeSPeter Maydell     uint16_t len;
56145e93764SLeonid Bloch     unsigned int frames = s->tx.tso_frames, css, sofar;
5627c23b892Sbalrog     struct e1000_tx *tp = &s->tx;
563d62644b4SEd Swierk via Qemu-devel     struct e1000x_txd_props *props = tp->cptse ? &tp->tso_props : &tp->props;
5647c23b892Sbalrog 
565d62644b4SEd Swierk via Qemu-devel     if (tp->cptse) {
566d62644b4SEd Swierk via Qemu-devel         css = props->ipcss;
5677c23b892Sbalrog         DBGOUT(TXSUM, "frames %d size %d ipcss %d\n",
5687c23b892Sbalrog                frames, tp->size, css);
569d62644b4SEd Swierk via Qemu-devel         if (props->ip) {    /* IPv4 */
570d8ee2591SPeter Maydell             stw_be_p(tp->data+css+2, tp->size - css);
571d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4,
57214e60aaeSPeter Maydell                      lduw_be_p(tp->data + css + 4) + frames);
57320f3e863SLeonid Bloch         } else {         /* IPv6 */
574d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4, tp->size - css);
57520f3e863SLeonid Bloch         }
576d62644b4SEd Swierk via Qemu-devel         css = props->tucss;
5777c23b892Sbalrog         len = tp->size - css;
578d62644b4SEd Swierk via Qemu-devel         DBGOUT(TXSUM, "tcp %d tucss %d len %d\n", props->tcp, css, len);
579d62644b4SEd Swierk via Qemu-devel         if (props->tcp) {
580d62644b4SEd Swierk via Qemu-devel             sofar = frames * props->mss;
5816bd194abSPeter Maydell             stl_be_p(tp->data+css+4, ldl_be_p(tp->data+css+4)+sofar); /* seq */
582d62644b4SEd Swierk via Qemu-devel             if (props->paylen - sofar > props->mss) {
58320f3e863SLeonid Bloch                 tp->data[css + 13] &= ~9;    /* PSH, FIN */
5843b274301SLeonid Bloch             } else if (frames) {
585093454e2SDmitry Fleytman                 e1000x_inc_reg_if_not_full(s->mac_reg, TSCTC);
5863b274301SLeonid Bloch             }
587d62644b4SEd Swierk via Qemu-devel         } else {    /* UDP */
588d8ee2591SPeter Maydell             stw_be_p(tp->data+css+4, len);
589d62644b4SEd Swierk via Qemu-devel         }
5907d08c73eSEd Swierk via Qemu-devel         if (tp->sum_needed & E1000_TXD_POPTS_TXSM) {
591e685b4ebSAlex Williamson             unsigned int phsum;
5927c23b892Sbalrog             // add pseudo-header length before checksum calculation
593d62644b4SEd Swierk via Qemu-devel             void *sp = tp->data + props->tucso;
59414e60aaeSPeter Maydell 
59514e60aaeSPeter Maydell             phsum = lduw_be_p(sp) + len;
596e685b4ebSAlex Williamson             phsum = (phsum >> 16) + (phsum & 0xffff);
597d8ee2591SPeter Maydell             stw_be_p(sp, phsum);
5987c23b892Sbalrog         }
5997c23b892Sbalrog         tp->tso_frames++;
6007c23b892Sbalrog     }
6017c23b892Sbalrog 
6027d08c73eSEd Swierk via Qemu-devel     if (tp->sum_needed & E1000_TXD_POPTS_TXSM) {
603d62644b4SEd Swierk via Qemu-devel         putsum(tp->data, tp->size, props->tucso, props->tucss, props->tucse);
604093454e2SDmitry Fleytman     }
6057d08c73eSEd Swierk via Qemu-devel     if (tp->sum_needed & E1000_TXD_POPTS_IXSM) {
606d62644b4SEd Swierk via Qemu-devel         putsum(tp->data, tp->size, props->ipcso, props->ipcss, props->ipcse);
607093454e2SDmitry Fleytman     }
6088f2e8d1fSaliguori     if (tp->vlan_needed) {
609b10fec9bSStefan Weil         memmove(tp->vlan, tp->data, 4);
610b10fec9bSStefan Weil         memmove(tp->data, tp->data + 4, 8);
6118f2e8d1fSaliguori         memcpy(tp->data + 8, tp->vlan_header, 4);
61293e37d76SJason Wang         e1000_send_packet(s, tp->vlan, tp->size + 4);
61320f3e863SLeonid Bloch     } else {
61493e37d76SJason Wang         e1000_send_packet(s, tp->data, tp->size);
61520f3e863SLeonid Bloch     }
61620f3e863SLeonid Bloch 
617093454e2SDmitry Fleytman     e1000x_inc_reg_if_not_full(s->mac_reg, TPT);
618093454e2SDmitry Fleytman     e1000x_grow_8reg_if_not_full(s->mac_reg, TOTL, s->tx.size);
6191f67f92cSLeonid Bloch     s->mac_reg[GPTC] = s->mac_reg[TPT];
6203b274301SLeonid Bloch     s->mac_reg[GOTCL] = s->mac_reg[TOTL];
6213b274301SLeonid Bloch     s->mac_reg[GOTCH] = s->mac_reg[TOTH];
6227c23b892Sbalrog }
6237c23b892Sbalrog 
6247c23b892Sbalrog static void
6257c23b892Sbalrog process_tx_desc(E1000State *s, struct e1000_tx_desc *dp)
6267c23b892Sbalrog {
627b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
6287c23b892Sbalrog     uint32_t txd_lower = le32_to_cpu(dp->lower.data);
6297c23b892Sbalrog     uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D);
630093454e2SDmitry Fleytman     unsigned int split_size = txd_lower & 0xffff, bytes, sz;
631a0ae17a6SAndrew Jones     unsigned int msh = 0xfffff;
6327c23b892Sbalrog     uint64_t addr;
6337c23b892Sbalrog     struct e1000_context_desc *xp = (struct e1000_context_desc *)dp;
6347c23b892Sbalrog     struct e1000_tx *tp = &s->tx;
6357c23b892Sbalrog 
636e9845f09SVincenzo Maffione     s->mit_ide |= (txd_lower & E1000_TXD_CMD_IDE);
63720f3e863SLeonid Bloch     if (dtype == E1000_TXD_CMD_DEXT) {    /* context descriptor */
638d62644b4SEd Swierk via Qemu-devel         if (le32_to_cpu(xp->cmd_and_length) & E1000_TXD_CMD_TSE) {
639d62644b4SEd Swierk via Qemu-devel             e1000x_read_tx_ctx_descr(xp, &tp->tso_props);
640ff214d42SDr. David Alan Gilbert             s->use_tso_for_migration = 1;
6417c23b892Sbalrog             tp->tso_frames = 0;
642d62644b4SEd Swierk via Qemu-devel         } else {
643d62644b4SEd Swierk via Qemu-devel             e1000x_read_tx_ctx_descr(xp, &tp->props);
644ff214d42SDr. David Alan Gilbert             s->use_tso_for_migration = 0;
6457c23b892Sbalrog         }
6467c23b892Sbalrog         return;
6471b0009dbSbalrog     } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) {
6481b0009dbSbalrog         // data descriptor
649735e77ecSStefan Hajnoczi         if (tp->size == 0) {
6507d08c73eSEd Swierk via Qemu-devel             tp->sum_needed = le32_to_cpu(dp->upper.data) >> 8;
651735e77ecSStefan Hajnoczi         }
6527d08c73eSEd Swierk via Qemu-devel         tp->cptse = (txd_lower & E1000_TXD_CMD_TSE) ? 1 : 0;
65343ad7e3eSJes Sorensen     } else {
6541b0009dbSbalrog         // legacy descriptor
6557d08c73eSEd Swierk via Qemu-devel         tp->cptse = 0;
65643ad7e3eSJes Sorensen     }
6577c23b892Sbalrog 
658093454e2SDmitry Fleytman     if (e1000x_vlan_enabled(s->mac_reg) &&
659093454e2SDmitry Fleytman         e1000x_is_vlan_txd(txd_lower) &&
6607d08c73eSEd Swierk via Qemu-devel         (tp->cptse || txd_lower & E1000_TXD_CMD_EOP)) {
6618f2e8d1fSaliguori         tp->vlan_needed = 1;
662d8ee2591SPeter Maydell         stw_be_p(tp->vlan_header,
6634e60a250SShannon Zhao                       le16_to_cpu(s->mac_reg[VET]));
664d8ee2591SPeter Maydell         stw_be_p(tp->vlan_header + 2,
6658f2e8d1fSaliguori                       le16_to_cpu(dp->upper.fields.special));
6668f2e8d1fSaliguori     }
6678f2e8d1fSaliguori 
6687c23b892Sbalrog     addr = le64_to_cpu(dp->buffer_addr);
669d62644b4SEd Swierk via Qemu-devel     if (tp->cptse) {
670d62644b4SEd Swierk via Qemu-devel         msh = tp->tso_props.hdr_len + tp->tso_props.mss;
6717c23b892Sbalrog         do {
6727c23b892Sbalrog             bytes = split_size;
6737c23b892Sbalrog             if (tp->size + bytes > msh)
6747c23b892Sbalrog                 bytes = msh - tp->size;
67565f82df0SAnthony Liguori 
67665f82df0SAnthony Liguori             bytes = MIN(sizeof(tp->data) - tp->size, bytes);
677b08340d5SAndreas Färber             pci_dma_read(d, addr, tp->data + tp->size, bytes);
678a0ae17a6SAndrew Jones             sz = tp->size + bytes;
679d62644b4SEd Swierk via Qemu-devel             if (sz >= tp->tso_props.hdr_len
680d62644b4SEd Swierk via Qemu-devel                 && tp->size < tp->tso_props.hdr_len) {
681d62644b4SEd Swierk via Qemu-devel                 memmove(tp->header, tp->data, tp->tso_props.hdr_len);
682a0ae17a6SAndrew Jones             }
6837c23b892Sbalrog             tp->size = sz;
6847c23b892Sbalrog             addr += bytes;
6857c23b892Sbalrog             if (sz == msh) {
6867c23b892Sbalrog                 xmit_seg(s);
687d62644b4SEd Swierk via Qemu-devel                 memmove(tp->data, tp->header, tp->tso_props.hdr_len);
688d62644b4SEd Swierk via Qemu-devel                 tp->size = tp->tso_props.hdr_len;
6897c23b892Sbalrog             }
690b947ac2bSP J P             split_size -= bytes;
691b947ac2bSP J P         } while (bytes && split_size);
6921b0009dbSbalrog     } else {
69365f82df0SAnthony Liguori         split_size = MIN(sizeof(tp->data) - tp->size, split_size);
694b08340d5SAndreas Färber         pci_dma_read(d, addr, tp->data + tp->size, split_size);
6951b0009dbSbalrog         tp->size += split_size;
6961b0009dbSbalrog     }
6977c23b892Sbalrog 
6987c23b892Sbalrog     if (!(txd_lower & E1000_TXD_CMD_EOP))
6997c23b892Sbalrog         return;
700d62644b4SEd Swierk via Qemu-devel     if (!(tp->cptse && tp->size < tp->tso_props.hdr_len)) {
7017c23b892Sbalrog         xmit_seg(s);
702a0ae17a6SAndrew Jones     }
7037c23b892Sbalrog     tp->tso_frames = 0;
7047d08c73eSEd Swierk via Qemu-devel     tp->sum_needed = 0;
7058f2e8d1fSaliguori     tp->vlan_needed = 0;
7067c23b892Sbalrog     tp->size = 0;
7077d08c73eSEd Swierk via Qemu-devel     tp->cptse = 0;
7087c23b892Sbalrog }
7097c23b892Sbalrog 
7107c23b892Sbalrog static uint32_t
71162ecbd35SEduard - Gabriel Munteanu txdesc_writeback(E1000State *s, dma_addr_t base, struct e1000_tx_desc *dp)
7127c23b892Sbalrog {
713b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
7147c23b892Sbalrog     uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data);
7157c23b892Sbalrog 
7167c23b892Sbalrog     if (!(txd_lower & (E1000_TXD_CMD_RS|E1000_TXD_CMD_RPS)))
7177c23b892Sbalrog         return 0;
7187c23b892Sbalrog     txd_upper = (le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD) &
7197c23b892Sbalrog                 ~(E1000_TXD_STAT_EC | E1000_TXD_STAT_LC | E1000_TXD_STAT_TU);
7207c23b892Sbalrog     dp->upper.data = cpu_to_le32(txd_upper);
721b08340d5SAndreas Färber     pci_dma_write(d, base + ((char *)&dp->upper - (char *)dp),
72200c3a05bSDavid Gibson                   &dp->upper, sizeof(dp->upper));
7237c23b892Sbalrog     return E1000_ICR_TXDW;
7247c23b892Sbalrog }
7257c23b892Sbalrog 
726d17161f6SKevin Wolf static uint64_t tx_desc_base(E1000State *s)
727d17161f6SKevin Wolf {
728d17161f6SKevin Wolf     uint64_t bah = s->mac_reg[TDBAH];
729d17161f6SKevin Wolf     uint64_t bal = s->mac_reg[TDBAL] & ~0xf;
730d17161f6SKevin Wolf 
731d17161f6SKevin Wolf     return (bah << 32) + bal;
732d17161f6SKevin Wolf }
733d17161f6SKevin Wolf 
7347c23b892Sbalrog static void
7357c23b892Sbalrog start_xmit(E1000State *s)
7367c23b892Sbalrog {
737b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
73862ecbd35SEduard - Gabriel Munteanu     dma_addr_t base;
7397c23b892Sbalrog     struct e1000_tx_desc desc;
7407c23b892Sbalrog     uint32_t tdh_start = s->mac_reg[TDH], cause = E1000_ICS_TXQE;
7417c23b892Sbalrog 
7427c23b892Sbalrog     if (!(s->mac_reg[TCTL] & E1000_TCTL_EN)) {
7437c23b892Sbalrog         DBGOUT(TX, "tx disabled\n");
7447c23b892Sbalrog         return;
7457c23b892Sbalrog     }
7467c23b892Sbalrog 
7477c23b892Sbalrog     while (s->mac_reg[TDH] != s->mac_reg[TDT]) {
748d17161f6SKevin Wolf         base = tx_desc_base(s) +
7497c23b892Sbalrog                sizeof(struct e1000_tx_desc) * s->mac_reg[TDH];
750b08340d5SAndreas Färber         pci_dma_read(d, base, &desc, sizeof(desc));
7517c23b892Sbalrog 
7527c23b892Sbalrog         DBGOUT(TX, "index %d: %p : %x %x\n", s->mac_reg[TDH],
7536106075bSths                (void *)(intptr_t)desc.buffer_addr, desc.lower.data,
7547c23b892Sbalrog                desc.upper.data);
7557c23b892Sbalrog 
7567c23b892Sbalrog         process_tx_desc(s, &desc);
75762ecbd35SEduard - Gabriel Munteanu         cause |= txdesc_writeback(s, base, &desc);
7587c23b892Sbalrog 
7597c23b892Sbalrog         if (++s->mac_reg[TDH] * sizeof(desc) >= s->mac_reg[TDLEN])
7607c23b892Sbalrog             s->mac_reg[TDH] = 0;
7617c23b892Sbalrog         /*
7627c23b892Sbalrog          * the following could happen only if guest sw assigns
7637c23b892Sbalrog          * bogus values to TDT/TDLEN.
7647c23b892Sbalrog          * there's nothing too intelligent we could do about this.
7657c23b892Sbalrog          */
766dd793a74SLaszlo Ersek         if (s->mac_reg[TDH] == tdh_start ||
767dd793a74SLaszlo Ersek             tdh_start >= s->mac_reg[TDLEN] / sizeof(desc)) {
7687c23b892Sbalrog             DBGOUT(TXERR, "TDH wraparound @%x, TDT %x, TDLEN %x\n",
7697c23b892Sbalrog                    tdh_start, s->mac_reg[TDT], s->mac_reg[TDLEN]);
7707c23b892Sbalrog             break;
7717c23b892Sbalrog         }
7727c23b892Sbalrog     }
7737c23b892Sbalrog     set_ics(s, 0, cause);
7747c23b892Sbalrog }
7757c23b892Sbalrog 
7767c23b892Sbalrog static int
7777c23b892Sbalrog receive_filter(E1000State *s, const uint8_t *buf, int size)
7787c23b892Sbalrog {
779093454e2SDmitry Fleytman     uint32_t rctl = s->mac_reg[RCTL];
7804aeea330SLeonid Bloch     int isbcast = !memcmp(buf, bcast, sizeof bcast), ismcast = (buf[0] & 1);
7817c23b892Sbalrog 
782093454e2SDmitry Fleytman     if (e1000x_is_vlan_packet(buf, le16_to_cpu(s->mac_reg[VET])) &&
783093454e2SDmitry Fleytman         e1000x_vlan_rx_filter_enabled(s->mac_reg)) {
78414e60aaeSPeter Maydell         uint16_t vid = lduw_be_p(buf + 14);
78514e60aaeSPeter Maydell         uint32_t vfta = ldl_le_p((uint32_t*)(s->mac_reg + VFTA) +
7868f2e8d1fSaliguori                                  ((vid >> 5) & 0x7f));
7878f2e8d1fSaliguori         if ((vfta & (1 << (vid & 0x1f))) == 0)
7888f2e8d1fSaliguori             return 0;
7898f2e8d1fSaliguori     }
7908f2e8d1fSaliguori 
7914aeea330SLeonid Bloch     if (!isbcast && !ismcast && (rctl & E1000_RCTL_UPE)) { /* promiscuous ucast */
7927c23b892Sbalrog         return 1;
7934aeea330SLeonid Bloch     }
7947c23b892Sbalrog 
7954aeea330SLeonid Bloch     if (ismcast && (rctl & E1000_RCTL_MPE)) {          /* promiscuous mcast */
796093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, MPRC);
7977c23b892Sbalrog         return 1;
7984aeea330SLeonid Bloch     }
7997c23b892Sbalrog 
8004aeea330SLeonid Bloch     if (isbcast && (rctl & E1000_RCTL_BAM)) {          /* broadcast enabled */
801093454e2SDmitry Fleytman         e1000x_inc_reg_if_not_full(s->mac_reg, BPRC);
8027c23b892Sbalrog         return 1;
8034aeea330SLeonid Bloch     }
8047c23b892Sbalrog 
805093454e2SDmitry Fleytman     return e1000x_rx_group_filter(s->mac_reg, buf);
8067c23b892Sbalrog }
8077c23b892Sbalrog 
80899ed7e30Saliguori static void
8094e68f7a0SStefan Hajnoczi e1000_set_link_status(NetClientState *nc)
81099ed7e30Saliguori {
811cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
81299ed7e30Saliguori     uint32_t old_status = s->mac_reg[STATUS];
81399ed7e30Saliguori 
814d4044c2aSBjørn Mork     if (nc->link_down) {
815093454e2SDmitry Fleytman         e1000x_update_regs_on_link_down(s->mac_reg, s->phy_reg);
816d4044c2aSBjørn Mork     } else {
817d7a41552SGabriel L. Somlo         if (have_autoneg(s) &&
8186a2acedbSGabriel L. Somlo             !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
819093454e2SDmitry Fleytman             e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer);
8206a2acedbSGabriel L. Somlo         } else {
82171aadd3cSJason Wang             e1000_link_up(s);
822d4044c2aSBjørn Mork         }
8236a2acedbSGabriel L. Somlo     }
82499ed7e30Saliguori 
82599ed7e30Saliguori     if (s->mac_reg[STATUS] != old_status)
82699ed7e30Saliguori         set_ics(s, 0, E1000_ICR_LSC);
82799ed7e30Saliguori }
82899ed7e30Saliguori 
829322fd48aSMichael S. Tsirkin static bool e1000_has_rxbufs(E1000State *s, size_t total_size)
830322fd48aSMichael S. Tsirkin {
831322fd48aSMichael S. Tsirkin     int bufs;
832322fd48aSMichael S. Tsirkin     /* Fast-path short packets */
833322fd48aSMichael S. Tsirkin     if (total_size <= s->rxbuf_size) {
834e5b8b0d4SDmitry Fleytman         return s->mac_reg[RDH] != s->mac_reg[RDT];
835322fd48aSMichael S. Tsirkin     }
836322fd48aSMichael S. Tsirkin     if (s->mac_reg[RDH] < s->mac_reg[RDT]) {
837322fd48aSMichael S. Tsirkin         bufs = s->mac_reg[RDT] - s->mac_reg[RDH];
838e5b8b0d4SDmitry Fleytman     } else if (s->mac_reg[RDH] > s->mac_reg[RDT]) {
839322fd48aSMichael S. Tsirkin         bufs = s->mac_reg[RDLEN] /  sizeof(struct e1000_rx_desc) +
840322fd48aSMichael S. Tsirkin             s->mac_reg[RDT] - s->mac_reg[RDH];
841322fd48aSMichael S. Tsirkin     } else {
842322fd48aSMichael S. Tsirkin         return false;
843322fd48aSMichael S. Tsirkin     }
844322fd48aSMichael S. Tsirkin     return total_size <= bufs * s->rxbuf_size;
845322fd48aSMichael S. Tsirkin }
846322fd48aSMichael S. Tsirkin 
8476cdfab28SMichael S. Tsirkin static int
8484e68f7a0SStefan Hajnoczi e1000_can_receive(NetClientState *nc)
8496cdfab28SMichael S. Tsirkin {
850cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
8516cdfab28SMichael S. Tsirkin 
852093454e2SDmitry Fleytman     return e1000x_rx_ready(&s->parent_obj, s->mac_reg) &&
853157628d0Syuchenlin         e1000_has_rxbufs(s, 1) && !timer_pending(s->flush_queue_timer);
8546cdfab28SMichael S. Tsirkin }
8556cdfab28SMichael S. Tsirkin 
856d17161f6SKevin Wolf static uint64_t rx_desc_base(E1000State *s)
857d17161f6SKevin Wolf {
858d17161f6SKevin Wolf     uint64_t bah = s->mac_reg[RDBAH];
859d17161f6SKevin Wolf     uint64_t bal = s->mac_reg[RDBAL] & ~0xf;
860d17161f6SKevin Wolf 
861d17161f6SKevin Wolf     return (bah << 32) + bal;
862d17161f6SKevin Wolf }
863d17161f6SKevin Wolf 
8641001cf45SJason Wang static void
8651001cf45SJason Wang e1000_receiver_overrun(E1000State *s, size_t size)
8661001cf45SJason Wang {
8671001cf45SJason Wang     trace_e1000_receiver_overrun(size, s->mac_reg[RDH], s->mac_reg[RDT]);
8681001cf45SJason Wang     e1000x_inc_reg_if_not_full(s->mac_reg, RNBC);
8691001cf45SJason Wang     e1000x_inc_reg_if_not_full(s->mac_reg, MPC);
8701001cf45SJason Wang     set_ics(s, 0, E1000_ICS_RXO);
8711001cf45SJason Wang }
8721001cf45SJason Wang 
8734f1c942bSMark McLoughlin static ssize_t
87497410ddeSVincenzo Maffione e1000_receive_iov(NetClientState *nc, const struct iovec *iov, int iovcnt)
8757c23b892Sbalrog {
876cc1f0f45SJason Wang     E1000State *s = qemu_get_nic_opaque(nc);
877b08340d5SAndreas Färber     PCIDevice *d = PCI_DEVICE(s);
8787c23b892Sbalrog     struct e1000_rx_desc desc;
87962ecbd35SEduard - Gabriel Munteanu     dma_addr_t base;
8807c23b892Sbalrog     unsigned int n, rdt;
8817c23b892Sbalrog     uint32_t rdh_start;
8828f2e8d1fSaliguori     uint16_t vlan_special = 0;
88397410ddeSVincenzo Maffione     uint8_t vlan_status = 0;
88478aeb23eSStefan Hajnoczi     uint8_t min_buf[MIN_BUF_SIZE];
88597410ddeSVincenzo Maffione     struct iovec min_iov;
88697410ddeSVincenzo Maffione     uint8_t *filter_buf = iov->iov_base;
88797410ddeSVincenzo Maffione     size_t size = iov_size(iov, iovcnt);
88897410ddeSVincenzo Maffione     size_t iov_ofs = 0;
889b19487e2SMichael S. Tsirkin     size_t desc_offset;
890b19487e2SMichael S. Tsirkin     size_t desc_size;
891b19487e2SMichael S. Tsirkin     size_t total_size;
8927c23b892Sbalrog 
893093454e2SDmitry Fleytman     if (!e1000x_hw_rx_enabled(s->mac_reg)) {
894ddcb73b7SMichael S. Tsirkin         return -1;
895ddcb73b7SMichael S. Tsirkin     }
8967c23b892Sbalrog 
897157628d0Syuchenlin     if (timer_pending(s->flush_queue_timer)) {
898157628d0Syuchenlin         return 0;
899157628d0Syuchenlin     }
900157628d0Syuchenlin 
90178aeb23eSStefan Hajnoczi     /* Pad to minimum Ethernet frame length */
90278aeb23eSStefan Hajnoczi     if (size < sizeof(min_buf)) {
90397410ddeSVincenzo Maffione         iov_to_buf(iov, iovcnt, 0, min_buf, size);
90478aeb23eSStefan Hajnoczi         memset(&min_buf[size], 0, sizeof(min_buf) - size);
90597410ddeSVincenzo Maffione         min_iov.iov_base = filter_buf = min_buf;
90697410ddeSVincenzo Maffione         min_iov.iov_len = size = sizeof(min_buf);
90797410ddeSVincenzo Maffione         iovcnt = 1;
90897410ddeSVincenzo Maffione         iov = &min_iov;
90997410ddeSVincenzo Maffione     } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) {
91097410ddeSVincenzo Maffione         /* This is very unlikely, but may happen. */
91197410ddeSVincenzo Maffione         iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN);
91297410ddeSVincenzo Maffione         filter_buf = min_buf;
91378aeb23eSStefan Hajnoczi     }
91478aeb23eSStefan Hajnoczi 
915b0d9ffcdSMichael Contreras     /* Discard oversized packets if !LPE and !SBP. */
916093454e2SDmitry Fleytman     if (e1000x_is_oversized(s->mac_reg, size)) {
917b0d9ffcdSMichael Contreras         return size;
918b0d9ffcdSMichael Contreras     }
919b0d9ffcdSMichael Contreras 
92097410ddeSVincenzo Maffione     if (!receive_filter(s, filter_buf, size)) {
9214f1c942bSMark McLoughlin         return size;
92297410ddeSVincenzo Maffione     }
9237c23b892Sbalrog 
924093454e2SDmitry Fleytman     if (e1000x_vlan_enabled(s->mac_reg) &&
925093454e2SDmitry Fleytman         e1000x_is_vlan_packet(filter_buf, le16_to_cpu(s->mac_reg[VET]))) {
92614e60aaeSPeter Maydell         vlan_special = cpu_to_le16(lduw_be_p(filter_buf + 14));
92797410ddeSVincenzo Maffione         iov_ofs = 4;
92897410ddeSVincenzo Maffione         if (filter_buf == iov->iov_base) {
92997410ddeSVincenzo Maffione             memmove(filter_buf + 4, filter_buf, 12);
93097410ddeSVincenzo Maffione         } else {
93197410ddeSVincenzo Maffione             iov_from_buf(iov, iovcnt, 4, filter_buf, 12);
93297410ddeSVincenzo Maffione             while (iov->iov_len <= iov_ofs) {
93397410ddeSVincenzo Maffione                 iov_ofs -= iov->iov_len;
93497410ddeSVincenzo Maffione                 iov++;
93597410ddeSVincenzo Maffione             }
93697410ddeSVincenzo Maffione         }
9378f2e8d1fSaliguori         vlan_status = E1000_RXD_STAT_VP;
9388f2e8d1fSaliguori         size -= 4;
9398f2e8d1fSaliguori     }
9408f2e8d1fSaliguori 
9417c23b892Sbalrog     rdh_start = s->mac_reg[RDH];
942b19487e2SMichael S. Tsirkin     desc_offset = 0;
943093454e2SDmitry Fleytman     total_size = size + e1000x_fcs_len(s->mac_reg);
944322fd48aSMichael S. Tsirkin     if (!e1000_has_rxbufs(s, total_size)) {
9451001cf45SJason Wang         e1000_receiver_overrun(s, total_size);
946322fd48aSMichael S. Tsirkin         return -1;
947322fd48aSMichael S. Tsirkin     }
9487c23b892Sbalrog     do {
949b19487e2SMichael S. Tsirkin         desc_size = total_size - desc_offset;
950b19487e2SMichael S. Tsirkin         if (desc_size > s->rxbuf_size) {
951b19487e2SMichael S. Tsirkin             desc_size = s->rxbuf_size;
952b19487e2SMichael S. Tsirkin         }
953d17161f6SKevin Wolf         base = rx_desc_base(s) + sizeof(desc) * s->mac_reg[RDH];
954b08340d5SAndreas Färber         pci_dma_read(d, base, &desc, sizeof(desc));
9558f2e8d1fSaliguori         desc.special = vlan_special;
9568f2e8d1fSaliguori         desc.status |= (vlan_status | E1000_RXD_STAT_DD);
9577c23b892Sbalrog         if (desc.buffer_addr) {
958b19487e2SMichael S. Tsirkin             if (desc_offset < size) {
95997410ddeSVincenzo Maffione                 size_t iov_copy;
96097410ddeSVincenzo Maffione                 hwaddr ba = le64_to_cpu(desc.buffer_addr);
961b19487e2SMichael S. Tsirkin                 size_t copy_size = size - desc_offset;
962b19487e2SMichael S. Tsirkin                 if (copy_size > s->rxbuf_size) {
963b19487e2SMichael S. Tsirkin                     copy_size = s->rxbuf_size;
964b19487e2SMichael S. Tsirkin                 }
96597410ddeSVincenzo Maffione                 do {
96697410ddeSVincenzo Maffione                     iov_copy = MIN(copy_size, iov->iov_len - iov_ofs);
96797410ddeSVincenzo Maffione                     pci_dma_write(d, ba, iov->iov_base + iov_ofs, iov_copy);
96897410ddeSVincenzo Maffione                     copy_size -= iov_copy;
96997410ddeSVincenzo Maffione                     ba += iov_copy;
97097410ddeSVincenzo Maffione                     iov_ofs += iov_copy;
97197410ddeSVincenzo Maffione                     if (iov_ofs == iov->iov_len) {
97297410ddeSVincenzo Maffione                         iov++;
97397410ddeSVincenzo Maffione                         iov_ofs = 0;
97497410ddeSVincenzo Maffione                     }
97597410ddeSVincenzo Maffione                 } while (copy_size);
976b19487e2SMichael S. Tsirkin             }
977b19487e2SMichael S. Tsirkin             desc_offset += desc_size;
978b19487e2SMichael S. Tsirkin             desc.length = cpu_to_le16(desc_size);
979ee912ccfSMichael S. Tsirkin             if (desc_offset >= total_size) {
9807c23b892Sbalrog                 desc.status |= E1000_RXD_STAT_EOP | E1000_RXD_STAT_IXSM;
981b19487e2SMichael S. Tsirkin             } else {
982ee912ccfSMichael S. Tsirkin                 /* Guest zeroing out status is not a hardware requirement.
983ee912ccfSMichael S. Tsirkin                    Clear EOP in case guest didn't do it. */
984ee912ccfSMichael S. Tsirkin                 desc.status &= ~E1000_RXD_STAT_EOP;
985b19487e2SMichael S. Tsirkin             }
98643ad7e3eSJes Sorensen         } else { // as per intel docs; skip descriptors with null buf addr
9877c23b892Sbalrog             DBGOUT(RX, "Null RX descriptor!!\n");
98843ad7e3eSJes Sorensen         }
989b08340d5SAndreas Färber         pci_dma_write(d, base, &desc, sizeof(desc));
9907c23b892Sbalrog 
9917c23b892Sbalrog         if (++s->mac_reg[RDH] * sizeof(desc) >= s->mac_reg[RDLEN])
9927c23b892Sbalrog             s->mac_reg[RDH] = 0;
9937c23b892Sbalrog         /* see comment in start_xmit; same here */
994dd793a74SLaszlo Ersek         if (s->mac_reg[RDH] == rdh_start ||
995dd793a74SLaszlo Ersek             rdh_start >= s->mac_reg[RDLEN] / sizeof(desc)) {
9967c23b892Sbalrog             DBGOUT(RXERR, "RDH wraparound @%x, RDT %x, RDLEN %x\n",
9977c23b892Sbalrog                    rdh_start, s->mac_reg[RDT], s->mac_reg[RDLEN]);
9981001cf45SJason Wang             e1000_receiver_overrun(s, total_size);
9994f1c942bSMark McLoughlin             return -1;
10007c23b892Sbalrog         }
1001b19487e2SMichael S. Tsirkin     } while (desc_offset < total_size);
10027c23b892Sbalrog 
1003093454e2SDmitry Fleytman     e1000x_update_rx_total_stats(s->mac_reg, size, total_size);
10047c23b892Sbalrog 
10057c23b892Sbalrog     n = E1000_ICS_RXT0;
10067c23b892Sbalrog     if ((rdt = s->mac_reg[RDT]) < s->mac_reg[RDH])
10077c23b892Sbalrog         rdt += s->mac_reg[RDLEN] / sizeof(desc);
1008bf16cc8fSaliguori     if (((rdt - s->mac_reg[RDH]) * sizeof(desc)) <= s->mac_reg[RDLEN] >>
1009bf16cc8fSaliguori         s->rxbuf_min_shift)
10107c23b892Sbalrog         n |= E1000_ICS_RXDMT0;
10117c23b892Sbalrog 
10127c23b892Sbalrog     set_ics(s, 0, n);
10134f1c942bSMark McLoughlin 
10144f1c942bSMark McLoughlin     return size;
10157c23b892Sbalrog }
10167c23b892Sbalrog 
101797410ddeSVincenzo Maffione static ssize_t
101897410ddeSVincenzo Maffione e1000_receive(NetClientState *nc, const uint8_t *buf, size_t size)
101997410ddeSVincenzo Maffione {
102097410ddeSVincenzo Maffione     const struct iovec iov = {
102197410ddeSVincenzo Maffione         .iov_base = (uint8_t *)buf,
102297410ddeSVincenzo Maffione         .iov_len = size
102397410ddeSVincenzo Maffione     };
102497410ddeSVincenzo Maffione 
102597410ddeSVincenzo Maffione     return e1000_receive_iov(nc, &iov, 1);
102697410ddeSVincenzo Maffione }
102797410ddeSVincenzo Maffione 
10287c23b892Sbalrog static uint32_t
10297c23b892Sbalrog mac_readreg(E1000State *s, int index)
10307c23b892Sbalrog {
10317c23b892Sbalrog     return s->mac_reg[index];
10327c23b892Sbalrog }
10337c23b892Sbalrog 
10347c23b892Sbalrog static uint32_t
103572ea771cSLeonid Bloch mac_low4_read(E1000State *s, int index)
103672ea771cSLeonid Bloch {
103772ea771cSLeonid Bloch     return s->mac_reg[index] & 0xf;
103872ea771cSLeonid Bloch }
103972ea771cSLeonid Bloch 
104072ea771cSLeonid Bloch static uint32_t
104172ea771cSLeonid Bloch mac_low11_read(E1000State *s, int index)
104272ea771cSLeonid Bloch {
104372ea771cSLeonid Bloch     return s->mac_reg[index] & 0x7ff;
104472ea771cSLeonid Bloch }
104572ea771cSLeonid Bloch 
104672ea771cSLeonid Bloch static uint32_t
104772ea771cSLeonid Bloch mac_low13_read(E1000State *s, int index)
104872ea771cSLeonid Bloch {
104972ea771cSLeonid Bloch     return s->mac_reg[index] & 0x1fff;
105072ea771cSLeonid Bloch }
105172ea771cSLeonid Bloch 
105272ea771cSLeonid Bloch static uint32_t
105372ea771cSLeonid Bloch mac_low16_read(E1000State *s, int index)
105472ea771cSLeonid Bloch {
105572ea771cSLeonid Bloch     return s->mac_reg[index] & 0xffff;
105672ea771cSLeonid Bloch }
105772ea771cSLeonid Bloch 
105872ea771cSLeonid Bloch static uint32_t
10597c23b892Sbalrog mac_icr_read(E1000State *s, int index)
10607c23b892Sbalrog {
10617c23b892Sbalrog     uint32_t ret = s->mac_reg[ICR];
10627c23b892Sbalrog 
10637c23b892Sbalrog     DBGOUT(INTERRUPT, "ICR read: %x\n", ret);
10647c23b892Sbalrog     set_interrupt_cause(s, 0, 0);
10657c23b892Sbalrog     return ret;
10667c23b892Sbalrog }
10677c23b892Sbalrog 
10687c23b892Sbalrog static uint32_t
10697c23b892Sbalrog mac_read_clr4(E1000State *s, int index)
10707c23b892Sbalrog {
10717c23b892Sbalrog     uint32_t ret = s->mac_reg[index];
10727c23b892Sbalrog 
10737c23b892Sbalrog     s->mac_reg[index] = 0;
10747c23b892Sbalrog     return ret;
10757c23b892Sbalrog }
10767c23b892Sbalrog 
10777c23b892Sbalrog static uint32_t
10787c23b892Sbalrog mac_read_clr8(E1000State *s, int index)
10797c23b892Sbalrog {
10807c23b892Sbalrog     uint32_t ret = s->mac_reg[index];
10817c23b892Sbalrog 
10827c23b892Sbalrog     s->mac_reg[index] = 0;
10837c23b892Sbalrog     s->mac_reg[index-1] = 0;
10847c23b892Sbalrog     return ret;
10857c23b892Sbalrog }
10867c23b892Sbalrog 
10877c23b892Sbalrog static void
10887c23b892Sbalrog mac_writereg(E1000State *s, int index, uint32_t val)
10897c23b892Sbalrog {
10907c36507cSAmos Kong     uint32_t macaddr[2];
10917c36507cSAmos Kong 
10927c23b892Sbalrog     s->mac_reg[index] = val;
10937c36507cSAmos Kong 
109490d131fbSMichael S. Tsirkin     if (index == RA + 1) {
10957c36507cSAmos Kong         macaddr[0] = cpu_to_le32(s->mac_reg[RA]);
10967c36507cSAmos Kong         macaddr[1] = cpu_to_le32(s->mac_reg[RA + 1]);
10977c36507cSAmos Kong         qemu_format_nic_info_str(qemu_get_queue(s->nic), (uint8_t *)macaddr);
10987c36507cSAmos Kong     }
10997c23b892Sbalrog }
11007c23b892Sbalrog 
11017c23b892Sbalrog static void
11027c23b892Sbalrog set_rdt(E1000State *s, int index, uint32_t val)
11037c23b892Sbalrog {
11047c23b892Sbalrog     s->mac_reg[index] = val & 0xffff;
1105e8b4c680SPaolo Bonzini     if (e1000_has_rxbufs(s, 1)) {
1106b356f76dSJason Wang         qemu_flush_queued_packets(qemu_get_queue(s->nic));
1107e8b4c680SPaolo Bonzini     }
11087c23b892Sbalrog }
11097c23b892Sbalrog 
11107c23b892Sbalrog static void
11117c23b892Sbalrog set_16bit(E1000State *s, int index, uint32_t val)
11127c23b892Sbalrog {
11137c23b892Sbalrog     s->mac_reg[index] = val & 0xffff;
11147c23b892Sbalrog }
11157c23b892Sbalrog 
11167c23b892Sbalrog static void
11177c23b892Sbalrog set_dlen(E1000State *s, int index, uint32_t val)
11187c23b892Sbalrog {
11197c23b892Sbalrog     s->mac_reg[index] = val & 0xfff80;
11207c23b892Sbalrog }
11217c23b892Sbalrog 
11227c23b892Sbalrog static void
11237c23b892Sbalrog set_tctl(E1000State *s, int index, uint32_t val)
11247c23b892Sbalrog {
11257c23b892Sbalrog     s->mac_reg[index] = val;
11267c23b892Sbalrog     s->mac_reg[TDT] &= 0xffff;
11277c23b892Sbalrog     start_xmit(s);
11287c23b892Sbalrog }
11297c23b892Sbalrog 
11307c23b892Sbalrog static void
11317c23b892Sbalrog set_icr(E1000State *s, int index, uint32_t val)
11327c23b892Sbalrog {
11337c23b892Sbalrog     DBGOUT(INTERRUPT, "set_icr %x\n", val);
11347c23b892Sbalrog     set_interrupt_cause(s, 0, s->mac_reg[ICR] & ~val);
11357c23b892Sbalrog }
11367c23b892Sbalrog 
11377c23b892Sbalrog static void
11387c23b892Sbalrog set_imc(E1000State *s, int index, uint32_t val)
11397c23b892Sbalrog {
11407c23b892Sbalrog     s->mac_reg[IMS] &= ~val;
11417c23b892Sbalrog     set_ics(s, 0, 0);
11427c23b892Sbalrog }
11437c23b892Sbalrog 
11447c23b892Sbalrog static void
11457c23b892Sbalrog set_ims(E1000State *s, int index, uint32_t val)
11467c23b892Sbalrog {
11477c23b892Sbalrog     s->mac_reg[IMS] |= val;
11487c23b892Sbalrog     set_ics(s, 0, 0);
11497c23b892Sbalrog }
11507c23b892Sbalrog 
11517c23b892Sbalrog #define getreg(x)    [x] = mac_readreg
11527c23b892Sbalrog static uint32_t (*macreg_readops[])(E1000State *, int) = {
11537c23b892Sbalrog     getreg(PBA),      getreg(RCTL),     getreg(TDH),      getreg(TXDCTL),
11547c23b892Sbalrog     getreg(WUFC),     getreg(TDT),      getreg(CTRL),     getreg(LEDCTL),
11557c23b892Sbalrog     getreg(MANC),     getreg(MDIC),     getreg(SWSM),     getreg(STATUS),
11567c23b892Sbalrog     getreg(TORL),     getreg(TOTL),     getreg(IMS),      getreg(TCTL),
1157b1332393SBill Paul     getreg(RDH),      getreg(RDT),      getreg(VET),      getreg(ICS),
1158a00b2335SKay Ackermann     getreg(TDBAL),    getreg(TDBAH),    getreg(RDBAH),    getreg(RDBAL),
1159e9845f09SVincenzo Maffione     getreg(TDLEN),    getreg(RDLEN),    getreg(RDTR),     getreg(RADV),
116072ea771cSLeonid Bloch     getreg(TADV),     getreg(ITR),      getreg(FCRUC),    getreg(IPAV),
116172ea771cSLeonid Bloch     getreg(WUC),      getreg(WUS),      getreg(SCC),      getreg(ECOL),
116272ea771cSLeonid Bloch     getreg(MCC),      getreg(LATECOL),  getreg(COLC),     getreg(DC),
1163757704f1SKamil Rytarowski     getreg(TNCRS),    getreg(SEQEC),    getreg(CEXTERR),  getreg(RLEC),
116472ea771cSLeonid Bloch     getreg(XONRXC),   getreg(XONTXC),   getreg(XOFFRXC),  getreg(XOFFTXC),
116572ea771cSLeonid Bloch     getreg(RFC),      getreg(RJC),      getreg(RNBC),     getreg(TSCTFC),
11663b274301SLeonid Bloch     getreg(MGTPRC),   getreg(MGTPDC),   getreg(MGTPTC),   getreg(GORCL),
11673b274301SLeonid Bloch     getreg(GOTCL),
11687c23b892Sbalrog 
116920f3e863SLeonid Bloch     [TOTH]    = mac_read_clr8,      [TORH]    = mac_read_clr8,
11703b274301SLeonid Bloch     [GOTCH]   = mac_read_clr8,      [GORCH]   = mac_read_clr8,
11713b274301SLeonid Bloch     [PRC64]   = mac_read_clr4,      [PRC127]  = mac_read_clr4,
11723b274301SLeonid Bloch     [PRC255]  = mac_read_clr4,      [PRC511]  = mac_read_clr4,
11733b274301SLeonid Bloch     [PRC1023] = mac_read_clr4,      [PRC1522] = mac_read_clr4,
11743b274301SLeonid Bloch     [PTC64]   = mac_read_clr4,      [PTC127]  = mac_read_clr4,
11753b274301SLeonid Bloch     [PTC255]  = mac_read_clr4,      [PTC511]  = mac_read_clr4,
11763b274301SLeonid Bloch     [PTC1023] = mac_read_clr4,      [PTC1522] = mac_read_clr4,
117720f3e863SLeonid Bloch     [GPRC]    = mac_read_clr4,      [GPTC]    = mac_read_clr4,
117820f3e863SLeonid Bloch     [TPT]     = mac_read_clr4,      [TPR]     = mac_read_clr4,
11793b274301SLeonid Bloch     [RUC]     = mac_read_clr4,      [ROC]     = mac_read_clr4,
11803b274301SLeonid Bloch     [BPRC]    = mac_read_clr4,      [MPRC]    = mac_read_clr4,
11813b274301SLeonid Bloch     [TSCTC]   = mac_read_clr4,      [BPTC]    = mac_read_clr4,
11823b274301SLeonid Bloch     [MPTC]    = mac_read_clr4,
118320f3e863SLeonid Bloch     [ICR]     = mac_icr_read,       [EECD]    = get_eecd,
118420f3e863SLeonid Bloch     [EERD]    = flash_eerd_read,
118572ea771cSLeonid Bloch     [RDFH]    = mac_low13_read,     [RDFT]    = mac_low13_read,
118672ea771cSLeonid Bloch     [RDFHS]   = mac_low13_read,     [RDFTS]   = mac_low13_read,
118772ea771cSLeonid Bloch     [RDFPC]   = mac_low13_read,
118872ea771cSLeonid Bloch     [TDFH]    = mac_low11_read,     [TDFT]    = mac_low11_read,
118972ea771cSLeonid Bloch     [TDFHS]   = mac_low13_read,     [TDFTS]   = mac_low13_read,
119072ea771cSLeonid Bloch     [TDFPC]   = mac_low13_read,
119172ea771cSLeonid Bloch     [AIT]     = mac_low16_read,
119220f3e863SLeonid Bloch 
11937c23b892Sbalrog     [CRCERRS ... MPC]   = &mac_readreg,
119472ea771cSLeonid Bloch     [IP6AT ... IP6AT+3] = &mac_readreg,    [IP4AT ... IP4AT+6] = &mac_readreg,
119572ea771cSLeonid Bloch     [FFLT ... FFLT+6]   = &mac_low11_read,
11967c23b892Sbalrog     [RA ... RA+31]      = &mac_readreg,
119772ea771cSLeonid Bloch     [WUPM ... WUPM+31]  = &mac_readreg,
11987c23b892Sbalrog     [MTA ... MTA+127]   = &mac_readreg,
11998f2e8d1fSaliguori     [VFTA ... VFTA+127] = &mac_readreg,
120072ea771cSLeonid Bloch     [FFMT ... FFMT+254] = &mac_low4_read,
120172ea771cSLeonid Bloch     [FFVT ... FFVT+254] = &mac_readreg,
120272ea771cSLeonid Bloch     [PBM ... PBM+16383] = &mac_readreg,
12037c23b892Sbalrog };
1204b1503cdaSmalc enum { NREADOPS = ARRAY_SIZE(macreg_readops) };
12057c23b892Sbalrog 
12067c23b892Sbalrog #define putreg(x)    [x] = mac_writereg
12077c23b892Sbalrog static void (*macreg_writeops[])(E1000State *, int, uint32_t) = {
12087c23b892Sbalrog     putreg(PBA),      putreg(EERD),     putreg(SWSM),     putreg(WUFC),
12097c23b892Sbalrog     putreg(TDBAL),    putreg(TDBAH),    putreg(TXDCTL),   putreg(RDBAH),
121072ea771cSLeonid Bloch     putreg(RDBAL),    putreg(LEDCTL),   putreg(VET),      putreg(FCRUC),
121172ea771cSLeonid Bloch     putreg(TDFH),     putreg(TDFT),     putreg(TDFHS),    putreg(TDFTS),
121272ea771cSLeonid Bloch     putreg(TDFPC),    putreg(RDFH),     putreg(RDFT),     putreg(RDFHS),
121372ea771cSLeonid Bloch     putreg(RDFTS),    putreg(RDFPC),    putreg(IPAV),     putreg(WUC),
121472ea771cSLeonid Bloch     putreg(WUS),      putreg(AIT),
121520f3e863SLeonid Bloch 
12167c23b892Sbalrog     [TDLEN]  = set_dlen,   [RDLEN]  = set_dlen,       [TCTL] = set_tctl,
12177c23b892Sbalrog     [TDT]    = set_tctl,   [MDIC]   = set_mdic,       [ICS]  = set_ics,
12187c23b892Sbalrog     [TDH]    = set_16bit,  [RDH]    = set_16bit,      [RDT]  = set_rdt,
12197c23b892Sbalrog     [IMC]    = set_imc,    [IMS]    = set_ims,        [ICR]  = set_icr,
1220cab3c825SKevin Wolf     [EECD]   = set_eecd,   [RCTL]   = set_rx_control, [CTRL] = set_ctrl,
1221e9845f09SVincenzo Maffione     [RDTR]   = set_16bit,  [RADV]   = set_16bit,      [TADV] = set_16bit,
1222e9845f09SVincenzo Maffione     [ITR]    = set_16bit,
122320f3e863SLeonid Bloch 
122472ea771cSLeonid Bloch     [IP6AT ... IP6AT+3] = &mac_writereg, [IP4AT ... IP4AT+6] = &mac_writereg,
122572ea771cSLeonid Bloch     [FFLT ... FFLT+6]   = &mac_writereg,
12267c23b892Sbalrog     [RA ... RA+31]      = &mac_writereg,
122772ea771cSLeonid Bloch     [WUPM ... WUPM+31]  = &mac_writereg,
12287c23b892Sbalrog     [MTA ... MTA+127]   = &mac_writereg,
12298f2e8d1fSaliguori     [VFTA ... VFTA+127] = &mac_writereg,
123072ea771cSLeonid Bloch     [FFMT ... FFMT+254] = &mac_writereg, [FFVT ... FFVT+254] = &mac_writereg,
123172ea771cSLeonid Bloch     [PBM ... PBM+16383] = &mac_writereg,
12327c23b892Sbalrog };
1233b9d03e35SJason Wang 
1234b1503cdaSmalc enum { NWRITEOPS = ARRAY_SIZE(macreg_writeops) };
12357c23b892Sbalrog 
1236bc0f0674SLeonid Bloch enum { MAC_ACCESS_PARTIAL = 1, MAC_ACCESS_FLAG_NEEDED = 2 };
1237bc0f0674SLeonid Bloch 
1238bc0f0674SLeonid Bloch #define markflag(x)    ((E1000_FLAG_##x << 2) | MAC_ACCESS_FLAG_NEEDED)
1239bc0f0674SLeonid Bloch /* In the array below the meaning of the bits is: [f|f|f|f|f|f|n|p]
1240bc0f0674SLeonid Bloch  * f - flag bits (up to 6 possible flags)
1241bc0f0674SLeonid Bloch  * n - flag needed
1242bc0f0674SLeonid Bloch  * p - partially implenented */
1243bc0f0674SLeonid Bloch static const uint8_t mac_reg_access[0x8000] = {
1244bc0f0674SLeonid Bloch     [RDTR]    = markflag(MIT),    [TADV]    = markflag(MIT),
1245bc0f0674SLeonid Bloch     [RADV]    = markflag(MIT),    [ITR]     = markflag(MIT),
124672ea771cSLeonid Bloch 
124772ea771cSLeonid Bloch     [IPAV]    = markflag(MAC),    [WUC]     = markflag(MAC),
124872ea771cSLeonid Bloch     [IP6AT]   = markflag(MAC),    [IP4AT]   = markflag(MAC),
124972ea771cSLeonid Bloch     [FFVT]    = markflag(MAC),    [WUPM]    = markflag(MAC),
125072ea771cSLeonid Bloch     [ECOL]    = markflag(MAC),    [MCC]     = markflag(MAC),
125172ea771cSLeonid Bloch     [DC]      = markflag(MAC),    [TNCRS]   = markflag(MAC),
125272ea771cSLeonid Bloch     [RLEC]    = markflag(MAC),    [XONRXC]  = markflag(MAC),
125372ea771cSLeonid Bloch     [XOFFTXC] = markflag(MAC),    [RFC]     = markflag(MAC),
125472ea771cSLeonid Bloch     [TSCTFC]  = markflag(MAC),    [MGTPRC]  = markflag(MAC),
125572ea771cSLeonid Bloch     [WUS]     = markflag(MAC),    [AIT]     = markflag(MAC),
125672ea771cSLeonid Bloch     [FFLT]    = markflag(MAC),    [FFMT]    = markflag(MAC),
125772ea771cSLeonid Bloch     [SCC]     = markflag(MAC),    [FCRUC]   = markflag(MAC),
125872ea771cSLeonid Bloch     [LATECOL] = markflag(MAC),    [COLC]    = markflag(MAC),
1259757704f1SKamil Rytarowski     [SEQEC]   = markflag(MAC),    [CEXTERR] = markflag(MAC),
126072ea771cSLeonid Bloch     [XONTXC]  = markflag(MAC),    [XOFFRXC] = markflag(MAC),
126172ea771cSLeonid Bloch     [RJC]     = markflag(MAC),    [RNBC]    = markflag(MAC),
126272ea771cSLeonid Bloch     [MGTPDC]  = markflag(MAC),    [MGTPTC]  = markflag(MAC),
12633b274301SLeonid Bloch     [RUC]     = markflag(MAC),    [ROC]     = markflag(MAC),
12643b274301SLeonid Bloch     [GORCL]   = markflag(MAC),    [GORCH]   = markflag(MAC),
12653b274301SLeonid Bloch     [GOTCL]   = markflag(MAC),    [GOTCH]   = markflag(MAC),
12663b274301SLeonid Bloch     [BPRC]    = markflag(MAC),    [MPRC]    = markflag(MAC),
12673b274301SLeonid Bloch     [TSCTC]   = markflag(MAC),    [PRC64]   = markflag(MAC),
12683b274301SLeonid Bloch     [PRC127]  = markflag(MAC),    [PRC255]  = markflag(MAC),
12693b274301SLeonid Bloch     [PRC511]  = markflag(MAC),    [PRC1023] = markflag(MAC),
12703b274301SLeonid Bloch     [PRC1522] = markflag(MAC),    [PTC64]   = markflag(MAC),
12713b274301SLeonid Bloch     [PTC127]  = markflag(MAC),    [PTC255]  = markflag(MAC),
12723b274301SLeonid Bloch     [PTC511]  = markflag(MAC),    [PTC1023] = markflag(MAC),
12733b274301SLeonid Bloch     [PTC1522] = markflag(MAC),    [MPTC]    = markflag(MAC),
12743b274301SLeonid Bloch     [BPTC]    = markflag(MAC),
127572ea771cSLeonid Bloch 
127672ea771cSLeonid Bloch     [TDFH]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
127772ea771cSLeonid Bloch     [TDFT]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
127872ea771cSLeonid Bloch     [TDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
127972ea771cSLeonid Bloch     [TDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
128072ea771cSLeonid Bloch     [TDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL,
128172ea771cSLeonid Bloch     [RDFH]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
128272ea771cSLeonid Bloch     [RDFT]  = markflag(MAC) | MAC_ACCESS_PARTIAL,
128372ea771cSLeonid Bloch     [RDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
128472ea771cSLeonid Bloch     [RDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL,
128572ea771cSLeonid Bloch     [RDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL,
128672ea771cSLeonid Bloch     [PBM]   = markflag(MAC) | MAC_ACCESS_PARTIAL,
1287bc0f0674SLeonid Bloch };
1288bc0f0674SLeonid Bloch 
12897c23b892Sbalrog static void
1290a8170e5eSAvi Kivity e1000_mmio_write(void *opaque, hwaddr addr, uint64_t val,
1291ad00a9b9SAvi Kivity                  unsigned size)
12927c23b892Sbalrog {
12937c23b892Sbalrog     E1000State *s = opaque;
12948da3ff18Spbrook     unsigned int index = (addr & 0x1ffff) >> 2;
12957c23b892Sbalrog 
129643ad7e3eSJes Sorensen     if (index < NWRITEOPS && macreg_writeops[index]) {
1297bc0f0674SLeonid Bloch         if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED)
1298bc0f0674SLeonid Bloch             || (s->compat_flags & (mac_reg_access[index] >> 2))) {
1299bc0f0674SLeonid Bloch             if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
1300bc0f0674SLeonid Bloch                 DBGOUT(GENERAL, "Writing to register at offset: 0x%08x. "
1301bc0f0674SLeonid Bloch                        "It is not fully implemented.\n", index<<2);
1302bc0f0674SLeonid Bloch             }
13036b59fc74Saurel32             macreg_writeops[index](s, index, val);
1304bc0f0674SLeonid Bloch         } else {    /* "flag needed" bit is set, but the flag is not active */
1305bc0f0674SLeonid Bloch             DBGOUT(MMIO, "MMIO write attempt to disabled reg. addr=0x%08x\n",
1306bc0f0674SLeonid Bloch                    index<<2);
1307bc0f0674SLeonid Bloch         }
130843ad7e3eSJes Sorensen     } else if (index < NREADOPS && macreg_readops[index]) {
1309bc0f0674SLeonid Bloch         DBGOUT(MMIO, "e1000_mmio_writel RO %x: 0x%04"PRIx64"\n",
1310bc0f0674SLeonid Bloch                index<<2, val);
131143ad7e3eSJes Sorensen     } else {
1312ad00a9b9SAvi Kivity         DBGOUT(UNKNOWN, "MMIO unknown write addr=0x%08x,val=0x%08"PRIx64"\n",
13137c23b892Sbalrog                index<<2, val);
13147c23b892Sbalrog     }
131543ad7e3eSJes Sorensen }
13167c23b892Sbalrog 
1317ad00a9b9SAvi Kivity static uint64_t
1318a8170e5eSAvi Kivity e1000_mmio_read(void *opaque, hwaddr addr, unsigned size)
13197c23b892Sbalrog {
13207c23b892Sbalrog     E1000State *s = opaque;
13218da3ff18Spbrook     unsigned int index = (addr & 0x1ffff) >> 2;
13227c23b892Sbalrog 
1323bc0f0674SLeonid Bloch     if (index < NREADOPS && macreg_readops[index]) {
1324bc0f0674SLeonid Bloch         if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED)
1325bc0f0674SLeonid Bloch             || (s->compat_flags & (mac_reg_access[index] >> 2))) {
1326bc0f0674SLeonid Bloch             if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
1327bc0f0674SLeonid Bloch                 DBGOUT(GENERAL, "Reading register at offset: 0x%08x. "
1328bc0f0674SLeonid Bloch                        "It is not fully implemented.\n", index<<2);
13296b59fc74Saurel32             }
1330bc0f0674SLeonid Bloch             return macreg_readops[index](s, index);
1331bc0f0674SLeonid Bloch         } else {    /* "flag needed" bit is set, but the flag is not active */
1332bc0f0674SLeonid Bloch             DBGOUT(MMIO, "MMIO read attempt of disabled reg. addr=0x%08x\n",
1333bc0f0674SLeonid Bloch                    index<<2);
1334bc0f0674SLeonid Bloch         }
1335bc0f0674SLeonid Bloch     } else {
13367c23b892Sbalrog         DBGOUT(UNKNOWN, "MMIO unknown read addr=0x%08x\n", index<<2);
1337bc0f0674SLeonid Bloch     }
13387c23b892Sbalrog     return 0;
13397c23b892Sbalrog }
13407c23b892Sbalrog 
1341ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_mmio_ops = {
1342ad00a9b9SAvi Kivity     .read = e1000_mmio_read,
1343ad00a9b9SAvi Kivity     .write = e1000_mmio_write,
1344ad00a9b9SAvi Kivity     .endianness = DEVICE_LITTLE_ENDIAN,
1345ad00a9b9SAvi Kivity     .impl = {
1346ad00a9b9SAvi Kivity         .min_access_size = 4,
1347ad00a9b9SAvi Kivity         .max_access_size = 4,
1348ad00a9b9SAvi Kivity     },
1349ad00a9b9SAvi Kivity };
1350ad00a9b9SAvi Kivity 
1351a8170e5eSAvi Kivity static uint64_t e1000_io_read(void *opaque, hwaddr addr,
1352ad00a9b9SAvi Kivity                               unsigned size)
13537c23b892Sbalrog {
1354ad00a9b9SAvi Kivity     E1000State *s = opaque;
1355ad00a9b9SAvi Kivity 
1356ad00a9b9SAvi Kivity     (void)s;
1357ad00a9b9SAvi Kivity     return 0;
13587c23b892Sbalrog }
13597c23b892Sbalrog 
1360a8170e5eSAvi Kivity static void e1000_io_write(void *opaque, hwaddr addr,
1361ad00a9b9SAvi Kivity                            uint64_t val, unsigned size)
13627c23b892Sbalrog {
1363ad00a9b9SAvi Kivity     E1000State *s = opaque;
1364ad00a9b9SAvi Kivity 
1365ad00a9b9SAvi Kivity     (void)s;
13667c23b892Sbalrog }
13677c23b892Sbalrog 
1368ad00a9b9SAvi Kivity static const MemoryRegionOps e1000_io_ops = {
1369ad00a9b9SAvi Kivity     .read = e1000_io_read,
1370ad00a9b9SAvi Kivity     .write = e1000_io_write,
1371ad00a9b9SAvi Kivity     .endianness = DEVICE_LITTLE_ENDIAN,
1372ad00a9b9SAvi Kivity };
1373ad00a9b9SAvi Kivity 
1374e482dc3eSJuan Quintela static bool is_version_1(void *opaque, int version_id)
13757c23b892Sbalrog {
1376e482dc3eSJuan Quintela     return version_id == 1;
13777c23b892Sbalrog }
13787c23b892Sbalrog 
137944b1ff31SDr. David Alan Gilbert static int e1000_pre_save(void *opaque)
1380ddcb73b7SMichael S. Tsirkin {
1381ddcb73b7SMichael S. Tsirkin     E1000State *s = opaque;
1382ddcb73b7SMichael S. Tsirkin     NetClientState *nc = qemu_get_queue(s->nic);
13832af234e6SMichael S. Tsirkin 
1384e9845f09SVincenzo Maffione     /* If the mitigation timer is active, emulate a timeout now. */
1385e9845f09SVincenzo Maffione     if (s->mit_timer_on) {
1386e9845f09SVincenzo Maffione         e1000_mit_timer(s);
1387e9845f09SVincenzo Maffione     }
1388e9845f09SVincenzo Maffione 
1389ddcb73b7SMichael S. Tsirkin     /*
13906a2acedbSGabriel L. Somlo      * If link is down and auto-negotiation is supported and ongoing,
13916a2acedbSGabriel L. Somlo      * complete auto-negotiation immediately. This allows us to look
13926a2acedbSGabriel L. Somlo      * at MII_SR_AUTONEG_COMPLETE to infer link status on load.
1393ddcb73b7SMichael S. Tsirkin      */
1394d7a41552SGabriel L. Somlo     if (nc->link_down && have_autoneg(s)) {
1395ddcb73b7SMichael S. Tsirkin         s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
1396ddcb73b7SMichael S. Tsirkin     }
139744b1ff31SDr. David Alan Gilbert 
1398ff214d42SDr. David Alan Gilbert     /* Decide which set of props to migrate in the main structure */
1399ff214d42SDr. David Alan Gilbert     if (chkflag(TSO) || !s->use_tso_for_migration) {
1400ff214d42SDr. David Alan Gilbert         /* Either we're migrating with the extra subsection, in which
1401ff214d42SDr. David Alan Gilbert          * case the mig_props is always 'props' OR
1402ff214d42SDr. David Alan Gilbert          * we've not got the subsection, but 'props' was the last
1403ff214d42SDr. David Alan Gilbert          * updated.
1404ff214d42SDr. David Alan Gilbert          */
140559354484SDr. David Alan Gilbert         s->mig_props = s->tx.props;
1406ff214d42SDr. David Alan Gilbert     } else {
1407ff214d42SDr. David Alan Gilbert         /* We're not using the subsection, and 'tso_props' was
1408ff214d42SDr. David Alan Gilbert          * the last updated.
1409ff214d42SDr. David Alan Gilbert          */
1410ff214d42SDr. David Alan Gilbert         s->mig_props = s->tx.tso_props;
1411ff214d42SDr. David Alan Gilbert     }
141244b1ff31SDr. David Alan Gilbert     return 0;
1413ddcb73b7SMichael S. Tsirkin }
1414ddcb73b7SMichael S. Tsirkin 
1415e4b82364SAmos Kong static int e1000_post_load(void *opaque, int version_id)
1416e4b82364SAmos Kong {
1417e4b82364SAmos Kong     E1000State *s = opaque;
1418b356f76dSJason Wang     NetClientState *nc = qemu_get_queue(s->nic);
1419e4b82364SAmos Kong 
1420bc0f0674SLeonid Bloch     if (!chkflag(MIT)) {
1421e9845f09SVincenzo Maffione         s->mac_reg[ITR] = s->mac_reg[RDTR] = s->mac_reg[RADV] =
1422e9845f09SVincenzo Maffione             s->mac_reg[TADV] = 0;
1423e9845f09SVincenzo Maffione         s->mit_irq_level = false;
1424e9845f09SVincenzo Maffione     }
1425e9845f09SVincenzo Maffione     s->mit_ide = 0;
1426e9845f09SVincenzo Maffione     s->mit_timer_on = false;
1427e9845f09SVincenzo Maffione 
1428e4b82364SAmos Kong     /* nc.link_down can't be migrated, so infer link_down according
1429ddcb73b7SMichael S. Tsirkin      * to link status bit in mac_reg[STATUS].
1430ddcb73b7SMichael S. Tsirkin      * Alternatively, restart link negotiation if it was in progress. */
1431b356f76dSJason Wang     nc->link_down = (s->mac_reg[STATUS] & E1000_STATUS_LU) == 0;
14322af234e6SMichael S. Tsirkin 
1433d7a41552SGabriel L. Somlo     if (have_autoneg(s) &&
1434ddcb73b7SMichael S. Tsirkin         !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
1435ddcb73b7SMichael S. Tsirkin         nc->link_down = false;
1436d7a41552SGabriel L. Somlo         timer_mod(s->autoneg_timer,
1437d7a41552SGabriel L. Somlo                   qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
1438ddcb73b7SMichael S. Tsirkin     }
1439e4b82364SAmos Kong 
144059354484SDr. David Alan Gilbert     s->tx.props = s->mig_props;
14413c4053c5SDr. David Alan Gilbert     if (!s->received_tx_tso) {
14423c4053c5SDr. David Alan Gilbert         /* We received only one set of offload data (tx.props)
14433c4053c5SDr. David Alan Gilbert          * and haven't got tx.tso_props.  The best we can do
14443c4053c5SDr. David Alan Gilbert          * is dupe the data.
14453c4053c5SDr. David Alan Gilbert          */
144659354484SDr. David Alan Gilbert         s->tx.tso_props = s->mig_props;
14473c4053c5SDr. David Alan Gilbert     }
14483c4053c5SDr. David Alan Gilbert     return 0;
14493c4053c5SDr. David Alan Gilbert }
14503c4053c5SDr. David Alan Gilbert 
14513c4053c5SDr. David Alan Gilbert static int e1000_tx_tso_post_load(void *opaque, int version_id)
14523c4053c5SDr. David Alan Gilbert {
14533c4053c5SDr. David Alan Gilbert     E1000State *s = opaque;
14543c4053c5SDr. David Alan Gilbert     s->received_tx_tso = true;
1455e4b82364SAmos Kong     return 0;
1456e4b82364SAmos Kong }
1457e4b82364SAmos Kong 
1458e9845f09SVincenzo Maffione static bool e1000_mit_state_needed(void *opaque)
1459e9845f09SVincenzo Maffione {
1460e9845f09SVincenzo Maffione     E1000State *s = opaque;
1461e9845f09SVincenzo Maffione 
1462bc0f0674SLeonid Bloch     return chkflag(MIT);
1463e9845f09SVincenzo Maffione }
1464e9845f09SVincenzo Maffione 
14659e117734SLeonid Bloch static bool e1000_full_mac_needed(void *opaque)
14669e117734SLeonid Bloch {
14679e117734SLeonid Bloch     E1000State *s = opaque;
14689e117734SLeonid Bloch 
1469bc0f0674SLeonid Bloch     return chkflag(MAC);
14709e117734SLeonid Bloch }
14719e117734SLeonid Bloch 
147246f2a9ecSDr. David Alan Gilbert static bool e1000_tso_state_needed(void *opaque)
147346f2a9ecSDr. David Alan Gilbert {
147446f2a9ecSDr. David Alan Gilbert     E1000State *s = opaque;
147546f2a9ecSDr. David Alan Gilbert 
147646f2a9ecSDr. David Alan Gilbert     return chkflag(TSO);
147746f2a9ecSDr. David Alan Gilbert }
147846f2a9ecSDr. David Alan Gilbert 
1479e9845f09SVincenzo Maffione static const VMStateDescription vmstate_e1000_mit_state = {
1480e9845f09SVincenzo Maffione     .name = "e1000/mit_state",
1481e9845f09SVincenzo Maffione     .version_id = 1,
1482e9845f09SVincenzo Maffione     .minimum_version_id = 1,
14835cd8cadaSJuan Quintela     .needed = e1000_mit_state_needed,
1484e9845f09SVincenzo Maffione     .fields = (VMStateField[]) {
1485e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[RDTR], E1000State),
1486e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[RADV], E1000State),
1487e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[TADV], E1000State),
1488e9845f09SVincenzo Maffione         VMSTATE_UINT32(mac_reg[ITR], E1000State),
1489e9845f09SVincenzo Maffione         VMSTATE_BOOL(mit_irq_level, E1000State),
1490e9845f09SVincenzo Maffione         VMSTATE_END_OF_LIST()
1491e9845f09SVincenzo Maffione     }
1492e9845f09SVincenzo Maffione };
1493e9845f09SVincenzo Maffione 
14949e117734SLeonid Bloch static const VMStateDescription vmstate_e1000_full_mac_state = {
14959e117734SLeonid Bloch     .name = "e1000/full_mac_state",
14969e117734SLeonid Bloch     .version_id = 1,
14979e117734SLeonid Bloch     .minimum_version_id = 1,
14989e117734SLeonid Bloch     .needed = e1000_full_mac_needed,
14999e117734SLeonid Bloch     .fields = (VMStateField[]) {
15009e117734SLeonid Bloch         VMSTATE_UINT32_ARRAY(mac_reg, E1000State, 0x8000),
15019e117734SLeonid Bloch         VMSTATE_END_OF_LIST()
15029e117734SLeonid Bloch     }
15039e117734SLeonid Bloch };
15049e117734SLeonid Bloch 
15054ae4bf5bSDr. David Alan Gilbert static const VMStateDescription vmstate_e1000_tx_tso_state = {
15064ae4bf5bSDr. David Alan Gilbert     .name = "e1000/tx_tso_state",
15074ae4bf5bSDr. David Alan Gilbert     .version_id = 1,
15084ae4bf5bSDr. David Alan Gilbert     .minimum_version_id = 1,
150946f2a9ecSDr. David Alan Gilbert     .needed = e1000_tso_state_needed,
15103c4053c5SDr. David Alan Gilbert     .post_load = e1000_tx_tso_post_load,
15114ae4bf5bSDr. David Alan Gilbert     .fields = (VMStateField[]) {
15124ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT8(tx.tso_props.ipcss, E1000State),
15134ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT8(tx.tso_props.ipcso, E1000State),
15144ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT16(tx.tso_props.ipcse, E1000State),
15154ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT8(tx.tso_props.tucss, E1000State),
15164ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT8(tx.tso_props.tucso, E1000State),
15174ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT16(tx.tso_props.tucse, E1000State),
15184ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT32(tx.tso_props.paylen, E1000State),
15194ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT8(tx.tso_props.hdr_len, E1000State),
15204ae4bf5bSDr. David Alan Gilbert         VMSTATE_UINT16(tx.tso_props.mss, E1000State),
15214ae4bf5bSDr. David Alan Gilbert         VMSTATE_INT8(tx.tso_props.ip, E1000State),
15224ae4bf5bSDr. David Alan Gilbert         VMSTATE_INT8(tx.tso_props.tcp, E1000State),
15234ae4bf5bSDr. David Alan Gilbert         VMSTATE_END_OF_LIST()
15244ae4bf5bSDr. David Alan Gilbert     }
15254ae4bf5bSDr. David Alan Gilbert };
15264ae4bf5bSDr. David Alan Gilbert 
1527e482dc3eSJuan Quintela static const VMStateDescription vmstate_e1000 = {
1528e482dc3eSJuan Quintela     .name = "e1000",
15294ae4bf5bSDr. David Alan Gilbert     .version_id = 2,
1530e482dc3eSJuan Quintela     .minimum_version_id = 1,
1531ddcb73b7SMichael S. Tsirkin     .pre_save = e1000_pre_save,
1532e4b82364SAmos Kong     .post_load = e1000_post_load,
1533e482dc3eSJuan Quintela     .fields = (VMStateField[]) {
1534b08340d5SAndreas Färber         VMSTATE_PCI_DEVICE(parent_obj, E1000State),
1535e482dc3eSJuan Quintela         VMSTATE_UNUSED_TEST(is_version_1, 4), /* was instance id */
1536e482dc3eSJuan Quintela         VMSTATE_UNUSED(4), /* Was mmio_base.  */
1537e482dc3eSJuan Quintela         VMSTATE_UINT32(rxbuf_size, E1000State),
1538e482dc3eSJuan Quintela         VMSTATE_UINT32(rxbuf_min_shift, E1000State),
1539e482dc3eSJuan Quintela         VMSTATE_UINT32(eecd_state.val_in, E1000State),
1540e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.bitnum_in, E1000State),
1541e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.bitnum_out, E1000State),
1542e482dc3eSJuan Quintela         VMSTATE_UINT16(eecd_state.reading, E1000State),
1543e482dc3eSJuan Quintela         VMSTATE_UINT32(eecd_state.old_eecd, E1000State),
154459354484SDr. David Alan Gilbert         VMSTATE_UINT8(mig_props.ipcss, E1000State),
154559354484SDr. David Alan Gilbert         VMSTATE_UINT8(mig_props.ipcso, E1000State),
154659354484SDr. David Alan Gilbert         VMSTATE_UINT16(mig_props.ipcse, E1000State),
154759354484SDr. David Alan Gilbert         VMSTATE_UINT8(mig_props.tucss, E1000State),
154859354484SDr. David Alan Gilbert         VMSTATE_UINT8(mig_props.tucso, E1000State),
154959354484SDr. David Alan Gilbert         VMSTATE_UINT16(mig_props.tucse, E1000State),
155059354484SDr. David Alan Gilbert         VMSTATE_UINT32(mig_props.paylen, E1000State),
155159354484SDr. David Alan Gilbert         VMSTATE_UINT8(mig_props.hdr_len, E1000State),
155259354484SDr. David Alan Gilbert         VMSTATE_UINT16(mig_props.mss, E1000State),
1553e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.size, E1000State),
1554e482dc3eSJuan Quintela         VMSTATE_UINT16(tx.tso_frames, E1000State),
15557d08c73eSEd Swierk via Qemu-devel         VMSTATE_UINT8(tx.sum_needed, E1000State),
155659354484SDr. David Alan Gilbert         VMSTATE_INT8(mig_props.ip, E1000State),
155759354484SDr. David Alan Gilbert         VMSTATE_INT8(mig_props.tcp, E1000State),
1558e482dc3eSJuan Quintela         VMSTATE_BUFFER(tx.header, E1000State),
1559e482dc3eSJuan Quintela         VMSTATE_BUFFER(tx.data, E1000State),
1560e482dc3eSJuan Quintela         VMSTATE_UINT16_ARRAY(eeprom_data, E1000State, 64),
1561e482dc3eSJuan Quintela         VMSTATE_UINT16_ARRAY(phy_reg, E1000State, 0x20),
1562e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[CTRL], E1000State),
1563e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[EECD], E1000State),
1564e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[EERD], E1000State),
1565e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[GPRC], E1000State),
1566e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[GPTC], E1000State),
1567e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[ICR], E1000State),
1568e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[ICS], E1000State),
1569e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[IMC], E1000State),
1570e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[IMS], E1000State),
1571e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[LEDCTL], E1000State),
1572e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MANC], E1000State),
1573e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MDIC], E1000State),
1574e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[MPC], E1000State),
1575e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[PBA], E1000State),
1576e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RCTL], E1000State),
1577e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDBAH], E1000State),
1578e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDBAL], E1000State),
1579e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDH], E1000State),
1580e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDLEN], E1000State),
1581e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[RDT], E1000State),
1582e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[STATUS], E1000State),
1583e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[SWSM], E1000State),
1584e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TCTL], E1000State),
1585e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDBAH], E1000State),
1586e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDBAL], E1000State),
1587e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDH], E1000State),
1588e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDLEN], E1000State),
1589e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TDT], E1000State),
1590e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TORH], E1000State),
1591e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TORL], E1000State),
1592e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TOTH], E1000State),
1593e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TOTL], E1000State),
1594e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TPR], E1000State),
1595e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TPT], E1000State),
1596e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[TXDCTL], E1000State),
1597e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[WUFC], E1000State),
1598e482dc3eSJuan Quintela         VMSTATE_UINT32(mac_reg[VET], E1000State),
1599e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, RA, 32),
1600e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, MTA, 128),
1601e482dc3eSJuan Quintela         VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, VFTA, 128),
1602e482dc3eSJuan Quintela         VMSTATE_END_OF_LIST()
1603e9845f09SVincenzo Maffione     },
16045cd8cadaSJuan Quintela     .subsections = (const VMStateDescription*[]) {
16055cd8cadaSJuan Quintela         &vmstate_e1000_mit_state,
16069e117734SLeonid Bloch         &vmstate_e1000_full_mac_state,
16074ae4bf5bSDr. David Alan Gilbert         &vmstate_e1000_tx_tso_state,
16085cd8cadaSJuan Quintela         NULL
16097c23b892Sbalrog     }
1610e482dc3eSJuan Quintela };
16117c23b892Sbalrog 
16128597f2e1SGabriel L. Somlo /*
16138597f2e1SGabriel L. Somlo  * EEPROM contents documented in Tables 5-2 and 5-3, pp. 98-102.
16148597f2e1SGabriel L. Somlo  * Note: A valid DevId will be inserted during pci_e1000_init().
16158597f2e1SGabriel L. Somlo  */
161688b4e9dbSblueswir1 static const uint16_t e1000_eeprom_template[64] = {
16177c23b892Sbalrog     0x0000, 0x0000, 0x0000, 0x0000,      0xffff, 0x0000,      0x0000, 0x0000,
16188597f2e1SGabriel L. Somlo     0x3000, 0x1000, 0x6403, 0 /*DevId*/, 0x8086, 0 /*DevId*/, 0x8086, 0x3040,
16197c23b892Sbalrog     0x0008, 0x2000, 0x7e14, 0x0048,      0x1000, 0x00d8,      0x0000, 0x2700,
16207c23b892Sbalrog     0x6cc9, 0x3150, 0x0722, 0x040b,      0x0984, 0x0000,      0xc000, 0x0706,
16217c23b892Sbalrog     0x1008, 0x0000, 0x0f04, 0x7fff,      0x4d01, 0xffff,      0xffff, 0xffff,
16227c23b892Sbalrog     0xffff, 0xffff, 0xffff, 0xffff,      0xffff, 0xffff,      0xffff, 0xffff,
16237c23b892Sbalrog     0x0100, 0x4000, 0x121c, 0xffff,      0xffff, 0xffff,      0xffff, 0xffff,
16247c23b892Sbalrog     0xffff, 0xffff, 0xffff, 0xffff,      0xffff, 0xffff,      0xffff, 0x0000,
16257c23b892Sbalrog };
16267c23b892Sbalrog 
16277c23b892Sbalrog /* PCI interface */
16287c23b892Sbalrog 
16297c23b892Sbalrog static void
1630ad00a9b9SAvi Kivity e1000_mmio_setup(E1000State *d)
16317c23b892Sbalrog {
1632f65ed4c1Saliguori     int i;
1633f65ed4c1Saliguori     const uint32_t excluded_regs[] = {
1634f65ed4c1Saliguori         E1000_MDIC, E1000_ICR, E1000_ICS, E1000_IMS,
1635f65ed4c1Saliguori         E1000_IMC, E1000_TCTL, E1000_TDT, PNPMMIO_SIZE
1636f65ed4c1Saliguori     };
1637f65ed4c1Saliguori 
1638eedfac6fSPaolo Bonzini     memory_region_init_io(&d->mmio, OBJECT(d), &e1000_mmio_ops, d,
1639eedfac6fSPaolo Bonzini                           "e1000-mmio", PNPMMIO_SIZE);
1640ad00a9b9SAvi Kivity     memory_region_add_coalescing(&d->mmio, 0, excluded_regs[0]);
1641f65ed4c1Saliguori     for (i = 0; excluded_regs[i] != PNPMMIO_SIZE; i++)
1642ad00a9b9SAvi Kivity         memory_region_add_coalescing(&d->mmio, excluded_regs[i] + 4,
1643ad00a9b9SAvi Kivity                                      excluded_regs[i+1] - excluded_regs[i] - 4);
1644eedfac6fSPaolo Bonzini     memory_region_init_io(&d->io, OBJECT(d), &e1000_io_ops, d, "e1000-io", IOPORT_SIZE);
16457c23b892Sbalrog }
16467c23b892Sbalrog 
1647b946a153Saliguori static void
16484b09be85Saliguori pci_e1000_uninit(PCIDevice *dev)
16494b09be85Saliguori {
1650567a3c9eSPeter Crosthwaite     E1000State *d = E1000(dev);
16514b09be85Saliguori 
1652bc72ad67SAlex Bligh     timer_del(d->autoneg_timer);
1653bc72ad67SAlex Bligh     timer_free(d->autoneg_timer);
1654e9845f09SVincenzo Maffione     timer_del(d->mit_timer);
1655e9845f09SVincenzo Maffione     timer_free(d->mit_timer);
1656157628d0Syuchenlin     timer_del(d->flush_queue_timer);
1657157628d0Syuchenlin     timer_free(d->flush_queue_timer);
1658948ecf21SJason Wang     qemu_del_nic(d->nic);
16594b09be85Saliguori }
16604b09be85Saliguori 
1661a03e2aecSMark McLoughlin static NetClientInfo net_e1000_info = {
1662f394b2e2SEric Blake     .type = NET_CLIENT_DRIVER_NIC,
1663a03e2aecSMark McLoughlin     .size = sizeof(NICState),
1664a03e2aecSMark McLoughlin     .can_receive = e1000_can_receive,
1665a03e2aecSMark McLoughlin     .receive = e1000_receive,
166697410ddeSVincenzo Maffione     .receive_iov = e1000_receive_iov,
1667a03e2aecSMark McLoughlin     .link_status_changed = e1000_set_link_status,
1668a03e2aecSMark McLoughlin };
1669a03e2aecSMark McLoughlin 
167020302e71SMichael S. Tsirkin static void e1000_write_config(PCIDevice *pci_dev, uint32_t address,
167120302e71SMichael S. Tsirkin                                 uint32_t val, int len)
167220302e71SMichael S. Tsirkin {
167320302e71SMichael S. Tsirkin     E1000State *s = E1000(pci_dev);
167420302e71SMichael S. Tsirkin 
167520302e71SMichael S. Tsirkin     pci_default_write_config(pci_dev, address, val, len);
167620302e71SMichael S. Tsirkin 
167720302e71SMichael S. Tsirkin     if (range_covers_byte(address, len, PCI_COMMAND) &&
167820302e71SMichael S. Tsirkin         (pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
167920302e71SMichael S. Tsirkin         qemu_flush_queued_packets(qemu_get_queue(s->nic));
168020302e71SMichael S. Tsirkin     }
168120302e71SMichael S. Tsirkin }
168220302e71SMichael S. Tsirkin 
16839af21dbeSMarkus Armbruster static void pci_e1000_realize(PCIDevice *pci_dev, Error **errp)
16847c23b892Sbalrog {
1685567a3c9eSPeter Crosthwaite     DeviceState *dev = DEVICE(pci_dev);
1686567a3c9eSPeter Crosthwaite     E1000State *d = E1000(pci_dev);
16877c23b892Sbalrog     uint8_t *pci_conf;
1688fbdaa002SGerd Hoffmann     uint8_t *macaddr;
1689aff427a1SChris Wright 
169020302e71SMichael S. Tsirkin     pci_dev->config_write = e1000_write_config;
169120302e71SMichael S. Tsirkin 
1692b08340d5SAndreas Färber     pci_conf = pci_dev->config;
16937c23b892Sbalrog 
1694a9cbacb0SMichael S. Tsirkin     /* TODO: RST# value should be 0, PCI spec 6.2.4 */
1695a9cbacb0SMichael S. Tsirkin     pci_conf[PCI_CACHE_LINE_SIZE] = 0x10;
16967c23b892Sbalrog 
1697817e0b6fSMichael S. Tsirkin     pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
16987c23b892Sbalrog 
1699ad00a9b9SAvi Kivity     e1000_mmio_setup(d);
17007c23b892Sbalrog 
1701b08340d5SAndreas Färber     pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
17027c23b892Sbalrog 
1703b08340d5SAndreas Färber     pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->io);
17047c23b892Sbalrog 
1705fbdaa002SGerd Hoffmann     qemu_macaddr_default_if_unset(&d->conf.macaddr);
1706fbdaa002SGerd Hoffmann     macaddr = d->conf.macaddr.a;
1707093454e2SDmitry Fleytman 
1708093454e2SDmitry Fleytman     e1000x_core_prepare_eeprom(d->eeprom_data,
1709093454e2SDmitry Fleytman                                e1000_eeprom_template,
1710093454e2SDmitry Fleytman                                sizeof(e1000_eeprom_template),
1711093454e2SDmitry Fleytman                                PCI_DEVICE_GET_CLASS(pci_dev)->device_id,
1712093454e2SDmitry Fleytman                                macaddr);
17137c23b892Sbalrog 
1714a03e2aecSMark McLoughlin     d->nic = qemu_new_nic(&net_e1000_info, &d->conf,
1715567a3c9eSPeter Crosthwaite                           object_get_typename(OBJECT(d)), dev->id, d);
17167c23b892Sbalrog 
1717b356f76dSJason Wang     qemu_format_nic_info_str(qemu_get_queue(d->nic), macaddr);
17181ca4d09aSGleb Natapov 
1719bc72ad67SAlex Bligh     d->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, e1000_autoneg_timer, d);
1720e9845f09SVincenzo Maffione     d->mit_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000_mit_timer, d);
1721157628d0Syuchenlin     d->flush_queue_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
1722157628d0Syuchenlin                                         e1000_flush_queue_timer, d);
17237c23b892Sbalrog }
17249d07d757SPaul Brook 
1725fbdaa002SGerd Hoffmann static void qdev_e1000_reset(DeviceState *dev)
1726fbdaa002SGerd Hoffmann {
1727567a3c9eSPeter Crosthwaite     E1000State *d = E1000(dev);
1728fbdaa002SGerd Hoffmann     e1000_reset(d);
1729fbdaa002SGerd Hoffmann }
1730fbdaa002SGerd Hoffmann 
173140021f08SAnthony Liguori static Property e1000_properties[] = {
1732fbdaa002SGerd Hoffmann     DEFINE_NIC_PROPERTIES(E1000State, conf),
17332af234e6SMichael S. Tsirkin     DEFINE_PROP_BIT("autonegotiation", E1000State,
17342af234e6SMichael S. Tsirkin                     compat_flags, E1000_FLAG_AUTONEG_BIT, true),
1735e9845f09SVincenzo Maffione     DEFINE_PROP_BIT("mitigation", E1000State,
1736e9845f09SVincenzo Maffione                     compat_flags, E1000_FLAG_MIT_BIT, true),
1737ba63ec85SLeonid Bloch     DEFINE_PROP_BIT("extra_mac_registers", E1000State,
1738ba63ec85SLeonid Bloch                     compat_flags, E1000_FLAG_MAC_BIT, true),
173946f2a9ecSDr. David Alan Gilbert     DEFINE_PROP_BIT("migrate_tso_props", E1000State,
174046f2a9ecSDr. David Alan Gilbert                     compat_flags, E1000_FLAG_TSO_BIT, true),
1741fbdaa002SGerd Hoffmann     DEFINE_PROP_END_OF_LIST(),
174240021f08SAnthony Liguori };
174340021f08SAnthony Liguori 
17448597f2e1SGabriel L. Somlo typedef struct E1000Info {
17458597f2e1SGabriel L. Somlo     const char *name;
17468597f2e1SGabriel L. Somlo     uint16_t   device_id;
17478597f2e1SGabriel L. Somlo     uint8_t    revision;
17488597f2e1SGabriel L. Somlo     uint16_t   phy_id2;
17498597f2e1SGabriel L. Somlo } E1000Info;
17508597f2e1SGabriel L. Somlo 
175140021f08SAnthony Liguori static void e1000_class_init(ObjectClass *klass, void *data)
175240021f08SAnthony Liguori {
175339bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
175440021f08SAnthony Liguori     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
17558597f2e1SGabriel L. Somlo     E1000BaseClass *e = E1000_DEVICE_CLASS(klass);
17568597f2e1SGabriel L. Somlo     const E1000Info *info = data;
175740021f08SAnthony Liguori 
17589af21dbeSMarkus Armbruster     k->realize = pci_e1000_realize;
175940021f08SAnthony Liguori     k->exit = pci_e1000_uninit;
1760c45e5b5bSGerd Hoffmann     k->romfile = "efi-e1000.rom";
176140021f08SAnthony Liguori     k->vendor_id = PCI_VENDOR_ID_INTEL;
17628597f2e1SGabriel L. Somlo     k->device_id = info->device_id;
17638597f2e1SGabriel L. Somlo     k->revision = info->revision;
17648597f2e1SGabriel L. Somlo     e->phy_id2 = info->phy_id2;
176540021f08SAnthony Liguori     k->class_id = PCI_CLASS_NETWORK_ETHERNET;
1766125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
176739bffca2SAnthony Liguori     dc->desc = "Intel Gigabit Ethernet";
176839bffca2SAnthony Liguori     dc->reset = qdev_e1000_reset;
176939bffca2SAnthony Liguori     dc->vmsd = &vmstate_e1000;
177039bffca2SAnthony Liguori     dc->props = e1000_properties;
1771fbdaa002SGerd Hoffmann }
177240021f08SAnthony Liguori 
17735df3bf62SGonglei static void e1000_instance_init(Object *obj)
17745df3bf62SGonglei {
17755df3bf62SGonglei     E1000State *n = E1000(obj);
17765df3bf62SGonglei     device_add_bootindex_property(obj, &n->conf.bootindex,
17775df3bf62SGonglei                                   "bootindex", "/ethernet-phy@0",
17785df3bf62SGonglei                                   DEVICE(n), NULL);
17795df3bf62SGonglei }
17805df3bf62SGonglei 
17818597f2e1SGabriel L. Somlo static const TypeInfo e1000_base_info = {
17828597f2e1SGabriel L. Somlo     .name          = TYPE_E1000_BASE,
178339bffca2SAnthony Liguori     .parent        = TYPE_PCI_DEVICE,
178439bffca2SAnthony Liguori     .instance_size = sizeof(E1000State),
17855df3bf62SGonglei     .instance_init = e1000_instance_init,
17868597f2e1SGabriel L. Somlo     .class_size    = sizeof(E1000BaseClass),
17878597f2e1SGabriel L. Somlo     .abstract      = true,
1788fd3b02c8SEduardo Habkost     .interfaces = (InterfaceInfo[]) {
1789fd3b02c8SEduardo Habkost         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
1790fd3b02c8SEduardo Habkost         { },
1791fd3b02c8SEduardo Habkost     },
17928597f2e1SGabriel L. Somlo };
17938597f2e1SGabriel L. Somlo 
17948597f2e1SGabriel L. Somlo static const E1000Info e1000_devices[] = {
17958597f2e1SGabriel L. Somlo     {
179683044020SJason Wang         .name      = "e1000",
17978597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82540EM,
17988597f2e1SGabriel L. Somlo         .revision  = 0x03,
17998597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_8254xx_DEFAULT,
18008597f2e1SGabriel L. Somlo     },
18018597f2e1SGabriel L. Somlo     {
18028597f2e1SGabriel L. Somlo         .name      = "e1000-82544gc",
18038597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82544GC_COPPER,
18048597f2e1SGabriel L. Somlo         .revision  = 0x03,
18058597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_82544x,
18068597f2e1SGabriel L. Somlo     },
18078597f2e1SGabriel L. Somlo     {
18088597f2e1SGabriel L. Somlo         .name      = "e1000-82545em",
18098597f2e1SGabriel L. Somlo         .device_id = E1000_DEV_ID_82545EM_COPPER,
18108597f2e1SGabriel L. Somlo         .revision  = 0x03,
18118597f2e1SGabriel L. Somlo         .phy_id2   = E1000_PHY_ID2_8254xx_DEFAULT,
18128597f2e1SGabriel L. Somlo     },
18138597f2e1SGabriel L. Somlo };
18148597f2e1SGabriel L. Somlo 
181583f7d43aSAndreas Färber static void e1000_register_types(void)
18169d07d757SPaul Brook {
18178597f2e1SGabriel L. Somlo     int i;
18188597f2e1SGabriel L. Somlo 
18198597f2e1SGabriel L. Somlo     type_register_static(&e1000_base_info);
18208597f2e1SGabriel L. Somlo     for (i = 0; i < ARRAY_SIZE(e1000_devices); i++) {
18218597f2e1SGabriel L. Somlo         const E1000Info *info = &e1000_devices[i];
18228597f2e1SGabriel L. Somlo         TypeInfo type_info = {};
18238597f2e1SGabriel L. Somlo 
18248597f2e1SGabriel L. Somlo         type_info.name = info->name;
18258597f2e1SGabriel L. Somlo         type_info.parent = TYPE_E1000_BASE;
18268597f2e1SGabriel L. Somlo         type_info.class_data = (void *)info;
18278597f2e1SGabriel L. Somlo         type_info.class_init = e1000_class_init;
18285df3bf62SGonglei         type_info.instance_init = e1000_instance_init;
18298597f2e1SGabriel L. Somlo 
18308597f2e1SGabriel L. Somlo         type_register(&type_info);
18318597f2e1SGabriel L. Somlo     }
18329d07d757SPaul Brook }
18339d07d757SPaul Brook 
183483f7d43aSAndreas Färber type_init(e1000_register_types)
1835