]>
Commit | Line | Data |
---|---|---|
7c23b892 AZ |
1 | /* |
2 | * QEMU e1000 emulation | |
3 | * | |
2758aa52 MT |
4 | * Software developer's manual: |
5 | * http://download.intel.com/design/network/manuals/8254x_GBe_SDM.pdf | |
6 | * | |
7c23b892 AZ |
7 | * Nir Peleg, Tutis Systems Ltd. for Qumranet Inc. |
8 | * Copyright (c) 2008 Qumranet | |
9 | * Based on work done by: | |
10 | * Copyright (c) 2007 Dan Aloni | |
11 | * Copyright (c) 2004 Antony T Curtis | |
12 | * | |
13 | * This library is free software; you can redistribute it and/or | |
14 | * modify it under the terms of the GNU Lesser General Public | |
15 | * License as published by the Free Software Foundation; either | |
61f3c91a | 16 | * version 2.1 of the License, or (at your option) any later version. |
7c23b892 AZ |
17 | * |
18 | * This library is distributed in the hope that it will be useful, | |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
21 | * Lesser General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU Lesser General Public | |
8167ee88 | 24 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
7c23b892 AZ |
25 | */ |
26 | ||
27 | ||
e8d40465 | 28 | #include "qemu/osdep.h" |
83c9f4ca | 29 | #include "hw/pci/pci.h" |
a27bd6c7 | 30 | #include "hw/qdev-properties.h" |
d6454270 | 31 | #include "migration/vmstate.h" |
1422e32d | 32 | #include "net/net.h" |
7200ac3c | 33 | #include "net/checksum.h" |
9c17d615 PB |
34 | #include "sysemu/sysemu.h" |
35 | #include "sysemu/dma.h" | |
97410dde | 36 | #include "qemu/iov.h" |
0b8fa32f | 37 | #include "qemu/module.h" |
20302e71 | 38 | #include "qemu/range.h" |
7c23b892 | 39 | |
093454e2 | 40 | #include "e1000x_common.h" |
1001cf45 | 41 | #include "trace.h" |
db1015e9 | 42 | #include "qom/object.h" |
7c23b892 | 43 | |
3b274301 LB |
44 | static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
45 | ||
b4053c64 | 46 | /* #define E1000_DEBUG */ |
7c23b892 | 47 | |
27124888 | 48 | #ifdef E1000_DEBUG |
7c23b892 | 49 | enum { |
20f3e863 LB |
50 | DEBUG_GENERAL, DEBUG_IO, DEBUG_MMIO, DEBUG_INTERRUPT, |
51 | DEBUG_RX, DEBUG_TX, DEBUG_MDIC, DEBUG_EEPROM, | |
52 | DEBUG_UNKNOWN, DEBUG_TXSUM, DEBUG_TXERR, DEBUG_RXERR, | |
f9c1cdf4 | 53 | DEBUG_RXFILTER, DEBUG_PHY, DEBUG_NOTYET, |
7c23b892 | 54 | }; |
20f3e863 | 55 | #define DBGBIT(x) (1<<DEBUG_##x) |
7c23b892 AZ |
56 | static int debugflags = DBGBIT(TXERR) | DBGBIT(GENERAL); |
57 | ||
20f3e863 | 58 | #define DBGOUT(what, fmt, ...) do { \ |
7c23b892 | 59 | if (debugflags & DBGBIT(what)) \ |
6c7f4b47 | 60 | fprintf(stderr, "e1000: " fmt, ## __VA_ARGS__); \ |
7c23b892 AZ |
61 | } while (0) |
62 | #else | |
20f3e863 | 63 | #define DBGOUT(what, fmt, ...) do {} while (0) |
7c23b892 AZ |
64 | #endif |
65 | ||
66 | #define IOPORT_SIZE 0x40 | |
e94bbefe | 67 | #define PNPMMIO_SIZE 0x20000 |
78aeb23e | 68 | #define MIN_BUF_SIZE 60 /* Min. octets in an ethernet frame sans FCS */ |
7c23b892 | 69 | |
97410dde VM |
70 | #define MAXIMUM_ETHERNET_HDR_LEN (14+4) |
71 | ||
7c23b892 AZ |
72 | /* |
73 | * HW models: | |
8597f2e1 | 74 | * E1000_DEV_ID_82540EM works with Windows, Linux, and OS X <= 10.8 |
7c23b892 | 75 | * E1000_DEV_ID_82544GC_COPPER appears to work; not well tested |
8597f2e1 | 76 | * E1000_DEV_ID_82545EM_COPPER works with Linux and OS X >= 10.6 |
7c23b892 AZ |
77 | * Others never tested |
78 | */ | |
7c23b892 | 79 | |
db1015e9 | 80 | struct E1000State_st { |
b08340d5 AF |
81 | /*< private >*/ |
82 | PCIDevice parent_obj; | |
83 | /*< public >*/ | |
84 | ||
a03e2aec | 85 | NICState *nic; |
fbdaa002 | 86 | NICConf conf; |
ad00a9b9 AK |
87 | MemoryRegion mmio; |
88 | MemoryRegion io; | |
7c23b892 AZ |
89 | |
90 | uint32_t mac_reg[0x8000]; | |
91 | uint16_t phy_reg[0x20]; | |
92 | uint16_t eeprom_data[64]; | |
93 | ||
94 | uint32_t rxbuf_size; | |
95 | uint32_t rxbuf_min_shift; | |
7c23b892 AZ |
96 | struct e1000_tx { |
97 | unsigned char header[256]; | |
8f2e8d1f | 98 | unsigned char vlan_header[4]; |
b10fec9b | 99 | /* Fields vlan and data must not be reordered or separated. */ |
8f2e8d1f | 100 | unsigned char vlan[4]; |
7c23b892 AZ |
101 | unsigned char data[0x10000]; |
102 | uint16_t size; | |
8f2e8d1f | 103 | unsigned char vlan_needed; |
7d08c73e ES |
104 | unsigned char sum_needed; |
105 | bool cptse; | |
093454e2 | 106 | e1000x_txd_props props; |
d62644b4 | 107 | e1000x_txd_props tso_props; |
7c23b892 | 108 | uint16_t tso_frames; |
7c23b892 AZ |
109 | } tx; |
110 | ||
111 | struct { | |
20f3e863 | 112 | uint32_t val_in; /* shifted in from guest driver */ |
7c23b892 AZ |
113 | uint16_t bitnum_in; |
114 | uint16_t bitnum_out; | |
115 | uint16_t reading; | |
116 | uint32_t old_eecd; | |
117 | } eecd_state; | |
b9d03e35 JW |
118 | |
119 | QEMUTimer *autoneg_timer; | |
2af234e6 | 120 | |
e9845f09 VM |
121 | QEMUTimer *mit_timer; /* Mitigation timer. */ |
122 | bool mit_timer_on; /* Mitigation timer is running. */ | |
123 | bool mit_irq_level; /* Tracks interrupt pin level. */ | |
124 | uint32_t mit_ide; /* Tracks E1000_TXD_CMD_IDE bit. */ | |
125 | ||
157628d0 YCL |
126 | QEMUTimer *flush_queue_timer; |
127 | ||
2af234e6 MT |
128 | /* Compatibility flags for migration to/from qemu 1.3.0 and older */ |
129 | #define E1000_FLAG_AUTONEG_BIT 0 | |
e9845f09 | 130 | #define E1000_FLAG_MIT_BIT 1 |
9e117734 | 131 | #define E1000_FLAG_MAC_BIT 2 |
46f2a9ec | 132 | #define E1000_FLAG_TSO_BIT 3 |
2af234e6 | 133 | #define E1000_FLAG_AUTONEG (1 << E1000_FLAG_AUTONEG_BIT) |
e9845f09 | 134 | #define E1000_FLAG_MIT (1 << E1000_FLAG_MIT_BIT) |
9e117734 | 135 | #define E1000_FLAG_MAC (1 << E1000_FLAG_MAC_BIT) |
46f2a9ec | 136 | #define E1000_FLAG_TSO (1 << E1000_FLAG_TSO_BIT) |
2af234e6 | 137 | uint32_t compat_flags; |
3c4053c5 | 138 | bool received_tx_tso; |
ff214d42 | 139 | bool use_tso_for_migration; |
59354484 | 140 | e1000x_txd_props mig_props; |
db1015e9 EH |
141 | }; |
142 | typedef struct E1000State_st E1000State; | |
7c23b892 | 143 | |
bc0f0674 LB |
144 | #define chkflag(x) (s->compat_flags & E1000_FLAG_##x) |
145 | ||
db1015e9 | 146 | struct E1000BaseClass { |
8597f2e1 GS |
147 | PCIDeviceClass parent_class; |
148 | uint16_t phy_id2; | |
db1015e9 EH |
149 | }; |
150 | typedef struct E1000BaseClass E1000BaseClass; | |
8597f2e1 GS |
151 | |
152 | #define TYPE_E1000_BASE "e1000-base" | |
567a3c9e | 153 | |
8110fa1d EH |
154 | DECLARE_OBJ_CHECKERS(E1000State, E1000BaseClass, |
155 | E1000, TYPE_E1000_BASE) | |
8597f2e1 | 156 | |
567a3c9e | 157 | |
71aadd3c | 158 | static void |
093454e2 | 159 | e1000_link_up(E1000State *s) |
71aadd3c | 160 | { |
093454e2 DF |
161 | e1000x_update_regs_on_link_up(s->mac_reg, s->phy_reg); |
162 | ||
163 | /* E1000_STATUS_LU is tested by e1000_can_receive() */ | |
164 | qemu_flush_queued_packets(qemu_get_queue(s->nic)); | |
71aadd3c JW |
165 | } |
166 | ||
167 | static void | |
093454e2 | 168 | e1000_autoneg_done(E1000State *s) |
71aadd3c | 169 | { |
093454e2 | 170 | e1000x_update_regs_on_autoneg_done(s->mac_reg, s->phy_reg); |
5df6a185 SH |
171 | |
172 | /* E1000_STATUS_LU is tested by e1000_can_receive() */ | |
173 | qemu_flush_queued_packets(qemu_get_queue(s->nic)); | |
71aadd3c JW |
174 | } |
175 | ||
1195fed9 GS |
176 | static bool |
177 | have_autoneg(E1000State *s) | |
178 | { | |
bc0f0674 | 179 | return chkflag(AUTONEG) && (s->phy_reg[PHY_CTRL] & MII_CR_AUTO_NEG_EN); |
1195fed9 GS |
180 | } |
181 | ||
b9d03e35 JW |
182 | static void |
183 | set_phy_ctrl(E1000State *s, int index, uint16_t val) | |
184 | { | |
1195fed9 GS |
185 | /* bits 0-5 reserved; MII_CR_[RESTART_AUTO_NEG,RESET] are self clearing */ |
186 | s->phy_reg[PHY_CTRL] = val & ~(0x3f | | |
187 | MII_CR_RESET | | |
188 | MII_CR_RESTART_AUTO_NEG); | |
189 | ||
2af234e6 MT |
190 | /* |
191 | * QEMU 1.3 does not support link auto-negotiation emulation, so if we | |
192 | * migrate during auto negotiation, after migration the link will be | |
193 | * down. | |
194 | */ | |
1195fed9 | 195 | if (have_autoneg(s) && (val & MII_CR_RESTART_AUTO_NEG)) { |
093454e2 | 196 | e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer); |
b9d03e35 JW |
197 | } |
198 | } | |
199 | ||
b9d03e35 JW |
200 | static void (*phyreg_writeops[])(E1000State *, int, uint16_t) = { |
201 | [PHY_CTRL] = set_phy_ctrl, | |
202 | }; | |
203 | ||
204 | enum { NPHYWRITEOPS = ARRAY_SIZE(phyreg_writeops) }; | |
205 | ||
7c23b892 | 206 | enum { PHY_R = 1, PHY_W = 2, PHY_RW = PHY_R | PHY_W }; |
88b4e9db | 207 | static const char phy_regcap[0x20] = { |
20f3e863 LB |
208 | [PHY_STATUS] = PHY_R, [M88E1000_EXT_PHY_SPEC_CTRL] = PHY_RW, |
209 | [PHY_ID1] = PHY_R, [M88E1000_PHY_SPEC_CTRL] = PHY_RW, | |
210 | [PHY_CTRL] = PHY_RW, [PHY_1000T_CTRL] = PHY_RW, | |
211 | [PHY_LP_ABILITY] = PHY_R, [PHY_1000T_STATUS] = PHY_R, | |
212 | [PHY_AUTONEG_ADV] = PHY_RW, [M88E1000_RX_ERR_CNTR] = PHY_R, | |
213 | [PHY_ID2] = PHY_R, [M88E1000_PHY_SPEC_STATUS] = PHY_R, | |
6883b591 | 214 | [PHY_AUTONEG_EXP] = PHY_R, |
7c23b892 AZ |
215 | }; |
216 | ||
8597f2e1 | 217 | /* PHY_ID2 documented in 8254x_GBe_SDM.pdf, pp. 250 */ |
814cd3ac | 218 | static const uint16_t phy_reg_init[] = { |
20f3e863 | 219 | [PHY_CTRL] = MII_CR_SPEED_SELECT_MSB | |
9616c290 GS |
220 | MII_CR_FULL_DUPLEX | |
221 | MII_CR_AUTO_NEG_EN, | |
222 | ||
223 | [PHY_STATUS] = MII_SR_EXTENDED_CAPS | | |
224 | MII_SR_LINK_STATUS | /* link initially up */ | |
225 | MII_SR_AUTONEG_CAPS | | |
226 | /* MII_SR_AUTONEG_COMPLETE: initially NOT completed */ | |
227 | MII_SR_PREAMBLE_SUPPRESS | | |
228 | MII_SR_EXTENDED_STATUS | | |
229 | MII_SR_10T_HD_CAPS | | |
230 | MII_SR_10T_FD_CAPS | | |
231 | MII_SR_100X_HD_CAPS | | |
232 | MII_SR_100X_FD_CAPS, | |
233 | ||
234 | [PHY_ID1] = 0x141, | |
235 | /* [PHY_ID2] configured per DevId, from e1000_reset() */ | |
236 | [PHY_AUTONEG_ADV] = 0xde1, | |
237 | [PHY_LP_ABILITY] = 0x1e0, | |
238 | [PHY_1000T_CTRL] = 0x0e00, | |
239 | [PHY_1000T_STATUS] = 0x3c00, | |
240 | [M88E1000_PHY_SPEC_CTRL] = 0x360, | |
814cd3ac | 241 | [M88E1000_PHY_SPEC_STATUS] = 0xac00, |
9616c290 | 242 | [M88E1000_EXT_PHY_SPEC_CTRL] = 0x0d60, |
814cd3ac MT |
243 | }; |
244 | ||
245 | static const uint32_t mac_reg_init[] = { | |
20f3e863 LB |
246 | [PBA] = 0x00100030, |
247 | [LEDCTL] = 0x602, | |
248 | [CTRL] = E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN0 | | |
814cd3ac | 249 | E1000_CTRL_SPD_1000 | E1000_CTRL_SLU, |
20f3e863 | 250 | [STATUS] = 0x80000000 | E1000_STATUS_GIO_MASTER_ENABLE | |
814cd3ac MT |
251 | E1000_STATUS_ASDV | E1000_STATUS_MTXCKOK | |
252 | E1000_STATUS_SPEED_1000 | E1000_STATUS_FD | | |
253 | E1000_STATUS_LU, | |
20f3e863 | 254 | [MANC] = E1000_MANC_EN_MNG2HOST | E1000_MANC_RCV_TCO_EN | |
814cd3ac MT |
255 | E1000_MANC_ARP_EN | E1000_MANC_0298_EN | |
256 | E1000_MANC_RMCP_EN, | |
257 | }; | |
258 | ||
e9845f09 VM |
259 | /* Helper function, *curr == 0 means the value is not set */ |
260 | static inline void | |
261 | mit_update_delay(uint32_t *curr, uint32_t value) | |
262 | { | |
263 | if (value && (*curr == 0 || value < *curr)) { | |
264 | *curr = value; | |
265 | } | |
266 | } | |
267 | ||
7c23b892 AZ |
268 | static void |
269 | set_interrupt_cause(E1000State *s, int index, uint32_t val) | |
270 | { | |
b08340d5 | 271 | PCIDevice *d = PCI_DEVICE(s); |
e9845f09 VM |
272 | uint32_t pending_ints; |
273 | uint32_t mit_delay; | |
b08340d5 | 274 | |
7c23b892 | 275 | s->mac_reg[ICR] = val; |
a52a8841 MT |
276 | |
277 | /* | |
278 | * Make sure ICR and ICS registers have the same value. | |
279 | * The spec says that the ICS register is write-only. However in practice, | |
280 | * on real hardware ICS is readable, and for reads it has the same value as | |
281 | * ICR (except that ICS does not have the clear on read behaviour of ICR). | |
282 | * | |
283 | * The VxWorks PRO/1000 driver uses this behaviour. | |
284 | */ | |
b1332393 | 285 | s->mac_reg[ICS] = val; |
a52a8841 | 286 | |
e9845f09 VM |
287 | pending_ints = (s->mac_reg[IMS] & s->mac_reg[ICR]); |
288 | if (!s->mit_irq_level && pending_ints) { | |
289 | /* | |
290 | * Here we detect a potential raising edge. We postpone raising the | |
291 | * interrupt line if we are inside the mitigation delay window | |
292 | * (s->mit_timer_on == 1). | |
293 | * We provide a partial implementation of interrupt mitigation, | |
294 | * emulating only RADV, TADV and ITR (lower 16 bits, 1024ns units for | |
295 | * RADV and TADV, 256ns units for ITR). RDTR is only used to enable | |
296 | * RADV; relative timers based on TIDV and RDTR are not implemented. | |
297 | */ | |
298 | if (s->mit_timer_on) { | |
299 | return; | |
300 | } | |
bc0f0674 | 301 | if (chkflag(MIT)) { |
e9845f09 VM |
302 | /* Compute the next mitigation delay according to pending |
303 | * interrupts and the current values of RADV (provided | |
304 | * RDTR!=0), TADV and ITR. | |
305 | * Then rearm the timer. | |
306 | */ | |
307 | mit_delay = 0; | |
308 | if (s->mit_ide && | |
309 | (pending_ints & (E1000_ICR_TXQE | E1000_ICR_TXDW))) { | |
310 | mit_update_delay(&mit_delay, s->mac_reg[TADV] * 4); | |
311 | } | |
312 | if (s->mac_reg[RDTR] && (pending_ints & E1000_ICS_RXT0)) { | |
313 | mit_update_delay(&mit_delay, s->mac_reg[RADV] * 4); | |
314 | } | |
315 | mit_update_delay(&mit_delay, s->mac_reg[ITR]); | |
316 | ||
74004e8c SJ |
317 | /* |
318 | * According to e1000 SPEC, the Ethernet controller guarantees | |
319 | * a maximum observable interrupt rate of 7813 interrupts/sec. | |
320 | * Thus if mit_delay < 500 then the delay should be set to the | |
321 | * minimum delay possible which is 500. | |
322 | */ | |
323 | mit_delay = (mit_delay < 500) ? 500 : mit_delay; | |
324 | ||
b92233b3 SJ |
325 | s->mit_timer_on = 1; |
326 | timer_mod(s->mit_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + | |
327 | mit_delay * 256); | |
e9845f09 VM |
328 | s->mit_ide = 0; |
329 | } | |
330 | } | |
331 | ||
332 | s->mit_irq_level = (pending_ints != 0); | |
9e64f8a3 | 333 | pci_set_irq(d, s->mit_irq_level); |
e9845f09 VM |
334 | } |
335 | ||
336 | static void | |
337 | e1000_mit_timer(void *opaque) | |
338 | { | |
339 | E1000State *s = opaque; | |
340 | ||
341 | s->mit_timer_on = 0; | |
342 | /* Call set_interrupt_cause to update the irq level (if necessary). */ | |
343 | set_interrupt_cause(s, 0, s->mac_reg[ICR]); | |
7c23b892 AZ |
344 | } |
345 | ||
346 | static void | |
347 | set_ics(E1000State *s, int index, uint32_t val) | |
348 | { | |
349 | DBGOUT(INTERRUPT, "set_ics %x, ICR %x, IMR %x\n", val, s->mac_reg[ICR], | |
350 | s->mac_reg[IMS]); | |
351 | set_interrupt_cause(s, 0, val | s->mac_reg[ICR]); | |
352 | } | |
353 | ||
d52aec95 GS |
354 | static void |
355 | e1000_autoneg_timer(void *opaque) | |
356 | { | |
357 | E1000State *s = opaque; | |
358 | if (!qemu_get_queue(s->nic)->link_down) { | |
093454e2 | 359 | e1000_autoneg_done(s); |
d52aec95 GS |
360 | set_ics(s, 0, E1000_ICS_LSC); /* signal link status change to guest */ |
361 | } | |
362 | } | |
363 | ||
814cd3ac MT |
364 | static void e1000_reset(void *opaque) |
365 | { | |
366 | E1000State *d = opaque; | |
c51325d8 | 367 | E1000BaseClass *edc = E1000_GET_CLASS(d); |
372254c6 | 368 | uint8_t *macaddr = d->conf.macaddr.a; |
814cd3ac | 369 | |
bc72ad67 | 370 | timer_del(d->autoneg_timer); |
e9845f09 | 371 | timer_del(d->mit_timer); |
157628d0 | 372 | timer_del(d->flush_queue_timer); |
e9845f09 VM |
373 | d->mit_timer_on = 0; |
374 | d->mit_irq_level = 0; | |
375 | d->mit_ide = 0; | |
814cd3ac MT |
376 | memset(d->phy_reg, 0, sizeof d->phy_reg); |
377 | memmove(d->phy_reg, phy_reg_init, sizeof phy_reg_init); | |
8597f2e1 | 378 | d->phy_reg[PHY_ID2] = edc->phy_id2; |
814cd3ac MT |
379 | memset(d->mac_reg, 0, sizeof d->mac_reg); |
380 | memmove(d->mac_reg, mac_reg_init, sizeof mac_reg_init); | |
381 | d->rxbuf_min_shift = 1; | |
382 | memset(&d->tx, 0, sizeof d->tx); | |
383 | ||
b356f76d | 384 | if (qemu_get_queue(d->nic)->link_down) { |
093454e2 | 385 | e1000x_update_regs_on_link_down(d->mac_reg, d->phy_reg); |
814cd3ac | 386 | } |
372254c6 | 387 | |
093454e2 | 388 | e1000x_reset_mac_addr(d->nic, d->mac_reg, macaddr); |
814cd3ac MT |
389 | } |
390 | ||
cab3c825 KW |
391 | static void |
392 | set_ctrl(E1000State *s, int index, uint32_t val) | |
393 | { | |
394 | /* RST is self clearing */ | |
395 | s->mac_reg[CTRL] = val & ~E1000_CTRL_RST; | |
396 | } | |
397 | ||
157628d0 YCL |
398 | static void |
399 | e1000_flush_queue_timer(void *opaque) | |
400 | { | |
401 | E1000State *s = opaque; | |
402 | ||
403 | qemu_flush_queued_packets(qemu_get_queue(s->nic)); | |
404 | } | |
405 | ||
7c23b892 AZ |
406 | static void |
407 | set_rx_control(E1000State *s, int index, uint32_t val) | |
408 | { | |
409 | s->mac_reg[RCTL] = val; | |
093454e2 | 410 | s->rxbuf_size = e1000x_rxbufsize(val); |
7c23b892 AZ |
411 | s->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1; |
412 | DBGOUT(RX, "RCTL: %d, mac_reg[RCTL] = 0x%x\n", s->mac_reg[RDT], | |
413 | s->mac_reg[RCTL]); | |
157628d0 YCL |
414 | timer_mod(s->flush_queue_timer, |
415 | qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 1000); | |
7c23b892 AZ |
416 | } |
417 | ||
418 | static void | |
419 | set_mdic(E1000State *s, int index, uint32_t val) | |
420 | { | |
421 | uint32_t data = val & E1000_MDIC_DATA_MASK; | |
422 | uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT); | |
423 | ||
424 | if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) // phy # | |
425 | val = s->mac_reg[MDIC] | E1000_MDIC_ERROR; | |
426 | else if (val & E1000_MDIC_OP_READ) { | |
427 | DBGOUT(MDIC, "MDIC read reg 0x%x\n", addr); | |
428 | if (!(phy_regcap[addr] & PHY_R)) { | |
429 | DBGOUT(MDIC, "MDIC read reg %x unhandled\n", addr); | |
430 | val |= E1000_MDIC_ERROR; | |
431 | } else | |
432 | val = (val ^ data) | s->phy_reg[addr]; | |
433 | } else if (val & E1000_MDIC_OP_WRITE) { | |
434 | DBGOUT(MDIC, "MDIC write reg 0x%x, value 0x%x\n", addr, data); | |
435 | if (!(phy_regcap[addr] & PHY_W)) { | |
436 | DBGOUT(MDIC, "MDIC write reg %x unhandled\n", addr); | |
437 | val |= E1000_MDIC_ERROR; | |
b9d03e35 JW |
438 | } else { |
439 | if (addr < NPHYWRITEOPS && phyreg_writeops[addr]) { | |
440 | phyreg_writeops[addr](s, index, data); | |
1195fed9 GS |
441 | } else { |
442 | s->phy_reg[addr] = data; | |
b9d03e35 | 443 | } |
b9d03e35 | 444 | } |
7c23b892 AZ |
445 | } |
446 | s->mac_reg[MDIC] = val | E1000_MDIC_READY; | |
17fbbb0b JW |
447 | |
448 | if (val & E1000_MDIC_INT_EN) { | |
449 | set_ics(s, 0, E1000_ICR_MDAC); | |
450 | } | |
7c23b892 AZ |
451 | } |
452 | ||
453 | static uint32_t | |
454 | get_eecd(E1000State *s, int index) | |
455 | { | |
456 | uint32_t ret = E1000_EECD_PRES|E1000_EECD_GNT | s->eecd_state.old_eecd; | |
457 | ||
458 | DBGOUT(EEPROM, "reading eeprom bit %d (reading %d)\n", | |
459 | s->eecd_state.bitnum_out, s->eecd_state.reading); | |
460 | if (!s->eecd_state.reading || | |
461 | ((s->eeprom_data[(s->eecd_state.bitnum_out >> 4) & 0x3f] >> | |
462 | ((s->eecd_state.bitnum_out & 0xf) ^ 0xf))) & 1) | |
463 | ret |= E1000_EECD_DO; | |
464 | return ret; | |
465 | } | |
466 | ||
467 | static void | |
468 | set_eecd(E1000State *s, int index, uint32_t val) | |
469 | { | |
470 | uint32_t oldval = s->eecd_state.old_eecd; | |
471 | ||
472 | s->eecd_state.old_eecd = val & (E1000_EECD_SK | E1000_EECD_CS | | |
473 | E1000_EECD_DI|E1000_EECD_FWE_MASK|E1000_EECD_REQ); | |
20f3e863 LB |
474 | if (!(E1000_EECD_CS & val)) { /* CS inactive; nothing to do */ |
475 | return; | |
476 | } | |
477 | if (E1000_EECD_CS & (val ^ oldval)) { /* CS rise edge; reset state */ | |
478 | s->eecd_state.val_in = 0; | |
479 | s->eecd_state.bitnum_in = 0; | |
480 | s->eecd_state.bitnum_out = 0; | |
481 | s->eecd_state.reading = 0; | |
9651ac55 | 482 | } |
20f3e863 | 483 | if (!(E1000_EECD_SK & (val ^ oldval))) { /* no clock edge */ |
7c23b892 | 484 | return; |
20f3e863 LB |
485 | } |
486 | if (!(E1000_EECD_SK & val)) { /* falling edge */ | |
7c23b892 AZ |
487 | s->eecd_state.bitnum_out++; |
488 | return; | |
489 | } | |
7c23b892 AZ |
490 | s->eecd_state.val_in <<= 1; |
491 | if (val & E1000_EECD_DI) | |
492 | s->eecd_state.val_in |= 1; | |
493 | if (++s->eecd_state.bitnum_in == 9 && !s->eecd_state.reading) { | |
494 | s->eecd_state.bitnum_out = ((s->eecd_state.val_in & 0x3f)<<4)-1; | |
495 | s->eecd_state.reading = (((s->eecd_state.val_in >> 6) & 7) == | |
496 | EEPROM_READ_OPCODE_MICROWIRE); | |
497 | } | |
498 | DBGOUT(EEPROM, "eeprom bitnum in %d out %d, reading %d\n", | |
499 | s->eecd_state.bitnum_in, s->eecd_state.bitnum_out, | |
500 | s->eecd_state.reading); | |
501 | } | |
502 | ||
503 | static uint32_t | |
504 | flash_eerd_read(E1000State *s, int x) | |
505 | { | |
506 | unsigned int index, r = s->mac_reg[EERD] & ~E1000_EEPROM_RW_REG_START; | |
507 | ||
b1332393 BP |
508 | if ((s->mac_reg[EERD] & E1000_EEPROM_RW_REG_START) == 0) |
509 | return (s->mac_reg[EERD]); | |
510 | ||
7c23b892 | 511 | if ((index = r >> E1000_EEPROM_RW_ADDR_SHIFT) > EEPROM_CHECKSUM_REG) |
b1332393 BP |
512 | return (E1000_EEPROM_RW_REG_DONE | r); |
513 | ||
514 | return ((s->eeprom_data[index] << E1000_EEPROM_RW_REG_DATA) | | |
515 | E1000_EEPROM_RW_REG_DONE | r); | |
7c23b892 AZ |
516 | } |
517 | ||
7c23b892 AZ |
518 | static void |
519 | putsum(uint8_t *data, uint32_t n, uint32_t sloc, uint32_t css, uint32_t cse) | |
520 | { | |
c6a6a5e3 AL |
521 | uint32_t sum; |
522 | ||
7c23b892 AZ |
523 | if (cse && cse < n) |
524 | n = cse + 1; | |
c6a6a5e3 AL |
525 | if (sloc < n-1) { |
526 | sum = net_checksum_add(n-css, data+css); | |
0dacea92 | 527 | stw_be_p(data + sloc, net_checksum_finish_nozero(sum)); |
c6a6a5e3 | 528 | } |
7c23b892 AZ |
529 | } |
530 | ||
3b274301 LB |
531 | static inline void |
532 | inc_tx_bcast_or_mcast_count(E1000State *s, const unsigned char *arr) | |
533 | { | |
534 | if (!memcmp(arr, bcast, sizeof bcast)) { | |
093454e2 | 535 | e1000x_inc_reg_if_not_full(s->mac_reg, BPTC); |
3b274301 | 536 | } else if (arr[0] & 1) { |
093454e2 | 537 | e1000x_inc_reg_if_not_full(s->mac_reg, MPTC); |
3b274301 LB |
538 | } |
539 | } | |
540 | ||
93e37d76 JW |
541 | static void |
542 | e1000_send_packet(E1000State *s, const uint8_t *buf, int size) | |
543 | { | |
3b274301 LB |
544 | static const int PTCregs[6] = { PTC64, PTC127, PTC255, PTC511, |
545 | PTC1023, PTC1522 }; | |
546 | ||
b356f76d | 547 | NetClientState *nc = qemu_get_queue(s->nic); |
93e37d76 | 548 | if (s->phy_reg[PHY_CTRL] & MII_CR_LOOPBACK) { |
1caff034 | 549 | qemu_receive_packet(nc, buf, size); |
93e37d76 | 550 | } else { |
b356f76d | 551 | qemu_send_packet(nc, buf, size); |
93e37d76 | 552 | } |
3b274301 | 553 | inc_tx_bcast_or_mcast_count(s, buf); |
093454e2 | 554 | e1000x_increase_size_stats(s->mac_reg, PTCregs, size); |
93e37d76 JW |
555 | } |
556 | ||
7c23b892 AZ |
557 | static void |
558 | xmit_seg(E1000State *s) | |
559 | { | |
14e60aae | 560 | uint16_t len; |
45e93764 | 561 | unsigned int frames = s->tx.tso_frames, css, sofar; |
7c23b892 | 562 | struct e1000_tx *tp = &s->tx; |
d62644b4 | 563 | struct e1000x_txd_props *props = tp->cptse ? &tp->tso_props : &tp->props; |
7c23b892 | 564 | |
d62644b4 ES |
565 | if (tp->cptse) { |
566 | css = props->ipcss; | |
7c23b892 AZ |
567 | DBGOUT(TXSUM, "frames %d size %d ipcss %d\n", |
568 | frames, tp->size, css); | |
d62644b4 | 569 | if (props->ip) { /* IPv4 */ |
d8ee2591 PM |
570 | stw_be_p(tp->data+css+2, tp->size - css); |
571 | stw_be_p(tp->data+css+4, | |
14e60aae | 572 | lduw_be_p(tp->data + css + 4) + frames); |
20f3e863 | 573 | } else { /* IPv6 */ |
d8ee2591 | 574 | stw_be_p(tp->data+css+4, tp->size - css); |
20f3e863 | 575 | } |
d62644b4 | 576 | css = props->tucss; |
7c23b892 | 577 | len = tp->size - css; |
d62644b4 ES |
578 | DBGOUT(TXSUM, "tcp %d tucss %d len %d\n", props->tcp, css, len); |
579 | if (props->tcp) { | |
580 | sofar = frames * props->mss; | |
6bd194ab | 581 | stl_be_p(tp->data+css+4, ldl_be_p(tp->data+css+4)+sofar); /* seq */ |
d62644b4 | 582 | if (props->paylen - sofar > props->mss) { |
20f3e863 | 583 | tp->data[css + 13] &= ~9; /* PSH, FIN */ |
3b274301 | 584 | } else if (frames) { |
093454e2 | 585 | e1000x_inc_reg_if_not_full(s->mac_reg, TSCTC); |
3b274301 | 586 | } |
d62644b4 | 587 | } else { /* UDP */ |
d8ee2591 | 588 | stw_be_p(tp->data+css+4, len); |
d62644b4 | 589 | } |
7d08c73e | 590 | if (tp->sum_needed & E1000_TXD_POPTS_TXSM) { |
e685b4eb | 591 | unsigned int phsum; |
7c23b892 | 592 | // add pseudo-header length before checksum calculation |
d62644b4 | 593 | void *sp = tp->data + props->tucso; |
14e60aae PM |
594 | |
595 | phsum = lduw_be_p(sp) + len; | |
e685b4eb | 596 | phsum = (phsum >> 16) + (phsum & 0xffff); |
d8ee2591 | 597 | stw_be_p(sp, phsum); |
7c23b892 AZ |
598 | } |
599 | tp->tso_frames++; | |
600 | } | |
601 | ||
7d08c73e | 602 | if (tp->sum_needed & E1000_TXD_POPTS_TXSM) { |
d62644b4 | 603 | putsum(tp->data, tp->size, props->tucso, props->tucss, props->tucse); |
093454e2 | 604 | } |
7d08c73e | 605 | if (tp->sum_needed & E1000_TXD_POPTS_IXSM) { |
d62644b4 | 606 | putsum(tp->data, tp->size, props->ipcso, props->ipcss, props->ipcse); |
093454e2 | 607 | } |
8f2e8d1f | 608 | if (tp->vlan_needed) { |
b10fec9b SW |
609 | memmove(tp->vlan, tp->data, 4); |
610 | memmove(tp->data, tp->data + 4, 8); | |
8f2e8d1f | 611 | memcpy(tp->data + 8, tp->vlan_header, 4); |
93e37d76 | 612 | e1000_send_packet(s, tp->vlan, tp->size + 4); |
20f3e863 | 613 | } else { |
93e37d76 | 614 | e1000_send_packet(s, tp->data, tp->size); |
20f3e863 LB |
615 | } |
616 | ||
093454e2 DF |
617 | e1000x_inc_reg_if_not_full(s->mac_reg, TPT); |
618 | e1000x_grow_8reg_if_not_full(s->mac_reg, TOTL, s->tx.size); | |
1f67f92c | 619 | s->mac_reg[GPTC] = s->mac_reg[TPT]; |
3b274301 LB |
620 | s->mac_reg[GOTCL] = s->mac_reg[TOTL]; |
621 | s->mac_reg[GOTCH] = s->mac_reg[TOTH]; | |
7c23b892 AZ |
622 | } |
623 | ||
624 | static void | |
625 | process_tx_desc(E1000State *s, struct e1000_tx_desc *dp) | |
626 | { | |
b08340d5 | 627 | PCIDevice *d = PCI_DEVICE(s); |
7c23b892 AZ |
628 | uint32_t txd_lower = le32_to_cpu(dp->lower.data); |
629 | uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D); | |
093454e2 | 630 | unsigned int split_size = txd_lower & 0xffff, bytes, sz; |
a0ae17a6 | 631 | unsigned int msh = 0xfffff; |
7c23b892 AZ |
632 | uint64_t addr; |
633 | struct e1000_context_desc *xp = (struct e1000_context_desc *)dp; | |
634 | struct e1000_tx *tp = &s->tx; | |
635 | ||
e9845f09 | 636 | s->mit_ide |= (txd_lower & E1000_TXD_CMD_IDE); |
20f3e863 | 637 | if (dtype == E1000_TXD_CMD_DEXT) { /* context descriptor */ |
d62644b4 ES |
638 | if (le32_to_cpu(xp->cmd_and_length) & E1000_TXD_CMD_TSE) { |
639 | e1000x_read_tx_ctx_descr(xp, &tp->tso_props); | |
ff214d42 | 640 | s->use_tso_for_migration = 1; |
d62644b4 ES |
641 | tp->tso_frames = 0; |
642 | } else { | |
643 | e1000x_read_tx_ctx_descr(xp, &tp->props); | |
ff214d42 | 644 | s->use_tso_for_migration = 0; |
7c23b892 AZ |
645 | } |
646 | return; | |
1b0009db AZ |
647 | } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) { |
648 | // data descriptor | |
735e77ec | 649 | if (tp->size == 0) { |
7d08c73e | 650 | tp->sum_needed = le32_to_cpu(dp->upper.data) >> 8; |
735e77ec | 651 | } |
7d08c73e | 652 | tp->cptse = (txd_lower & E1000_TXD_CMD_TSE) ? 1 : 0; |
43ad7e3e | 653 | } else { |
1b0009db | 654 | // legacy descriptor |
7d08c73e | 655 | tp->cptse = 0; |
43ad7e3e | 656 | } |
7c23b892 | 657 | |
093454e2 DF |
658 | if (e1000x_vlan_enabled(s->mac_reg) && |
659 | e1000x_is_vlan_txd(txd_lower) && | |
7d08c73e | 660 | (tp->cptse || txd_lower & E1000_TXD_CMD_EOP)) { |
8f2e8d1f | 661 | tp->vlan_needed = 1; |
d8ee2591 | 662 | stw_be_p(tp->vlan_header, |
4e60a250 | 663 | le16_to_cpu(s->mac_reg[VET])); |
d8ee2591 | 664 | stw_be_p(tp->vlan_header + 2, |
8f2e8d1f AL |
665 | le16_to_cpu(dp->upper.fields.special)); |
666 | } | |
20f3e863 | 667 | |
7c23b892 | 668 | addr = le64_to_cpu(dp->buffer_addr); |
d62644b4 ES |
669 | if (tp->cptse) { |
670 | msh = tp->tso_props.hdr_len + tp->tso_props.mss; | |
1b0009db AZ |
671 | do { |
672 | bytes = split_size; | |
3de46e6f JW |
673 | if (tp->size >= msh) { |
674 | goto eop; | |
675 | } | |
1b0009db AZ |
676 | if (tp->size + bytes > msh) |
677 | bytes = msh - tp->size; | |
65f82df0 AL |
678 | |
679 | bytes = MIN(sizeof(tp->data) - tp->size, bytes); | |
b08340d5 | 680 | pci_dma_read(d, addr, tp->data + tp->size, bytes); |
a0ae17a6 | 681 | sz = tp->size + bytes; |
d62644b4 ES |
682 | if (sz >= tp->tso_props.hdr_len |
683 | && tp->size < tp->tso_props.hdr_len) { | |
684 | memmove(tp->header, tp->data, tp->tso_props.hdr_len); | |
a0ae17a6 | 685 | } |
1b0009db AZ |
686 | tp->size = sz; |
687 | addr += bytes; | |
688 | if (sz == msh) { | |
689 | xmit_seg(s); | |
d62644b4 ES |
690 | memmove(tp->data, tp->header, tp->tso_props.hdr_len); |
691 | tp->size = tp->tso_props.hdr_len; | |
1b0009db | 692 | } |
b947ac2b PP |
693 | split_size -= bytes; |
694 | } while (bytes && split_size); | |
1b0009db | 695 | } else { |
65f82df0 | 696 | split_size = MIN(sizeof(tp->data) - tp->size, split_size); |
b08340d5 | 697 | pci_dma_read(d, addr, tp->data + tp->size, split_size); |
1b0009db | 698 | tp->size += split_size; |
7c23b892 | 699 | } |
7c23b892 | 700 | |
3de46e6f | 701 | eop: |
7c23b892 AZ |
702 | if (!(txd_lower & E1000_TXD_CMD_EOP)) |
703 | return; | |
d62644b4 | 704 | if (!(tp->cptse && tp->size < tp->tso_props.hdr_len)) { |
7c23b892 | 705 | xmit_seg(s); |
a0ae17a6 | 706 | } |
7c23b892 | 707 | tp->tso_frames = 0; |
7d08c73e | 708 | tp->sum_needed = 0; |
8f2e8d1f | 709 | tp->vlan_needed = 0; |
7c23b892 | 710 | tp->size = 0; |
7d08c73e | 711 | tp->cptse = 0; |
7c23b892 AZ |
712 | } |
713 | ||
714 | static uint32_t | |
62ecbd35 | 715 | txdesc_writeback(E1000State *s, dma_addr_t base, struct e1000_tx_desc *dp) |
7c23b892 | 716 | { |
b08340d5 | 717 | PCIDevice *d = PCI_DEVICE(s); |
7c23b892 AZ |
718 | uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data); |
719 | ||
720 | if (!(txd_lower & (E1000_TXD_CMD_RS|E1000_TXD_CMD_RPS))) | |
721 | return 0; | |
722 | txd_upper = (le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD) & | |
723 | ~(E1000_TXD_STAT_EC | E1000_TXD_STAT_LC | E1000_TXD_STAT_TU); | |
724 | dp->upper.data = cpu_to_le32(txd_upper); | |
b08340d5 | 725 | pci_dma_write(d, base + ((char *)&dp->upper - (char *)dp), |
00c3a05b | 726 | &dp->upper, sizeof(dp->upper)); |
7c23b892 AZ |
727 | return E1000_ICR_TXDW; |
728 | } | |
729 | ||
d17161f6 KW |
730 | static uint64_t tx_desc_base(E1000State *s) |
731 | { | |
732 | uint64_t bah = s->mac_reg[TDBAH]; | |
733 | uint64_t bal = s->mac_reg[TDBAL] & ~0xf; | |
734 | ||
735 | return (bah << 32) + bal; | |
736 | } | |
737 | ||
7c23b892 AZ |
738 | static void |
739 | start_xmit(E1000State *s) | |
740 | { | |
b08340d5 | 741 | PCIDevice *d = PCI_DEVICE(s); |
62ecbd35 | 742 | dma_addr_t base; |
7c23b892 AZ |
743 | struct e1000_tx_desc desc; |
744 | uint32_t tdh_start = s->mac_reg[TDH], cause = E1000_ICS_TXQE; | |
745 | ||
746 | if (!(s->mac_reg[TCTL] & E1000_TCTL_EN)) { | |
747 | DBGOUT(TX, "tx disabled\n"); | |
748 | return; | |
749 | } | |
750 | ||
751 | while (s->mac_reg[TDH] != s->mac_reg[TDT]) { | |
d17161f6 | 752 | base = tx_desc_base(s) + |
7c23b892 | 753 | sizeof(struct e1000_tx_desc) * s->mac_reg[TDH]; |
b08340d5 | 754 | pci_dma_read(d, base, &desc, sizeof(desc)); |
7c23b892 AZ |
755 | |
756 | DBGOUT(TX, "index %d: %p : %x %x\n", s->mac_reg[TDH], | |
6106075b | 757 | (void *)(intptr_t)desc.buffer_addr, desc.lower.data, |
7c23b892 AZ |
758 | desc.upper.data); |
759 | ||
760 | process_tx_desc(s, &desc); | |
62ecbd35 | 761 | cause |= txdesc_writeback(s, base, &desc); |
7c23b892 AZ |
762 | |
763 | if (++s->mac_reg[TDH] * sizeof(desc) >= s->mac_reg[TDLEN]) | |
764 | s->mac_reg[TDH] = 0; | |
765 | /* | |
766 | * the following could happen only if guest sw assigns | |
767 | * bogus values to TDT/TDLEN. | |
768 | * there's nothing too intelligent we could do about this. | |
769 | */ | |
dd793a74 LE |
770 | if (s->mac_reg[TDH] == tdh_start || |
771 | tdh_start >= s->mac_reg[TDLEN] / sizeof(desc)) { | |
7c23b892 AZ |
772 | DBGOUT(TXERR, "TDH wraparound @%x, TDT %x, TDLEN %x\n", |
773 | tdh_start, s->mac_reg[TDT], s->mac_reg[TDLEN]); | |
774 | break; | |
775 | } | |
776 | } | |
777 | set_ics(s, 0, cause); | |
778 | } | |
779 | ||
780 | static int | |
781 | receive_filter(E1000State *s, const uint8_t *buf, int size) | |
782 | { | |
093454e2 | 783 | uint32_t rctl = s->mac_reg[RCTL]; |
4aeea330 | 784 | int isbcast = !memcmp(buf, bcast, sizeof bcast), ismcast = (buf[0] & 1); |
7c23b892 | 785 | |
093454e2 DF |
786 | if (e1000x_is_vlan_packet(buf, le16_to_cpu(s->mac_reg[VET])) && |
787 | e1000x_vlan_rx_filter_enabled(s->mac_reg)) { | |
14e60aae PM |
788 | uint16_t vid = lduw_be_p(buf + 14); |
789 | uint32_t vfta = ldl_le_p((uint32_t*)(s->mac_reg + VFTA) + | |
790 | ((vid >> 5) & 0x7f)); | |
8f2e8d1f AL |
791 | if ((vfta & (1 << (vid & 0x1f))) == 0) |
792 | return 0; | |
793 | } | |
794 | ||
4aeea330 | 795 | if (!isbcast && !ismcast && (rctl & E1000_RCTL_UPE)) { /* promiscuous ucast */ |
7c23b892 | 796 | return 1; |
4aeea330 | 797 | } |
7c23b892 | 798 | |
4aeea330 | 799 | if (ismcast && (rctl & E1000_RCTL_MPE)) { /* promiscuous mcast */ |
093454e2 | 800 | e1000x_inc_reg_if_not_full(s->mac_reg, MPRC); |
7c23b892 | 801 | return 1; |
4aeea330 | 802 | } |
7c23b892 | 803 | |
4aeea330 | 804 | if (isbcast && (rctl & E1000_RCTL_BAM)) { /* broadcast enabled */ |
093454e2 | 805 | e1000x_inc_reg_if_not_full(s->mac_reg, BPRC); |
7c23b892 | 806 | return 1; |
3b274301 | 807 | } |
7c23b892 | 808 | |
093454e2 | 809 | return e1000x_rx_group_filter(s->mac_reg, buf); |
7c23b892 AZ |
810 | } |
811 | ||
99ed7e30 | 812 | static void |
4e68f7a0 | 813 | e1000_set_link_status(NetClientState *nc) |
99ed7e30 | 814 | { |
cc1f0f45 | 815 | E1000State *s = qemu_get_nic_opaque(nc); |
99ed7e30 AL |
816 | uint32_t old_status = s->mac_reg[STATUS]; |
817 | ||
d4044c2a | 818 | if (nc->link_down) { |
093454e2 | 819 | e1000x_update_regs_on_link_down(s->mac_reg, s->phy_reg); |
d4044c2a | 820 | } else { |
d7a41552 | 821 | if (have_autoneg(s) && |
6a2acedb | 822 | !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) { |
093454e2 | 823 | e1000x_restart_autoneg(s->mac_reg, s->phy_reg, s->autoneg_timer); |
6a2acedb GS |
824 | } else { |
825 | e1000_link_up(s); | |
826 | } | |
d4044c2a | 827 | } |
99ed7e30 AL |
828 | |
829 | if (s->mac_reg[STATUS] != old_status) | |
830 | set_ics(s, 0, E1000_ICR_LSC); | |
831 | } | |
832 | ||
322fd48a MT |
833 | static bool e1000_has_rxbufs(E1000State *s, size_t total_size) |
834 | { | |
835 | int bufs; | |
836 | /* Fast-path short packets */ | |
837 | if (total_size <= s->rxbuf_size) { | |
e5b8b0d4 | 838 | return s->mac_reg[RDH] != s->mac_reg[RDT]; |
322fd48a MT |
839 | } |
840 | if (s->mac_reg[RDH] < s->mac_reg[RDT]) { | |
841 | bufs = s->mac_reg[RDT] - s->mac_reg[RDH]; | |
e5b8b0d4 | 842 | } else if (s->mac_reg[RDH] > s->mac_reg[RDT]) { |
322fd48a MT |
843 | bufs = s->mac_reg[RDLEN] / sizeof(struct e1000_rx_desc) + |
844 | s->mac_reg[RDT] - s->mac_reg[RDH]; | |
845 | } else { | |
846 | return false; | |
847 | } | |
848 | return total_size <= bufs * s->rxbuf_size; | |
849 | } | |
850 | ||
b8c4b67e | 851 | static bool |
4e68f7a0 | 852 | e1000_can_receive(NetClientState *nc) |
6cdfab28 | 853 | { |
cc1f0f45 | 854 | E1000State *s = qemu_get_nic_opaque(nc); |
6cdfab28 | 855 | |
093454e2 | 856 | return e1000x_rx_ready(&s->parent_obj, s->mac_reg) && |
157628d0 | 857 | e1000_has_rxbufs(s, 1) && !timer_pending(s->flush_queue_timer); |
6cdfab28 MT |
858 | } |
859 | ||
d17161f6 KW |
860 | static uint64_t rx_desc_base(E1000State *s) |
861 | { | |
862 | uint64_t bah = s->mac_reg[RDBAH]; | |
863 | uint64_t bal = s->mac_reg[RDBAL] & ~0xf; | |
864 | ||
865 | return (bah << 32) + bal; | |
866 | } | |
867 | ||
1001cf45 JW |
868 | static void |
869 | e1000_receiver_overrun(E1000State *s, size_t size) | |
870 | { | |
871 | trace_e1000_receiver_overrun(size, s->mac_reg[RDH], s->mac_reg[RDT]); | |
872 | e1000x_inc_reg_if_not_full(s->mac_reg, RNBC); | |
873 | e1000x_inc_reg_if_not_full(s->mac_reg, MPC); | |
874 | set_ics(s, 0, E1000_ICS_RXO); | |
875 | } | |
876 | ||
4f1c942b | 877 | static ssize_t |
97410dde | 878 | e1000_receive_iov(NetClientState *nc, const struct iovec *iov, int iovcnt) |
7c23b892 | 879 | { |
cc1f0f45 | 880 | E1000State *s = qemu_get_nic_opaque(nc); |
b08340d5 | 881 | PCIDevice *d = PCI_DEVICE(s); |
7c23b892 | 882 | struct e1000_rx_desc desc; |
62ecbd35 | 883 | dma_addr_t base; |
7c23b892 AZ |
884 | unsigned int n, rdt; |
885 | uint32_t rdh_start; | |
8f2e8d1f | 886 | uint16_t vlan_special = 0; |
97410dde | 887 | uint8_t vlan_status = 0; |
78aeb23e | 888 | uint8_t min_buf[MIN_BUF_SIZE]; |
97410dde VM |
889 | struct iovec min_iov; |
890 | uint8_t *filter_buf = iov->iov_base; | |
891 | size_t size = iov_size(iov, iovcnt); | |
892 | size_t iov_ofs = 0; | |
b19487e2 MT |
893 | size_t desc_offset; |
894 | size_t desc_size; | |
895 | size_t total_size; | |
ddcb73b7 | 896 | |
093454e2 | 897 | if (!e1000x_hw_rx_enabled(s->mac_reg)) { |
4f1c942b | 898 | return -1; |
ddcb73b7 | 899 | } |
7c23b892 | 900 | |
157628d0 YCL |
901 | if (timer_pending(s->flush_queue_timer)) { |
902 | return 0; | |
903 | } | |
904 | ||
78aeb23e SH |
905 | /* Pad to minimum Ethernet frame length */ |
906 | if (size < sizeof(min_buf)) { | |
97410dde | 907 | iov_to_buf(iov, iovcnt, 0, min_buf, size); |
78aeb23e | 908 | memset(&min_buf[size], 0, sizeof(min_buf) - size); |
97410dde VM |
909 | min_iov.iov_base = filter_buf = min_buf; |
910 | min_iov.iov_len = size = sizeof(min_buf); | |
911 | iovcnt = 1; | |
912 | iov = &min_iov; | |
913 | } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) { | |
914 | /* This is very unlikely, but may happen. */ | |
915 | iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN); | |
916 | filter_buf = min_buf; | |
78aeb23e SH |
917 | } |
918 | ||
b0d9ffcd | 919 | /* Discard oversized packets if !LPE and !SBP. */ |
093454e2 | 920 | if (e1000x_is_oversized(s->mac_reg, size)) { |
b0d9ffcd MC |
921 | return size; |
922 | } | |
923 | ||
97410dde | 924 | if (!receive_filter(s, filter_buf, size)) { |
4f1c942b | 925 | return size; |
97410dde | 926 | } |
7c23b892 | 927 | |
093454e2 DF |
928 | if (e1000x_vlan_enabled(s->mac_reg) && |
929 | e1000x_is_vlan_packet(filter_buf, le16_to_cpu(s->mac_reg[VET]))) { | |
14e60aae | 930 | vlan_special = cpu_to_le16(lduw_be_p(filter_buf + 14)); |
97410dde VM |
931 | iov_ofs = 4; |
932 | if (filter_buf == iov->iov_base) { | |
933 | memmove(filter_buf + 4, filter_buf, 12); | |
934 | } else { | |
935 | iov_from_buf(iov, iovcnt, 4, filter_buf, 12); | |
936 | while (iov->iov_len <= iov_ofs) { | |
937 | iov_ofs -= iov->iov_len; | |
938 | iov++; | |
939 | } | |
940 | } | |
8f2e8d1f | 941 | vlan_status = E1000_RXD_STAT_VP; |
8f2e8d1f AL |
942 | size -= 4; |
943 | } | |
944 | ||
7c23b892 | 945 | rdh_start = s->mac_reg[RDH]; |
b19487e2 | 946 | desc_offset = 0; |
093454e2 | 947 | total_size = size + e1000x_fcs_len(s->mac_reg); |
322fd48a | 948 | if (!e1000_has_rxbufs(s, total_size)) { |
1001cf45 JW |
949 | e1000_receiver_overrun(s, total_size); |
950 | return -1; | |
322fd48a | 951 | } |
7c23b892 | 952 | do { |
b19487e2 MT |
953 | desc_size = total_size - desc_offset; |
954 | if (desc_size > s->rxbuf_size) { | |
955 | desc_size = s->rxbuf_size; | |
956 | } | |
d17161f6 | 957 | base = rx_desc_base(s) + sizeof(desc) * s->mac_reg[RDH]; |
b08340d5 | 958 | pci_dma_read(d, base, &desc, sizeof(desc)); |
8f2e8d1f AL |
959 | desc.special = vlan_special; |
960 | desc.status |= (vlan_status | E1000_RXD_STAT_DD); | |
7c23b892 | 961 | if (desc.buffer_addr) { |
b19487e2 | 962 | if (desc_offset < size) { |
97410dde VM |
963 | size_t iov_copy; |
964 | hwaddr ba = le64_to_cpu(desc.buffer_addr); | |
b19487e2 MT |
965 | size_t copy_size = size - desc_offset; |
966 | if (copy_size > s->rxbuf_size) { | |
967 | copy_size = s->rxbuf_size; | |
968 | } | |
97410dde VM |
969 | do { |
970 | iov_copy = MIN(copy_size, iov->iov_len - iov_ofs); | |
971 | pci_dma_write(d, ba, iov->iov_base + iov_ofs, iov_copy); | |
972 | copy_size -= iov_copy; | |
973 | ba += iov_copy; | |
974 | iov_ofs += iov_copy; | |
975 | if (iov_ofs == iov->iov_len) { | |
976 | iov++; | |
977 | iov_ofs = 0; | |
978 | } | |
979 | } while (copy_size); | |
b19487e2 MT |
980 | } |
981 | desc_offset += desc_size; | |
ee912ccf | 982 | desc.length = cpu_to_le16(desc_size); |
b19487e2 | 983 | if (desc_offset >= total_size) { |
b19487e2 MT |
984 | desc.status |= E1000_RXD_STAT_EOP | E1000_RXD_STAT_IXSM; |
985 | } else { | |
ee912ccf MT |
986 | /* Guest zeroing out status is not a hardware requirement. |
987 | Clear EOP in case guest didn't do it. */ | |
988 | desc.status &= ~E1000_RXD_STAT_EOP; | |
b19487e2 | 989 | } |
43ad7e3e | 990 | } else { // as per intel docs; skip descriptors with null buf addr |
7c23b892 | 991 | DBGOUT(RX, "Null RX descriptor!!\n"); |
43ad7e3e | 992 | } |
b08340d5 | 993 | pci_dma_write(d, base, &desc, sizeof(desc)); |
7c23b892 AZ |
994 | |
995 | if (++s->mac_reg[RDH] * sizeof(desc) >= s->mac_reg[RDLEN]) | |
996 | s->mac_reg[RDH] = 0; | |
7c23b892 | 997 | /* see comment in start_xmit; same here */ |
dd793a74 LE |
998 | if (s->mac_reg[RDH] == rdh_start || |
999 | rdh_start >= s->mac_reg[RDLEN] / sizeof(desc)) { | |
7c23b892 AZ |
1000 | DBGOUT(RXERR, "RDH wraparound @%x, RDT %x, RDLEN %x\n", |
1001 | rdh_start, s->mac_reg[RDT], s->mac_reg[RDLEN]); | |
1001cf45 | 1002 | e1000_receiver_overrun(s, total_size); |
4f1c942b | 1003 | return -1; |
7c23b892 | 1004 | } |
b19487e2 | 1005 | } while (desc_offset < total_size); |
7c23b892 | 1006 | |
093454e2 | 1007 | e1000x_update_rx_total_stats(s->mac_reg, size, total_size); |
7c23b892 AZ |
1008 | |
1009 | n = E1000_ICS_RXT0; | |
1010 | if ((rdt = s->mac_reg[RDT]) < s->mac_reg[RDH]) | |
1011 | rdt += s->mac_reg[RDLEN] / sizeof(desc); | |
bf16cc8f AL |
1012 | if (((rdt - s->mac_reg[RDH]) * sizeof(desc)) <= s->mac_reg[RDLEN] >> |
1013 | s->rxbuf_min_shift) | |
7c23b892 AZ |
1014 | n |= E1000_ICS_RXDMT0; |
1015 | ||
1016 | set_ics(s, 0, n); | |
4f1c942b MM |
1017 | |
1018 | return size; | |
7c23b892 AZ |
1019 | } |
1020 | ||
97410dde VM |
1021 | static ssize_t |
1022 | e1000_receive(NetClientState *nc, const uint8_t *buf, size_t size) | |
1023 | { | |
1024 | const struct iovec iov = { | |
1025 | .iov_base = (uint8_t *)buf, | |
1026 | .iov_len = size | |
1027 | }; | |
1028 | ||
1029 | return e1000_receive_iov(nc, &iov, 1); | |
1030 | } | |
1031 | ||
7c23b892 AZ |
1032 | static uint32_t |
1033 | mac_readreg(E1000State *s, int index) | |
1034 | { | |
1035 | return s->mac_reg[index]; | |
1036 | } | |
1037 | ||
72ea771c LB |
1038 | static uint32_t |
1039 | mac_low4_read(E1000State *s, int index) | |
1040 | { | |
1041 | return s->mac_reg[index] & 0xf; | |
1042 | } | |
1043 | ||
1044 | static uint32_t | |
1045 | mac_low11_read(E1000State *s, int index) | |
1046 | { | |
1047 | return s->mac_reg[index] & 0x7ff; | |
1048 | } | |
1049 | ||
1050 | static uint32_t | |
1051 | mac_low13_read(E1000State *s, int index) | |
1052 | { | |
1053 | return s->mac_reg[index] & 0x1fff; | |
1054 | } | |
1055 | ||
1056 | static uint32_t | |
1057 | mac_low16_read(E1000State *s, int index) | |
1058 | { | |
1059 | return s->mac_reg[index] & 0xffff; | |
1060 | } | |
1061 | ||
7c23b892 AZ |
1062 | static uint32_t |
1063 | mac_icr_read(E1000State *s, int index) | |
1064 | { | |
1065 | uint32_t ret = s->mac_reg[ICR]; | |
1066 | ||
1067 | DBGOUT(INTERRUPT, "ICR read: %x\n", ret); | |
1068 | set_interrupt_cause(s, 0, 0); | |
1069 | return ret; | |
1070 | } | |
1071 | ||
1072 | static uint32_t | |
1073 | mac_read_clr4(E1000State *s, int index) | |
1074 | { | |
1075 | uint32_t ret = s->mac_reg[index]; | |
1076 | ||
1077 | s->mac_reg[index] = 0; | |
1078 | return ret; | |
1079 | } | |
1080 | ||
1081 | static uint32_t | |
1082 | mac_read_clr8(E1000State *s, int index) | |
1083 | { | |
1084 | uint32_t ret = s->mac_reg[index]; | |
1085 | ||
1086 | s->mac_reg[index] = 0; | |
1087 | s->mac_reg[index-1] = 0; | |
1088 | return ret; | |
1089 | } | |
1090 | ||
1091 | static void | |
1092 | mac_writereg(E1000State *s, int index, uint32_t val) | |
1093 | { | |
7c36507c AK |
1094 | uint32_t macaddr[2]; |
1095 | ||
7c23b892 | 1096 | s->mac_reg[index] = val; |
7c36507c | 1097 | |
90d131fb | 1098 | if (index == RA + 1) { |
7c36507c AK |
1099 | macaddr[0] = cpu_to_le32(s->mac_reg[RA]); |
1100 | macaddr[1] = cpu_to_le32(s->mac_reg[RA + 1]); | |
1101 | qemu_format_nic_info_str(qemu_get_queue(s->nic), (uint8_t *)macaddr); | |
1102 | } | |
7c23b892 AZ |
1103 | } |
1104 | ||
1105 | static void | |
1106 | set_rdt(E1000State *s, int index, uint32_t val) | |
1107 | { | |
7c23b892 | 1108 | s->mac_reg[index] = val & 0xffff; |
e8b4c680 | 1109 | if (e1000_has_rxbufs(s, 1)) { |
b356f76d | 1110 | qemu_flush_queued_packets(qemu_get_queue(s->nic)); |
e8b4c680 | 1111 | } |
7c23b892 AZ |
1112 | } |
1113 | ||
1114 | static void | |
1115 | set_16bit(E1000State *s, int index, uint32_t val) | |
1116 | { | |
1117 | s->mac_reg[index] = val & 0xffff; | |
1118 | } | |
1119 | ||
1120 | static void | |
1121 | set_dlen(E1000State *s, int index, uint32_t val) | |
1122 | { | |
1123 | s->mac_reg[index] = val & 0xfff80; | |
1124 | } | |
1125 | ||
1126 | static void | |
1127 | set_tctl(E1000State *s, int index, uint32_t val) | |
1128 | { | |
1129 | s->mac_reg[index] = val; | |
1130 | s->mac_reg[TDT] &= 0xffff; | |
1131 | start_xmit(s); | |
1132 | } | |
1133 | ||
1134 | static void | |
1135 | set_icr(E1000State *s, int index, uint32_t val) | |
1136 | { | |
1137 | DBGOUT(INTERRUPT, "set_icr %x\n", val); | |
1138 | set_interrupt_cause(s, 0, s->mac_reg[ICR] & ~val); | |
1139 | } | |
1140 | ||
1141 | static void | |
1142 | set_imc(E1000State *s, int index, uint32_t val) | |
1143 | { | |
1144 | s->mac_reg[IMS] &= ~val; | |
1145 | set_ics(s, 0, 0); | |
1146 | } | |
1147 | ||
1148 | static void | |
1149 | set_ims(E1000State *s, int index, uint32_t val) | |
1150 | { | |
1151 | s->mac_reg[IMS] |= val; | |
1152 | set_ics(s, 0, 0); | |
1153 | } | |
1154 | ||
20f3e863 | 1155 | #define getreg(x) [x] = mac_readreg |
3b6b3a27 | 1156 | typedef uint32_t (*readops)(E1000State *, int); |
da5cf9a4 | 1157 | static const readops macreg_readops[] = { |
20f3e863 LB |
1158 | getreg(PBA), getreg(RCTL), getreg(TDH), getreg(TXDCTL), |
1159 | getreg(WUFC), getreg(TDT), getreg(CTRL), getreg(LEDCTL), | |
1160 | getreg(MANC), getreg(MDIC), getreg(SWSM), getreg(STATUS), | |
1161 | getreg(TORL), getreg(TOTL), getreg(IMS), getreg(TCTL), | |
1162 | getreg(RDH), getreg(RDT), getreg(VET), getreg(ICS), | |
1163 | getreg(TDBAL), getreg(TDBAH), getreg(RDBAH), getreg(RDBAL), | |
1164 | getreg(TDLEN), getreg(RDLEN), getreg(RDTR), getreg(RADV), | |
72ea771c LB |
1165 | getreg(TADV), getreg(ITR), getreg(FCRUC), getreg(IPAV), |
1166 | getreg(WUC), getreg(WUS), getreg(SCC), getreg(ECOL), | |
1167 | getreg(MCC), getreg(LATECOL), getreg(COLC), getreg(DC), | |
757704f1 | 1168 | getreg(TNCRS), getreg(SEQEC), getreg(CEXTERR), getreg(RLEC), |
72ea771c LB |
1169 | getreg(XONRXC), getreg(XONTXC), getreg(XOFFRXC), getreg(XOFFTXC), |
1170 | getreg(RFC), getreg(RJC), getreg(RNBC), getreg(TSCTFC), | |
3b274301 LB |
1171 | getreg(MGTPRC), getreg(MGTPDC), getreg(MGTPTC), getreg(GORCL), |
1172 | getreg(GOTCL), | |
20f3e863 LB |
1173 | |
1174 | [TOTH] = mac_read_clr8, [TORH] = mac_read_clr8, | |
3b274301 LB |
1175 | [GOTCH] = mac_read_clr8, [GORCH] = mac_read_clr8, |
1176 | [PRC64] = mac_read_clr4, [PRC127] = mac_read_clr4, | |
1177 | [PRC255] = mac_read_clr4, [PRC511] = mac_read_clr4, | |
1178 | [PRC1023] = mac_read_clr4, [PRC1522] = mac_read_clr4, | |
1179 | [PTC64] = mac_read_clr4, [PTC127] = mac_read_clr4, | |
1180 | [PTC255] = mac_read_clr4, [PTC511] = mac_read_clr4, | |
1181 | [PTC1023] = mac_read_clr4, [PTC1522] = mac_read_clr4, | |
20f3e863 LB |
1182 | [GPRC] = mac_read_clr4, [GPTC] = mac_read_clr4, |
1183 | [TPT] = mac_read_clr4, [TPR] = mac_read_clr4, | |
3b274301 LB |
1184 | [RUC] = mac_read_clr4, [ROC] = mac_read_clr4, |
1185 | [BPRC] = mac_read_clr4, [MPRC] = mac_read_clr4, | |
1186 | [TSCTC] = mac_read_clr4, [BPTC] = mac_read_clr4, | |
1187 | [MPTC] = mac_read_clr4, | |
20f3e863 LB |
1188 | [ICR] = mac_icr_read, [EECD] = get_eecd, |
1189 | [EERD] = flash_eerd_read, | |
72ea771c LB |
1190 | [RDFH] = mac_low13_read, [RDFT] = mac_low13_read, |
1191 | [RDFHS] = mac_low13_read, [RDFTS] = mac_low13_read, | |
1192 | [RDFPC] = mac_low13_read, | |
1193 | [TDFH] = mac_low11_read, [TDFT] = mac_low11_read, | |
1194 | [TDFHS] = mac_low13_read, [TDFTS] = mac_low13_read, | |
1195 | [TDFPC] = mac_low13_read, | |
1196 | [AIT] = mac_low16_read, | |
20f3e863 LB |
1197 | |
1198 | [CRCERRS ... MPC] = &mac_readreg, | |
72ea771c LB |
1199 | [IP6AT ... IP6AT+3] = &mac_readreg, [IP4AT ... IP4AT+6] = &mac_readreg, |
1200 | [FFLT ... FFLT+6] = &mac_low11_read, | |
20f3e863 | 1201 | [RA ... RA+31] = &mac_readreg, |
72ea771c | 1202 | [WUPM ... WUPM+31] = &mac_readreg, |
20f3e863 | 1203 | [MTA ... MTA+127] = &mac_readreg, |
8f2e8d1f | 1204 | [VFTA ... VFTA+127] = &mac_readreg, |
72ea771c LB |
1205 | [FFMT ... FFMT+254] = &mac_low4_read, |
1206 | [FFVT ... FFVT+254] = &mac_readreg, | |
1207 | [PBM ... PBM+16383] = &mac_readreg, | |
7c23b892 | 1208 | }; |
b1503cda | 1209 | enum { NREADOPS = ARRAY_SIZE(macreg_readops) }; |
7c23b892 | 1210 | |
20f3e863 | 1211 | #define putreg(x) [x] = mac_writereg |
3b6b3a27 | 1212 | typedef void (*writeops)(E1000State *, int, uint32_t); |
da5cf9a4 | 1213 | static const writeops macreg_writeops[] = { |
20f3e863 LB |
1214 | putreg(PBA), putreg(EERD), putreg(SWSM), putreg(WUFC), |
1215 | putreg(TDBAL), putreg(TDBAH), putreg(TXDCTL), putreg(RDBAH), | |
72ea771c LB |
1216 | putreg(RDBAL), putreg(LEDCTL), putreg(VET), putreg(FCRUC), |
1217 | putreg(TDFH), putreg(TDFT), putreg(TDFHS), putreg(TDFTS), | |
1218 | putreg(TDFPC), putreg(RDFH), putreg(RDFT), putreg(RDFHS), | |
1219 | putreg(RDFTS), putreg(RDFPC), putreg(IPAV), putreg(WUC), | |
1220 | putreg(WUS), putreg(AIT), | |
20f3e863 LB |
1221 | |
1222 | [TDLEN] = set_dlen, [RDLEN] = set_dlen, [TCTL] = set_tctl, | |
1223 | [TDT] = set_tctl, [MDIC] = set_mdic, [ICS] = set_ics, | |
1224 | [TDH] = set_16bit, [RDH] = set_16bit, [RDT] = set_rdt, | |
1225 | [IMC] = set_imc, [IMS] = set_ims, [ICR] = set_icr, | |
1226 | [EECD] = set_eecd, [RCTL] = set_rx_control, [CTRL] = set_ctrl, | |
1227 | [RDTR] = set_16bit, [RADV] = set_16bit, [TADV] = set_16bit, | |
1228 | [ITR] = set_16bit, | |
1229 | ||
72ea771c LB |
1230 | [IP6AT ... IP6AT+3] = &mac_writereg, [IP4AT ... IP4AT+6] = &mac_writereg, |
1231 | [FFLT ... FFLT+6] = &mac_writereg, | |
20f3e863 | 1232 | [RA ... RA+31] = &mac_writereg, |
72ea771c | 1233 | [WUPM ... WUPM+31] = &mac_writereg, |
20f3e863 | 1234 | [MTA ... MTA+127] = &mac_writereg, |
8f2e8d1f | 1235 | [VFTA ... VFTA+127] = &mac_writereg, |
72ea771c LB |
1236 | [FFMT ... FFMT+254] = &mac_writereg, [FFVT ... FFVT+254] = &mac_writereg, |
1237 | [PBM ... PBM+16383] = &mac_writereg, | |
7c23b892 | 1238 | }; |
b9d03e35 | 1239 | |
b1503cda | 1240 | enum { NWRITEOPS = ARRAY_SIZE(macreg_writeops) }; |
7c23b892 | 1241 | |
bc0f0674 LB |
1242 | enum { MAC_ACCESS_PARTIAL = 1, MAC_ACCESS_FLAG_NEEDED = 2 }; |
1243 | ||
1244 | #define markflag(x) ((E1000_FLAG_##x << 2) | MAC_ACCESS_FLAG_NEEDED) | |
1245 | /* In the array below the meaning of the bits is: [f|f|f|f|f|f|n|p] | |
1246 | * f - flag bits (up to 6 possible flags) | |
1247 | * n - flag needed | |
1248 | * p - partially implenented */ | |
1249 | static const uint8_t mac_reg_access[0x8000] = { | |
1250 | [RDTR] = markflag(MIT), [TADV] = markflag(MIT), | |
1251 | [RADV] = markflag(MIT), [ITR] = markflag(MIT), | |
72ea771c LB |
1252 | |
1253 | [IPAV] = markflag(MAC), [WUC] = markflag(MAC), | |
1254 | [IP6AT] = markflag(MAC), [IP4AT] = markflag(MAC), | |
1255 | [FFVT] = markflag(MAC), [WUPM] = markflag(MAC), | |
1256 | [ECOL] = markflag(MAC), [MCC] = markflag(MAC), | |
1257 | [DC] = markflag(MAC), [TNCRS] = markflag(MAC), | |
1258 | [RLEC] = markflag(MAC), [XONRXC] = markflag(MAC), | |
1259 | [XOFFTXC] = markflag(MAC), [RFC] = markflag(MAC), | |
1260 | [TSCTFC] = markflag(MAC), [MGTPRC] = markflag(MAC), | |
1261 | [WUS] = markflag(MAC), [AIT] = markflag(MAC), | |
1262 | [FFLT] = markflag(MAC), [FFMT] = markflag(MAC), | |
1263 | [SCC] = markflag(MAC), [FCRUC] = markflag(MAC), | |
1264 | [LATECOL] = markflag(MAC), [COLC] = markflag(MAC), | |
757704f1 | 1265 | [SEQEC] = markflag(MAC), [CEXTERR] = markflag(MAC), |
72ea771c LB |
1266 | [XONTXC] = markflag(MAC), [XOFFRXC] = markflag(MAC), |
1267 | [RJC] = markflag(MAC), [RNBC] = markflag(MAC), | |
1268 | [MGTPDC] = markflag(MAC), [MGTPTC] = markflag(MAC), | |
3b274301 LB |
1269 | [RUC] = markflag(MAC), [ROC] = markflag(MAC), |
1270 | [GORCL] = markflag(MAC), [GORCH] = markflag(MAC), | |
1271 | [GOTCL] = markflag(MAC), [GOTCH] = markflag(MAC), | |
1272 | [BPRC] = markflag(MAC), [MPRC] = markflag(MAC), | |
1273 | [TSCTC] = markflag(MAC), [PRC64] = markflag(MAC), | |
1274 | [PRC127] = markflag(MAC), [PRC255] = markflag(MAC), | |
1275 | [PRC511] = markflag(MAC), [PRC1023] = markflag(MAC), | |
1276 | [PRC1522] = markflag(MAC), [PTC64] = markflag(MAC), | |
1277 | [PTC127] = markflag(MAC), [PTC255] = markflag(MAC), | |
1278 | [PTC511] = markflag(MAC), [PTC1023] = markflag(MAC), | |
1279 | [PTC1522] = markflag(MAC), [MPTC] = markflag(MAC), | |
1280 | [BPTC] = markflag(MAC), | |
72ea771c LB |
1281 | |
1282 | [TDFH] = markflag(MAC) | MAC_ACCESS_PARTIAL, | |
1283 | [TDFT] = markflag(MAC) | MAC_ACCESS_PARTIAL, | |
1284 | [TDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL, | |
1285 | [TDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL, | |
1286 | [TDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL, | |
1287 | [RDFH] = markflag(MAC) | MAC_ACCESS_PARTIAL, | |
1288 | [RDFT] = markflag(MAC) | MAC_ACCESS_PARTIAL, | |
1289 | [RDFHS] = markflag(MAC) | MAC_ACCESS_PARTIAL, | |
1290 | [RDFTS] = markflag(MAC) | MAC_ACCESS_PARTIAL, | |
1291 | [RDFPC] = markflag(MAC) | MAC_ACCESS_PARTIAL, | |
1292 | [PBM] = markflag(MAC) | MAC_ACCESS_PARTIAL, | |
bc0f0674 LB |
1293 | }; |
1294 | ||
7c23b892 | 1295 | static void |
a8170e5e | 1296 | e1000_mmio_write(void *opaque, hwaddr addr, uint64_t val, |
ad00a9b9 | 1297 | unsigned size) |
7c23b892 AZ |
1298 | { |
1299 | E1000State *s = opaque; | |
8da3ff18 | 1300 | unsigned int index = (addr & 0x1ffff) >> 2; |
7c23b892 | 1301 | |
43ad7e3e | 1302 | if (index < NWRITEOPS && macreg_writeops[index]) { |
bc0f0674 LB |
1303 | if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED) |
1304 | || (s->compat_flags & (mac_reg_access[index] >> 2))) { | |
1305 | if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) { | |
1306 | DBGOUT(GENERAL, "Writing to register at offset: 0x%08x. " | |
1307 | "It is not fully implemented.\n", index<<2); | |
1308 | } | |
1309 | macreg_writeops[index](s, index, val); | |
1310 | } else { /* "flag needed" bit is set, but the flag is not active */ | |
1311 | DBGOUT(MMIO, "MMIO write attempt to disabled reg. addr=0x%08x\n", | |
1312 | index<<2); | |
1313 | } | |
43ad7e3e | 1314 | } else if (index < NREADOPS && macreg_readops[index]) { |
bc0f0674 LB |
1315 | DBGOUT(MMIO, "e1000_mmio_writel RO %x: 0x%04"PRIx64"\n", |
1316 | index<<2, val); | |
43ad7e3e | 1317 | } else { |
ad00a9b9 | 1318 | DBGOUT(UNKNOWN, "MMIO unknown write addr=0x%08x,val=0x%08"PRIx64"\n", |
7c23b892 | 1319 | index<<2, val); |
43ad7e3e | 1320 | } |
7c23b892 AZ |
1321 | } |
1322 | ||
ad00a9b9 | 1323 | static uint64_t |
a8170e5e | 1324 | e1000_mmio_read(void *opaque, hwaddr addr, unsigned size) |
7c23b892 AZ |
1325 | { |
1326 | E1000State *s = opaque; | |
8da3ff18 | 1327 | unsigned int index = (addr & 0x1ffff) >> 2; |
7c23b892 | 1328 | |
bc0f0674 LB |
1329 | if (index < NREADOPS && macreg_readops[index]) { |
1330 | if (!(mac_reg_access[index] & MAC_ACCESS_FLAG_NEEDED) | |
1331 | || (s->compat_flags & (mac_reg_access[index] >> 2))) { | |
1332 | if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) { | |
1333 | DBGOUT(GENERAL, "Reading register at offset: 0x%08x. " | |
1334 | "It is not fully implemented.\n", index<<2); | |
1335 | } | |
1336 | return macreg_readops[index](s, index); | |
1337 | } else { /* "flag needed" bit is set, but the flag is not active */ | |
1338 | DBGOUT(MMIO, "MMIO read attempt of disabled reg. addr=0x%08x\n", | |
1339 | index<<2); | |
1340 | } | |
1341 | } else { | |
1342 | DBGOUT(UNKNOWN, "MMIO unknown read addr=0x%08x\n", index<<2); | |
6b59fc74 | 1343 | } |
7c23b892 AZ |
1344 | return 0; |
1345 | } | |
1346 | ||
ad00a9b9 AK |
1347 | static const MemoryRegionOps e1000_mmio_ops = { |
1348 | .read = e1000_mmio_read, | |
1349 | .write = e1000_mmio_write, | |
1350 | .endianness = DEVICE_LITTLE_ENDIAN, | |
1351 | .impl = { | |
1352 | .min_access_size = 4, | |
1353 | .max_access_size = 4, | |
1354 | }, | |
1355 | }; | |
1356 | ||
a8170e5e | 1357 | static uint64_t e1000_io_read(void *opaque, hwaddr addr, |
ad00a9b9 | 1358 | unsigned size) |
7c23b892 | 1359 | { |
ad00a9b9 AK |
1360 | E1000State *s = opaque; |
1361 | ||
1362 | (void)s; | |
1363 | return 0; | |
7c23b892 AZ |
1364 | } |
1365 | ||
a8170e5e | 1366 | static void e1000_io_write(void *opaque, hwaddr addr, |
ad00a9b9 | 1367 | uint64_t val, unsigned size) |
7c23b892 | 1368 | { |
ad00a9b9 AK |
1369 | E1000State *s = opaque; |
1370 | ||
1371 | (void)s; | |
7c23b892 AZ |
1372 | } |
1373 | ||
ad00a9b9 AK |
1374 | static const MemoryRegionOps e1000_io_ops = { |
1375 | .read = e1000_io_read, | |
1376 | .write = e1000_io_write, | |
1377 | .endianness = DEVICE_LITTLE_ENDIAN, | |
1378 | }; | |
1379 | ||
e482dc3e | 1380 | static bool is_version_1(void *opaque, int version_id) |
7c23b892 | 1381 | { |
e482dc3e | 1382 | return version_id == 1; |
7c23b892 AZ |
1383 | } |
1384 | ||
44b1ff31 | 1385 | static int e1000_pre_save(void *opaque) |
ddcb73b7 MT |
1386 | { |
1387 | E1000State *s = opaque; | |
1388 | NetClientState *nc = qemu_get_queue(s->nic); | |
2af234e6 | 1389 | |
ddcb73b7 | 1390 | /* |
6a2acedb GS |
1391 | * If link is down and auto-negotiation is supported and ongoing, |
1392 | * complete auto-negotiation immediately. This allows us to look | |
1393 | * at MII_SR_AUTONEG_COMPLETE to infer link status on load. | |
ddcb73b7 | 1394 | */ |
d7a41552 GS |
1395 | if (nc->link_down && have_autoneg(s)) { |
1396 | s->phy_reg[PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE; | |
ddcb73b7 | 1397 | } |
44b1ff31 | 1398 | |
ff214d42 DDAG |
1399 | /* Decide which set of props to migrate in the main structure */ |
1400 | if (chkflag(TSO) || !s->use_tso_for_migration) { | |
1401 | /* Either we're migrating with the extra subsection, in which | |
1402 | * case the mig_props is always 'props' OR | |
1403 | * we've not got the subsection, but 'props' was the last | |
1404 | * updated. | |
1405 | */ | |
1406 | s->mig_props = s->tx.props; | |
1407 | } else { | |
1408 | /* We're not using the subsection, and 'tso_props' was | |
1409 | * the last updated. | |
1410 | */ | |
1411 | s->mig_props = s->tx.tso_props; | |
1412 | } | |
44b1ff31 | 1413 | return 0; |
ddcb73b7 MT |
1414 | } |
1415 | ||
e4b82364 AK |
1416 | static int e1000_post_load(void *opaque, int version_id) |
1417 | { | |
1418 | E1000State *s = opaque; | |
b356f76d | 1419 | NetClientState *nc = qemu_get_queue(s->nic); |
e4b82364 | 1420 | |
bc0f0674 | 1421 | if (!chkflag(MIT)) { |
e9845f09 VM |
1422 | s->mac_reg[ITR] = s->mac_reg[RDTR] = s->mac_reg[RADV] = |
1423 | s->mac_reg[TADV] = 0; | |
1424 | s->mit_irq_level = false; | |
1425 | } | |
1426 | s->mit_ide = 0; | |
f46efa9b JW |
1427 | s->mit_timer_on = true; |
1428 | timer_mod(s->mit_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 1); | |
e9845f09 | 1429 | |
e4b82364 | 1430 | /* nc.link_down can't be migrated, so infer link_down according |
ddcb73b7 MT |
1431 | * to link status bit in mac_reg[STATUS]. |
1432 | * Alternatively, restart link negotiation if it was in progress. */ | |
b356f76d | 1433 | nc->link_down = (s->mac_reg[STATUS] & E1000_STATUS_LU) == 0; |
2af234e6 | 1434 | |
d7a41552 | 1435 | if (have_autoneg(s) && |
ddcb73b7 MT |
1436 | !(s->phy_reg[PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) { |
1437 | nc->link_down = false; | |
d7a41552 GS |
1438 | timer_mod(s->autoneg_timer, |
1439 | qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500); | |
ddcb73b7 | 1440 | } |
e4b82364 | 1441 | |
59354484 | 1442 | s->tx.props = s->mig_props; |
3c4053c5 DDAG |
1443 | if (!s->received_tx_tso) { |
1444 | /* We received only one set of offload data (tx.props) | |
1445 | * and haven't got tx.tso_props. The best we can do | |
1446 | * is dupe the data. | |
1447 | */ | |
59354484 | 1448 | s->tx.tso_props = s->mig_props; |
3c4053c5 DDAG |
1449 | } |
1450 | return 0; | |
1451 | } | |
1452 | ||
1453 | static int e1000_tx_tso_post_load(void *opaque, int version_id) | |
1454 | { | |
1455 | E1000State *s = opaque; | |
1456 | s->received_tx_tso = true; | |
e4b82364 AK |
1457 | return 0; |
1458 | } | |
1459 | ||
e9845f09 VM |
1460 | static bool e1000_mit_state_needed(void *opaque) |
1461 | { | |
1462 | E1000State *s = opaque; | |
1463 | ||
bc0f0674 | 1464 | return chkflag(MIT); |
e9845f09 VM |
1465 | } |
1466 | ||
9e117734 LB |
1467 | static bool e1000_full_mac_needed(void *opaque) |
1468 | { | |
1469 | E1000State *s = opaque; | |
1470 | ||
bc0f0674 | 1471 | return chkflag(MAC); |
9e117734 LB |
1472 | } |
1473 | ||
46f2a9ec DDAG |
1474 | static bool e1000_tso_state_needed(void *opaque) |
1475 | { | |
1476 | E1000State *s = opaque; | |
1477 | ||
1478 | return chkflag(TSO); | |
1479 | } | |
1480 | ||
e9845f09 VM |
1481 | static const VMStateDescription vmstate_e1000_mit_state = { |
1482 | .name = "e1000/mit_state", | |
1483 | .version_id = 1, | |
1484 | .minimum_version_id = 1, | |
5cd8cada | 1485 | .needed = e1000_mit_state_needed, |
d49805ae | 1486 | .fields = (VMStateField[]) { |
e9845f09 VM |
1487 | VMSTATE_UINT32(mac_reg[RDTR], E1000State), |
1488 | VMSTATE_UINT32(mac_reg[RADV], E1000State), | |
1489 | VMSTATE_UINT32(mac_reg[TADV], E1000State), | |
1490 | VMSTATE_UINT32(mac_reg[ITR], E1000State), | |
1491 | VMSTATE_BOOL(mit_irq_level, E1000State), | |
1492 | VMSTATE_END_OF_LIST() | |
1493 | } | |
1494 | }; | |
1495 | ||
9e117734 LB |
1496 | static const VMStateDescription vmstate_e1000_full_mac_state = { |
1497 | .name = "e1000/full_mac_state", | |
1498 | .version_id = 1, | |
1499 | .minimum_version_id = 1, | |
1500 | .needed = e1000_full_mac_needed, | |
1501 | .fields = (VMStateField[]) { | |
1502 | VMSTATE_UINT32_ARRAY(mac_reg, E1000State, 0x8000), | |
1503 | VMSTATE_END_OF_LIST() | |
1504 | } | |
1505 | }; | |
1506 | ||
4ae4bf5b DDAG |
1507 | static const VMStateDescription vmstate_e1000_tx_tso_state = { |
1508 | .name = "e1000/tx_tso_state", | |
1509 | .version_id = 1, | |
1510 | .minimum_version_id = 1, | |
46f2a9ec | 1511 | .needed = e1000_tso_state_needed, |
3c4053c5 | 1512 | .post_load = e1000_tx_tso_post_load, |
4ae4bf5b DDAG |
1513 | .fields = (VMStateField[]) { |
1514 | VMSTATE_UINT8(tx.tso_props.ipcss, E1000State), | |
1515 | VMSTATE_UINT8(tx.tso_props.ipcso, E1000State), | |
1516 | VMSTATE_UINT16(tx.tso_props.ipcse, E1000State), | |
1517 | VMSTATE_UINT8(tx.tso_props.tucss, E1000State), | |
1518 | VMSTATE_UINT8(tx.tso_props.tucso, E1000State), | |
1519 | VMSTATE_UINT16(tx.tso_props.tucse, E1000State), | |
1520 | VMSTATE_UINT32(tx.tso_props.paylen, E1000State), | |
1521 | VMSTATE_UINT8(tx.tso_props.hdr_len, E1000State), | |
1522 | VMSTATE_UINT16(tx.tso_props.mss, E1000State), | |
1523 | VMSTATE_INT8(tx.tso_props.ip, E1000State), | |
1524 | VMSTATE_INT8(tx.tso_props.tcp, E1000State), | |
1525 | VMSTATE_END_OF_LIST() | |
1526 | } | |
1527 | }; | |
1528 | ||
e482dc3e JQ |
1529 | static const VMStateDescription vmstate_e1000 = { |
1530 | .name = "e1000", | |
4ae4bf5b | 1531 | .version_id = 2, |
e482dc3e | 1532 | .minimum_version_id = 1, |
ddcb73b7 | 1533 | .pre_save = e1000_pre_save, |
e4b82364 | 1534 | .post_load = e1000_post_load, |
d49805ae | 1535 | .fields = (VMStateField[]) { |
b08340d5 | 1536 | VMSTATE_PCI_DEVICE(parent_obj, E1000State), |
e482dc3e JQ |
1537 | VMSTATE_UNUSED_TEST(is_version_1, 4), /* was instance id */ |
1538 | VMSTATE_UNUSED(4), /* Was mmio_base. */ | |
1539 | VMSTATE_UINT32(rxbuf_size, E1000State), | |
1540 | VMSTATE_UINT32(rxbuf_min_shift, E1000State), | |
1541 | VMSTATE_UINT32(eecd_state.val_in, E1000State), | |
1542 | VMSTATE_UINT16(eecd_state.bitnum_in, E1000State), | |
1543 | VMSTATE_UINT16(eecd_state.bitnum_out, E1000State), | |
1544 | VMSTATE_UINT16(eecd_state.reading, E1000State), | |
1545 | VMSTATE_UINT32(eecd_state.old_eecd, E1000State), | |
59354484 DDAG |
1546 | VMSTATE_UINT8(mig_props.ipcss, E1000State), |
1547 | VMSTATE_UINT8(mig_props.ipcso, E1000State), | |
1548 | VMSTATE_UINT16(mig_props.ipcse, E1000State), | |
1549 | VMSTATE_UINT8(mig_props.tucss, E1000State), | |
1550 | VMSTATE_UINT8(mig_props.tucso, E1000State), | |
1551 | VMSTATE_UINT16(mig_props.tucse, E1000State), | |
1552 | VMSTATE_UINT32(mig_props.paylen, E1000State), | |
1553 | VMSTATE_UINT8(mig_props.hdr_len, E1000State), | |
1554 | VMSTATE_UINT16(mig_props.mss, E1000State), | |
e482dc3e JQ |
1555 | VMSTATE_UINT16(tx.size, E1000State), |
1556 | VMSTATE_UINT16(tx.tso_frames, E1000State), | |
7d08c73e | 1557 | VMSTATE_UINT8(tx.sum_needed, E1000State), |
59354484 DDAG |
1558 | VMSTATE_INT8(mig_props.ip, E1000State), |
1559 | VMSTATE_INT8(mig_props.tcp, E1000State), | |
e482dc3e JQ |
1560 | VMSTATE_BUFFER(tx.header, E1000State), |
1561 | VMSTATE_BUFFER(tx.data, E1000State), | |
1562 | VMSTATE_UINT16_ARRAY(eeprom_data, E1000State, 64), | |
1563 | VMSTATE_UINT16_ARRAY(phy_reg, E1000State, 0x20), | |
1564 | VMSTATE_UINT32(mac_reg[CTRL], E1000State), | |
1565 | VMSTATE_UINT32(mac_reg[EECD], E1000State), | |
1566 | VMSTATE_UINT32(mac_reg[EERD], E1000State), | |
1567 | VMSTATE_UINT32(mac_reg[GPRC], E1000State), | |
1568 | VMSTATE_UINT32(mac_reg[GPTC], E1000State), | |
1569 | VMSTATE_UINT32(mac_reg[ICR], E1000State), | |
1570 | VMSTATE_UINT32(mac_reg[ICS], E1000State), | |
1571 | VMSTATE_UINT32(mac_reg[IMC], E1000State), | |
1572 | VMSTATE_UINT32(mac_reg[IMS], E1000State), | |
1573 | VMSTATE_UINT32(mac_reg[LEDCTL], E1000State), | |
1574 | VMSTATE_UINT32(mac_reg[MANC], E1000State), | |
1575 | VMSTATE_UINT32(mac_reg[MDIC], E1000State), | |
1576 | VMSTATE_UINT32(mac_reg[MPC], E1000State), | |
1577 | VMSTATE_UINT32(mac_reg[PBA], E1000State), | |
1578 | VMSTATE_UINT32(mac_reg[RCTL], E1000State), | |
1579 | VMSTATE_UINT32(mac_reg[RDBAH], E1000State), | |
1580 | VMSTATE_UINT32(mac_reg[RDBAL], E1000State), | |
1581 | VMSTATE_UINT32(mac_reg[RDH], E1000State), | |
1582 | VMSTATE_UINT32(mac_reg[RDLEN], E1000State), | |
1583 | VMSTATE_UINT32(mac_reg[RDT], E1000State), | |
1584 | VMSTATE_UINT32(mac_reg[STATUS], E1000State), | |
1585 | VMSTATE_UINT32(mac_reg[SWSM], E1000State), | |
1586 | VMSTATE_UINT32(mac_reg[TCTL], E1000State), | |
1587 | VMSTATE_UINT32(mac_reg[TDBAH], E1000State), | |
1588 | VMSTATE_UINT32(mac_reg[TDBAL], E1000State), | |
1589 | VMSTATE_UINT32(mac_reg[TDH], E1000State), | |
1590 | VMSTATE_UINT32(mac_reg[TDLEN], E1000State), | |
1591 | VMSTATE_UINT32(mac_reg[TDT], E1000State), | |
1592 | VMSTATE_UINT32(mac_reg[TORH], E1000State), | |
1593 | VMSTATE_UINT32(mac_reg[TORL], E1000State), | |
1594 | VMSTATE_UINT32(mac_reg[TOTH], E1000State), | |
1595 | VMSTATE_UINT32(mac_reg[TOTL], E1000State), | |
1596 | VMSTATE_UINT32(mac_reg[TPR], E1000State), | |
1597 | VMSTATE_UINT32(mac_reg[TPT], E1000State), | |
1598 | VMSTATE_UINT32(mac_reg[TXDCTL], E1000State), | |
1599 | VMSTATE_UINT32(mac_reg[WUFC], E1000State), | |
1600 | VMSTATE_UINT32(mac_reg[VET], E1000State), | |
1601 | VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, RA, 32), | |
1602 | VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, MTA, 128), | |
1603 | VMSTATE_UINT32_SUB_ARRAY(mac_reg, E1000State, VFTA, 128), | |
1604 | VMSTATE_END_OF_LIST() | |
e9845f09 | 1605 | }, |
5cd8cada JQ |
1606 | .subsections = (const VMStateDescription*[]) { |
1607 | &vmstate_e1000_mit_state, | |
9e117734 | 1608 | &vmstate_e1000_full_mac_state, |
4ae4bf5b | 1609 | &vmstate_e1000_tx_tso_state, |
5cd8cada | 1610 | NULL |
e482dc3e JQ |
1611 | } |
1612 | }; | |
7c23b892 | 1613 | |
8597f2e1 GS |
1614 | /* |
1615 | * EEPROM contents documented in Tables 5-2 and 5-3, pp. 98-102. | |
80867bdb | 1616 | * Note: A valid DevId will be inserted during pci_e1000_realize(). |
8597f2e1 | 1617 | */ |
88b4e9db | 1618 | static const uint16_t e1000_eeprom_template[64] = { |
7c23b892 | 1619 | 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0x0000, |
8597f2e1 | 1620 | 0x3000, 0x1000, 0x6403, 0 /*DevId*/, 0x8086, 0 /*DevId*/, 0x8086, 0x3040, |
7c23b892 AZ |
1621 | 0x0008, 0x2000, 0x7e14, 0x0048, 0x1000, 0x00d8, 0x0000, 0x2700, |
1622 | 0x6cc9, 0x3150, 0x0722, 0x040b, 0x0984, 0x0000, 0xc000, 0x0706, | |
1623 | 0x1008, 0x0000, 0x0f04, 0x7fff, 0x4d01, 0xffff, 0xffff, 0xffff, | |
1624 | 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, | |
1625 | 0x0100, 0x4000, 0x121c, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, | |
1626 | 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x0000, | |
1627 | }; | |
1628 | ||
7c23b892 AZ |
1629 | /* PCI interface */ |
1630 | ||
7c23b892 | 1631 | static void |
ad00a9b9 | 1632 | e1000_mmio_setup(E1000State *d) |
7c23b892 | 1633 | { |
f65ed4c1 AL |
1634 | int i; |
1635 | const uint32_t excluded_regs[] = { | |
1636 | E1000_MDIC, E1000_ICR, E1000_ICS, E1000_IMS, | |
1637 | E1000_IMC, E1000_TCTL, E1000_TDT, PNPMMIO_SIZE | |
1638 | }; | |
1639 | ||
eedfac6f PB |
1640 | memory_region_init_io(&d->mmio, OBJECT(d), &e1000_mmio_ops, d, |
1641 | "e1000-mmio", PNPMMIO_SIZE); | |
ad00a9b9 | 1642 | memory_region_add_coalescing(&d->mmio, 0, excluded_regs[0]); |
f65ed4c1 | 1643 | for (i = 0; excluded_regs[i] != PNPMMIO_SIZE; i++) |
ad00a9b9 AK |
1644 | memory_region_add_coalescing(&d->mmio, excluded_regs[i] + 4, |
1645 | excluded_regs[i+1] - excluded_regs[i] - 4); | |
eedfac6f | 1646 | memory_region_init_io(&d->io, OBJECT(d), &e1000_io_ops, d, "e1000-io", IOPORT_SIZE); |
7c23b892 AZ |
1647 | } |
1648 | ||
f90c2bcd | 1649 | static void |
4b09be85 AL |
1650 | pci_e1000_uninit(PCIDevice *dev) |
1651 | { | |
567a3c9e | 1652 | E1000State *d = E1000(dev); |
4b09be85 | 1653 | |
bc72ad67 | 1654 | timer_free(d->autoneg_timer); |
e9845f09 | 1655 | timer_free(d->mit_timer); |
157628d0 | 1656 | timer_free(d->flush_queue_timer); |
948ecf21 | 1657 | qemu_del_nic(d->nic); |
4b09be85 AL |
1658 | } |
1659 | ||
a03e2aec | 1660 | static NetClientInfo net_e1000_info = { |
f394b2e2 | 1661 | .type = NET_CLIENT_DRIVER_NIC, |
a03e2aec MM |
1662 | .size = sizeof(NICState), |
1663 | .can_receive = e1000_can_receive, | |
1664 | .receive = e1000_receive, | |
97410dde | 1665 | .receive_iov = e1000_receive_iov, |
a03e2aec MM |
1666 | .link_status_changed = e1000_set_link_status, |
1667 | }; | |
1668 | ||
20302e71 MT |
1669 | static void e1000_write_config(PCIDevice *pci_dev, uint32_t address, |
1670 | uint32_t val, int len) | |
1671 | { | |
1672 | E1000State *s = E1000(pci_dev); | |
1673 | ||
1674 | pci_default_write_config(pci_dev, address, val, len); | |
1675 | ||
1676 | if (range_covers_byte(address, len, PCI_COMMAND) && | |
1677 | (pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER)) { | |
1678 | qemu_flush_queued_packets(qemu_get_queue(s->nic)); | |
1679 | } | |
1680 | } | |
1681 | ||
9af21dbe | 1682 | static void pci_e1000_realize(PCIDevice *pci_dev, Error **errp) |
7c23b892 | 1683 | { |
567a3c9e PC |
1684 | DeviceState *dev = DEVICE(pci_dev); |
1685 | E1000State *d = E1000(pci_dev); | |
7c23b892 | 1686 | uint8_t *pci_conf; |
fbdaa002 | 1687 | uint8_t *macaddr; |
aff427a1 | 1688 | |
20302e71 MT |
1689 | pci_dev->config_write = e1000_write_config; |
1690 | ||
b08340d5 | 1691 | pci_conf = pci_dev->config; |
7c23b892 | 1692 | |
a9cbacb0 MT |
1693 | /* TODO: RST# value should be 0, PCI spec 6.2.4 */ |
1694 | pci_conf[PCI_CACHE_LINE_SIZE] = 0x10; | |
7c23b892 | 1695 | |
817e0b6f | 1696 | pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */ |
7c23b892 | 1697 | |
ad00a9b9 | 1698 | e1000_mmio_setup(d); |
7c23b892 | 1699 | |
b08340d5 | 1700 | pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio); |
7c23b892 | 1701 | |
b08340d5 | 1702 | pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->io); |
7c23b892 | 1703 | |
fbdaa002 GH |
1704 | qemu_macaddr_default_if_unset(&d->conf.macaddr); |
1705 | macaddr = d->conf.macaddr.a; | |
093454e2 DF |
1706 | |
1707 | e1000x_core_prepare_eeprom(d->eeprom_data, | |
1708 | e1000_eeprom_template, | |
1709 | sizeof(e1000_eeprom_template), | |
1710 | PCI_DEVICE_GET_CLASS(pci_dev)->device_id, | |
1711 | macaddr); | |
7c23b892 | 1712 | |
a03e2aec | 1713 | d->nic = qemu_new_nic(&net_e1000_info, &d->conf, |
567a3c9e | 1714 | object_get_typename(OBJECT(d)), dev->id, d); |
7c23b892 | 1715 | |
b356f76d | 1716 | qemu_format_nic_info_str(qemu_get_queue(d->nic), macaddr); |
1ca4d09a | 1717 | |
bc72ad67 | 1718 | d->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, e1000_autoneg_timer, d); |
e9845f09 | 1719 | d->mit_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000_mit_timer, d); |
157628d0 YCL |
1720 | d->flush_queue_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, |
1721 | e1000_flush_queue_timer, d); | |
9d07d757 | 1722 | } |
72da4208 | 1723 | |
fbdaa002 GH |
1724 | static void qdev_e1000_reset(DeviceState *dev) |
1725 | { | |
567a3c9e | 1726 | E1000State *d = E1000(dev); |
fbdaa002 GH |
1727 | e1000_reset(d); |
1728 | } | |
1729 | ||
40021f08 AL |
1730 | static Property e1000_properties[] = { |
1731 | DEFINE_NIC_PROPERTIES(E1000State, conf), | |
2af234e6 MT |
1732 | DEFINE_PROP_BIT("autonegotiation", E1000State, |
1733 | compat_flags, E1000_FLAG_AUTONEG_BIT, true), | |
e9845f09 VM |
1734 | DEFINE_PROP_BIT("mitigation", E1000State, |
1735 | compat_flags, E1000_FLAG_MIT_BIT, true), | |
ba63ec85 LB |
1736 | DEFINE_PROP_BIT("extra_mac_registers", E1000State, |
1737 | compat_flags, E1000_FLAG_MAC_BIT, true), | |
46f2a9ec DDAG |
1738 | DEFINE_PROP_BIT("migrate_tso_props", E1000State, |
1739 | compat_flags, E1000_FLAG_TSO_BIT, true), | |
40021f08 AL |
1740 | DEFINE_PROP_END_OF_LIST(), |
1741 | }; | |
1742 | ||
8597f2e1 GS |
1743 | typedef struct E1000Info { |
1744 | const char *name; | |
1745 | uint16_t device_id; | |
1746 | uint8_t revision; | |
1747 | uint16_t phy_id2; | |
8597f2e1 GS |
1748 | } E1000Info; |
1749 | ||
40021f08 AL |
1750 | static void e1000_class_init(ObjectClass *klass, void *data) |
1751 | { | |
39bffca2 | 1752 | DeviceClass *dc = DEVICE_CLASS(klass); |
40021f08 | 1753 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
c51325d8 | 1754 | E1000BaseClass *e = E1000_CLASS(klass); |
8597f2e1 | 1755 | const E1000Info *info = data; |
40021f08 | 1756 | |
9af21dbe | 1757 | k->realize = pci_e1000_realize; |
40021f08 | 1758 | k->exit = pci_e1000_uninit; |
c45e5b5b | 1759 | k->romfile = "efi-e1000.rom"; |
40021f08 | 1760 | k->vendor_id = PCI_VENDOR_ID_INTEL; |
8597f2e1 GS |
1761 | k->device_id = info->device_id; |
1762 | k->revision = info->revision; | |
1763 | e->phy_id2 = info->phy_id2; | |
40021f08 | 1764 | k->class_id = PCI_CLASS_NETWORK_ETHERNET; |
125ee0ed | 1765 | set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); |
39bffca2 AL |
1766 | dc->desc = "Intel Gigabit Ethernet"; |
1767 | dc->reset = qdev_e1000_reset; | |
1768 | dc->vmsd = &vmstate_e1000; | |
4f67d30b | 1769 | device_class_set_props(dc, e1000_properties); |
40021f08 AL |
1770 | } |
1771 | ||
5df3bf62 GA |
1772 | static void e1000_instance_init(Object *obj) |
1773 | { | |
1774 | E1000State *n = E1000(obj); | |
1775 | device_add_bootindex_property(obj, &n->conf.bootindex, | |
1776 | "bootindex", "/ethernet-phy@0", | |
40c2281c | 1777 | DEVICE(n)); |
5df3bf62 GA |
1778 | } |
1779 | ||
8597f2e1 GS |
1780 | static const TypeInfo e1000_base_info = { |
1781 | .name = TYPE_E1000_BASE, | |
39bffca2 AL |
1782 | .parent = TYPE_PCI_DEVICE, |
1783 | .instance_size = sizeof(E1000State), | |
5df3bf62 | 1784 | .instance_init = e1000_instance_init, |
8597f2e1 GS |
1785 | .class_size = sizeof(E1000BaseClass), |
1786 | .abstract = true, | |
fd3b02c8 EH |
1787 | .interfaces = (InterfaceInfo[]) { |
1788 | { INTERFACE_CONVENTIONAL_PCI_DEVICE }, | |
1789 | { }, | |
1790 | }, | |
8597f2e1 GS |
1791 | }; |
1792 | ||
1793 | static const E1000Info e1000_devices[] = { | |
1794 | { | |
83044020 | 1795 | .name = "e1000", |
8597f2e1 GS |
1796 | .device_id = E1000_DEV_ID_82540EM, |
1797 | .revision = 0x03, | |
1798 | .phy_id2 = E1000_PHY_ID2_8254xx_DEFAULT, | |
1799 | }, | |
1800 | { | |
1801 | .name = "e1000-82544gc", | |
1802 | .device_id = E1000_DEV_ID_82544GC_COPPER, | |
1803 | .revision = 0x03, | |
1804 | .phy_id2 = E1000_PHY_ID2_82544x, | |
1805 | }, | |
1806 | { | |
1807 | .name = "e1000-82545em", | |
1808 | .device_id = E1000_DEV_ID_82545EM_COPPER, | |
1809 | .revision = 0x03, | |
1810 | .phy_id2 = E1000_PHY_ID2_8254xx_DEFAULT, | |
1811 | }, | |
8597f2e1 GS |
1812 | }; |
1813 | ||
83f7d43a | 1814 | static void e1000_register_types(void) |
9d07d757 | 1815 | { |
8597f2e1 GS |
1816 | int i; |
1817 | ||
1818 | type_register_static(&e1000_base_info); | |
1819 | for (i = 0; i < ARRAY_SIZE(e1000_devices); i++) { | |
1820 | const E1000Info *info = &e1000_devices[i]; | |
1821 | TypeInfo type_info = {}; | |
1822 | ||
1823 | type_info.name = info->name; | |
1824 | type_info.parent = TYPE_E1000_BASE; | |
1825 | type_info.class_data = (void *)info; | |
1826 | type_info.class_init = e1000_class_init; | |
1827 | ||
1828 | type_register(&type_info); | |
1829 | } | |
7c23b892 | 1830 | } |
9d07d757 | 1831 | |
83f7d43a | 1832 | type_init(e1000_register_types) |