xref: /openbmc/qemu/hw/net/e1000e_core.c (revision 17ccd016)
1 /*
2  * Core code for QEMU e1000e emulation
3  *
4  * Software developer's manuals:
5  * http://www.intel.com/content/dam/doc/datasheet/82574l-gbe-controller-datasheet.pdf
6  *
7  * Copyright (c) 2015 Ravello Systems LTD (http://ravellosystems.com)
8  * Developed by Daynix Computing LTD (http://www.daynix.com)
9  *
10  * Authors:
11  * Dmitry Fleytman <dmitry@daynix.com>
12  * Leonid Bloch <leonid@daynix.com>
13  * Yan Vugenfirer <yan@daynix.com>
14  *
15  * Based on work done by:
16  * Nir Peleg, Tutis Systems Ltd. for Qumranet Inc.
17  * Copyright (c) 2008 Qumranet
18  * Based on work done by:
19  * Copyright (c) 2007 Dan Aloni
20  * Copyright (c) 2004 Antony T Curtis
21  *
22  * This library is free software; you can redistribute it and/or
23  * modify it under the terms of the GNU Lesser General Public
24  * License as published by the Free Software Foundation; either
25  * version 2.1 of the License, or (at your option) any later version.
26  *
27  * This library is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
30  * Lesser General Public License for more details.
31  *
32  * You should have received a copy of the GNU Lesser General Public
33  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
34  */
35 
36 #include "qemu/osdep.h"
37 #include "qemu/log.h"
38 #include "net/net.h"
39 #include "net/tap.h"
40 #include "hw/net/mii.h"
41 #include "hw/pci/msi.h"
42 #include "hw/pci/msix.h"
43 #include "sysemu/runstate.h"
44 
45 #include "net_tx_pkt.h"
46 #include "net_rx_pkt.h"
47 
48 #include "e1000_common.h"
49 #include "e1000x_common.h"
50 #include "e1000e_core.h"
51 
52 #include "trace.h"
53 
54 /* No more then 7813 interrupts per second according to spec 10.2.4.2 */
55 #define E1000E_MIN_XITR     (500)
56 
57 #define E1000E_MAX_TX_FRAGS (64)
58 
59 union e1000_rx_desc_union {
60     struct e1000_rx_desc legacy;
61     union e1000_rx_desc_extended extended;
62     union e1000_rx_desc_packet_split packet_split;
63 };
64 
65 static ssize_t
66 e1000e_receive_internal(E1000ECore *core, const struct iovec *iov, int iovcnt,
67                         bool has_vnet);
68 
69 static inline void
70 e1000e_set_interrupt_cause(E1000ECore *core, uint32_t val);
71 
72 static void e1000e_reset(E1000ECore *core, bool sw);
73 
74 static inline void
75 e1000e_process_ts_option(E1000ECore *core, struct e1000_tx_desc *dp)
76 {
77     if (le32_to_cpu(dp->upper.data) & E1000_TXD_EXTCMD_TSTAMP) {
78         trace_e1000e_wrn_no_ts_support();
79     }
80 }
81 
82 static inline void
83 e1000e_process_snap_option(E1000ECore *core, uint32_t cmd_and_length)
84 {
85     if (cmd_and_length & E1000_TXD_CMD_SNAP) {
86         trace_e1000e_wrn_no_snap_support();
87     }
88 }
89 
90 static inline void
91 e1000e_raise_legacy_irq(E1000ECore *core)
92 {
93     trace_e1000e_irq_legacy_notify(true);
94     e1000x_inc_reg_if_not_full(core->mac, IAC);
95     pci_set_irq(core->owner, 1);
96 }
97 
98 static inline void
99 e1000e_lower_legacy_irq(E1000ECore *core)
100 {
101     trace_e1000e_irq_legacy_notify(false);
102     pci_set_irq(core->owner, 0);
103 }
104 
105 static inline void
106 e1000e_intrmgr_rearm_timer(E1000IntrDelayTimer *timer)
107 {
108     int64_t delay_ns = (int64_t) timer->core->mac[timer->delay_reg] *
109                                  timer->delay_resolution_ns;
110 
111     trace_e1000e_irq_rearm_timer(timer->delay_reg << 2, delay_ns);
112 
113     timer_mod(timer->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + delay_ns);
114 
115     timer->running = true;
116 }
117 
118 static void
119 e1000e_intmgr_timer_resume(E1000IntrDelayTimer *timer)
120 {
121     if (timer->running) {
122         e1000e_intrmgr_rearm_timer(timer);
123     }
124 }
125 
126 static void
127 e1000e_intmgr_timer_pause(E1000IntrDelayTimer *timer)
128 {
129     if (timer->running) {
130         timer_del(timer->timer);
131     }
132 }
133 
134 static inline void
135 e1000e_intrmgr_stop_timer(E1000IntrDelayTimer *timer)
136 {
137     if (timer->running) {
138         timer_del(timer->timer);
139         timer->running = false;
140     }
141 }
142 
143 static inline void
144 e1000e_intrmgr_fire_delayed_interrupts(E1000ECore *core)
145 {
146     trace_e1000e_irq_fire_delayed_interrupts();
147     e1000e_set_interrupt_cause(core, 0);
148 }
149 
150 static void
151 e1000e_intrmgr_on_timer(void *opaque)
152 {
153     E1000IntrDelayTimer *timer = opaque;
154 
155     trace_e1000e_irq_throttling_timer(timer->delay_reg << 2);
156 
157     timer->running = false;
158     e1000e_intrmgr_fire_delayed_interrupts(timer->core);
159 }
160 
161 static void
162 e1000e_intrmgr_on_throttling_timer(void *opaque)
163 {
164     E1000IntrDelayTimer *timer = opaque;
165 
166     timer->running = false;
167 
168     if (timer->core->mac[IMS] & timer->core->mac[ICR]) {
169         if (msi_enabled(timer->core->owner)) {
170             trace_e1000e_irq_msi_notify_postponed();
171             msi_notify(timer->core->owner, 0);
172         } else {
173             trace_e1000e_irq_legacy_notify_postponed();
174             e1000e_raise_legacy_irq(timer->core);
175         }
176     }
177 }
178 
179 static void
180 e1000e_intrmgr_on_msix_throttling_timer(void *opaque)
181 {
182     E1000IntrDelayTimer *timer = opaque;
183     int idx = timer - &timer->core->eitr[0];
184 
185     timer->running = false;
186 
187     trace_e1000e_irq_msix_notify_postponed_vec(idx);
188     msix_notify(timer->core->owner, idx);
189 }
190 
191 static void
192 e1000e_intrmgr_initialize_all_timers(E1000ECore *core, bool create)
193 {
194     int i;
195 
196     core->radv.delay_reg = RADV;
197     core->rdtr.delay_reg = RDTR;
198     core->raid.delay_reg = RAID;
199     core->tadv.delay_reg = TADV;
200     core->tidv.delay_reg = TIDV;
201 
202     core->radv.delay_resolution_ns = E1000_INTR_DELAY_NS_RES;
203     core->rdtr.delay_resolution_ns = E1000_INTR_DELAY_NS_RES;
204     core->raid.delay_resolution_ns = E1000_INTR_DELAY_NS_RES;
205     core->tadv.delay_resolution_ns = E1000_INTR_DELAY_NS_RES;
206     core->tidv.delay_resolution_ns = E1000_INTR_DELAY_NS_RES;
207 
208     core->radv.core = core;
209     core->rdtr.core = core;
210     core->raid.core = core;
211     core->tadv.core = core;
212     core->tidv.core = core;
213 
214     core->itr.core = core;
215     core->itr.delay_reg = ITR;
216     core->itr.delay_resolution_ns = E1000_INTR_THROTTLING_NS_RES;
217 
218     for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
219         core->eitr[i].core = core;
220         core->eitr[i].delay_reg = EITR + i;
221         core->eitr[i].delay_resolution_ns = E1000_INTR_THROTTLING_NS_RES;
222     }
223 
224     if (!create) {
225         return;
226     }
227 
228     core->radv.timer =
229         timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000e_intrmgr_on_timer, &core->radv);
230     core->rdtr.timer =
231         timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000e_intrmgr_on_timer, &core->rdtr);
232     core->raid.timer =
233         timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000e_intrmgr_on_timer, &core->raid);
234 
235     core->tadv.timer =
236         timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000e_intrmgr_on_timer, &core->tadv);
237     core->tidv.timer =
238         timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000e_intrmgr_on_timer, &core->tidv);
239 
240     core->itr.timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
241                                    e1000e_intrmgr_on_throttling_timer,
242                                    &core->itr);
243 
244     for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
245         core->eitr[i].timer =
246             timer_new_ns(QEMU_CLOCK_VIRTUAL,
247                          e1000e_intrmgr_on_msix_throttling_timer,
248                          &core->eitr[i]);
249     }
250 }
251 
252 static inline void
253 e1000e_intrmgr_stop_delay_timers(E1000ECore *core)
254 {
255     e1000e_intrmgr_stop_timer(&core->radv);
256     e1000e_intrmgr_stop_timer(&core->rdtr);
257     e1000e_intrmgr_stop_timer(&core->raid);
258     e1000e_intrmgr_stop_timer(&core->tidv);
259     e1000e_intrmgr_stop_timer(&core->tadv);
260 }
261 
262 static bool
263 e1000e_intrmgr_delay_rx_causes(E1000ECore *core, uint32_t *causes)
264 {
265     uint32_t delayable_causes;
266     uint32_t rdtr = core->mac[RDTR];
267     uint32_t radv = core->mac[RADV];
268     uint32_t raid = core->mac[RAID];
269 
270     if (msix_enabled(core->owner)) {
271         return false;
272     }
273 
274     delayable_causes = E1000_ICR_RXQ0 |
275                        E1000_ICR_RXQ1 |
276                        E1000_ICR_RXT0;
277 
278     if (!(core->mac[RFCTL] & E1000_RFCTL_ACK_DIS)) {
279         delayable_causes |= E1000_ICR_ACK;
280     }
281 
282     /* Clean up all causes that may be delayed */
283     core->delayed_causes |= *causes & delayable_causes;
284     *causes &= ~delayable_causes;
285 
286     /*
287      * Check if delayed RX interrupts disabled by client
288      * or if there are causes that cannot be delayed
289      */
290     if ((rdtr == 0) || (*causes != 0)) {
291         return false;
292     }
293 
294     /*
295      * Check if delayed RX ACK interrupts disabled by client
296      * and there is an ACK packet received
297      */
298     if ((raid == 0) && (core->delayed_causes & E1000_ICR_ACK)) {
299         return false;
300     }
301 
302     /* All causes delayed */
303     e1000e_intrmgr_rearm_timer(&core->rdtr);
304 
305     if (!core->radv.running && (radv != 0)) {
306         e1000e_intrmgr_rearm_timer(&core->radv);
307     }
308 
309     if (!core->raid.running && (core->delayed_causes & E1000_ICR_ACK)) {
310         e1000e_intrmgr_rearm_timer(&core->raid);
311     }
312 
313     return true;
314 }
315 
316 static bool
317 e1000e_intrmgr_delay_tx_causes(E1000ECore *core, uint32_t *causes)
318 {
319     static const uint32_t delayable_causes = E1000_ICR_TXQ0 |
320                                              E1000_ICR_TXQ1 |
321                                              E1000_ICR_TXQE |
322                                              E1000_ICR_TXDW;
323 
324     if (msix_enabled(core->owner)) {
325         return false;
326     }
327 
328     /* Clean up all causes that may be delayed */
329     core->delayed_causes |= *causes & delayable_causes;
330     *causes &= ~delayable_causes;
331 
332     /* If there are causes that cannot be delayed */
333     if (*causes != 0) {
334         return false;
335     }
336 
337     /* All causes delayed */
338     e1000e_intrmgr_rearm_timer(&core->tidv);
339 
340     if (!core->tadv.running && (core->mac[TADV] != 0)) {
341         e1000e_intrmgr_rearm_timer(&core->tadv);
342     }
343 
344     return true;
345 }
346 
347 static uint32_t
348 e1000e_intmgr_collect_delayed_causes(E1000ECore *core)
349 {
350     uint32_t res;
351 
352     if (msix_enabled(core->owner)) {
353         assert(core->delayed_causes == 0);
354         return 0;
355     }
356 
357     res = core->delayed_causes;
358     core->delayed_causes = 0;
359 
360     e1000e_intrmgr_stop_delay_timers(core);
361 
362     return res;
363 }
364 
365 static void
366 e1000e_intrmgr_fire_all_timers(E1000ECore *core)
367 {
368     int i;
369 
370     if (core->itr.running) {
371         timer_del(core->itr.timer);
372         e1000e_intrmgr_on_throttling_timer(&core->itr);
373     }
374 
375     for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
376         if (core->eitr[i].running) {
377             timer_del(core->eitr[i].timer);
378             e1000e_intrmgr_on_msix_throttling_timer(&core->eitr[i]);
379         }
380     }
381 }
382 
383 static void
384 e1000e_intrmgr_resume(E1000ECore *core)
385 {
386     int i;
387 
388     e1000e_intmgr_timer_resume(&core->radv);
389     e1000e_intmgr_timer_resume(&core->rdtr);
390     e1000e_intmgr_timer_resume(&core->raid);
391     e1000e_intmgr_timer_resume(&core->tidv);
392     e1000e_intmgr_timer_resume(&core->tadv);
393 
394     e1000e_intmgr_timer_resume(&core->itr);
395 
396     for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
397         e1000e_intmgr_timer_resume(&core->eitr[i]);
398     }
399 }
400 
401 static void
402 e1000e_intrmgr_pause(E1000ECore *core)
403 {
404     int i;
405 
406     e1000e_intmgr_timer_pause(&core->radv);
407     e1000e_intmgr_timer_pause(&core->rdtr);
408     e1000e_intmgr_timer_pause(&core->raid);
409     e1000e_intmgr_timer_pause(&core->tidv);
410     e1000e_intmgr_timer_pause(&core->tadv);
411 
412     e1000e_intmgr_timer_pause(&core->itr);
413 
414     for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
415         e1000e_intmgr_timer_pause(&core->eitr[i]);
416     }
417 }
418 
419 static void
420 e1000e_intrmgr_reset(E1000ECore *core)
421 {
422     int i;
423 
424     core->delayed_causes = 0;
425 
426     e1000e_intrmgr_stop_delay_timers(core);
427 
428     e1000e_intrmgr_stop_timer(&core->itr);
429 
430     for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
431         e1000e_intrmgr_stop_timer(&core->eitr[i]);
432     }
433 }
434 
435 static void
436 e1000e_intrmgr_pci_unint(E1000ECore *core)
437 {
438     int i;
439 
440     timer_free(core->radv.timer);
441     timer_free(core->rdtr.timer);
442     timer_free(core->raid.timer);
443 
444     timer_free(core->tadv.timer);
445     timer_free(core->tidv.timer);
446 
447     timer_free(core->itr.timer);
448 
449     for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
450         timer_free(core->eitr[i].timer);
451     }
452 }
453 
454 static void
455 e1000e_intrmgr_pci_realize(E1000ECore *core)
456 {
457     e1000e_intrmgr_initialize_all_timers(core, true);
458 }
459 
460 static inline bool
461 e1000e_rx_csum_enabled(E1000ECore *core)
462 {
463     return (core->mac[RXCSUM] & E1000_RXCSUM_PCSD) ? false : true;
464 }
465 
466 static inline bool
467 e1000e_rx_use_legacy_descriptor(E1000ECore *core)
468 {
469     return (core->mac[RFCTL] & E1000_RFCTL_EXTEN) ? false : true;
470 }
471 
472 static inline bool
473 e1000e_rx_use_ps_descriptor(E1000ECore *core)
474 {
475     return !e1000e_rx_use_legacy_descriptor(core) &&
476            (core->mac[RCTL] & E1000_RCTL_DTYP_PS);
477 }
478 
479 static inline bool
480 e1000e_rss_enabled(E1000ECore *core)
481 {
482     return E1000_MRQC_ENABLED(core->mac[MRQC]) &&
483            !e1000e_rx_csum_enabled(core) &&
484            !e1000e_rx_use_legacy_descriptor(core);
485 }
486 
487 typedef struct E1000E_RSSInfo_st {
488     bool enabled;
489     uint32_t hash;
490     uint32_t queue;
491     uint32_t type;
492 } E1000E_RSSInfo;
493 
494 static uint32_t
495 e1000e_rss_get_hash_type(E1000ECore *core, struct NetRxPkt *pkt)
496 {
497     bool hasip4, hasip6;
498     EthL4HdrProto l4hdr_proto;
499 
500     assert(e1000e_rss_enabled(core));
501 
502     net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto);
503 
504     if (hasip4) {
505         trace_e1000e_rx_rss_ip4(l4hdr_proto, core->mac[MRQC],
506                                 E1000_MRQC_EN_TCPIPV4(core->mac[MRQC]),
507                                 E1000_MRQC_EN_IPV4(core->mac[MRQC]));
508 
509         if (l4hdr_proto == ETH_L4_HDR_PROTO_TCP &&
510             E1000_MRQC_EN_TCPIPV4(core->mac[MRQC])) {
511             return E1000_MRQ_RSS_TYPE_IPV4TCP;
512         }
513 
514         if (E1000_MRQC_EN_IPV4(core->mac[MRQC])) {
515             return E1000_MRQ_RSS_TYPE_IPV4;
516         }
517     } else if (hasip6) {
518         eth_ip6_hdr_info *ip6info = net_rx_pkt_get_ip6_info(pkt);
519 
520         bool ex_dis = core->mac[RFCTL] & E1000_RFCTL_IPV6_EX_DIS;
521         bool new_ex_dis = core->mac[RFCTL] & E1000_RFCTL_NEW_IPV6_EXT_DIS;
522 
523         /*
524          * Following two traces must not be combined because resulting
525          * event will have 11 arguments totally and some trace backends
526          * (at least "ust") have limitation of maximum 10 arguments per
527          * event. Events with more arguments fail to compile for
528          * backends like these.
529          */
530         trace_e1000e_rx_rss_ip6_rfctl(core->mac[RFCTL]);
531         trace_e1000e_rx_rss_ip6(ex_dis, new_ex_dis, l4hdr_proto,
532                                 ip6info->has_ext_hdrs,
533                                 ip6info->rss_ex_dst_valid,
534                                 ip6info->rss_ex_src_valid,
535                                 core->mac[MRQC],
536                                 E1000_MRQC_EN_TCPIPV6EX(core->mac[MRQC]),
537                                 E1000_MRQC_EN_IPV6EX(core->mac[MRQC]),
538                                 E1000_MRQC_EN_IPV6(core->mac[MRQC]));
539 
540         if ((!ex_dis || !ip6info->has_ext_hdrs) &&
541             (!new_ex_dis || !(ip6info->rss_ex_dst_valid ||
542                               ip6info->rss_ex_src_valid))) {
543 
544             if (l4hdr_proto == ETH_L4_HDR_PROTO_TCP &&
545                 E1000_MRQC_EN_TCPIPV6EX(core->mac[MRQC])) {
546                 return E1000_MRQ_RSS_TYPE_IPV6TCPEX;
547             }
548 
549             if (E1000_MRQC_EN_IPV6EX(core->mac[MRQC])) {
550                 return E1000_MRQ_RSS_TYPE_IPV6EX;
551             }
552 
553         }
554 
555         if (E1000_MRQC_EN_IPV6(core->mac[MRQC])) {
556             return E1000_MRQ_RSS_TYPE_IPV6;
557         }
558 
559     }
560 
561     return E1000_MRQ_RSS_TYPE_NONE;
562 }
563 
564 static uint32_t
565 e1000e_rss_calc_hash(E1000ECore *core,
566                      struct NetRxPkt *pkt,
567                      E1000E_RSSInfo *info)
568 {
569     NetRxPktRssType type;
570 
571     assert(e1000e_rss_enabled(core));
572 
573     switch (info->type) {
574     case E1000_MRQ_RSS_TYPE_IPV4:
575         type = NetPktRssIpV4;
576         break;
577     case E1000_MRQ_RSS_TYPE_IPV4TCP:
578         type = NetPktRssIpV4Tcp;
579         break;
580     case E1000_MRQ_RSS_TYPE_IPV6TCPEX:
581         type = NetPktRssIpV6TcpEx;
582         break;
583     case E1000_MRQ_RSS_TYPE_IPV6:
584         type = NetPktRssIpV6;
585         break;
586     case E1000_MRQ_RSS_TYPE_IPV6EX:
587         type = NetPktRssIpV6Ex;
588         break;
589     default:
590         assert(false);
591         return 0;
592     }
593 
594     return net_rx_pkt_calc_rss_hash(pkt, type, (uint8_t *) &core->mac[RSSRK]);
595 }
596 
597 static void
598 e1000e_rss_parse_packet(E1000ECore *core,
599                         struct NetRxPkt *pkt,
600                         E1000E_RSSInfo *info)
601 {
602     trace_e1000e_rx_rss_started();
603 
604     if (!e1000e_rss_enabled(core)) {
605         info->enabled = false;
606         info->hash = 0;
607         info->queue = 0;
608         info->type = 0;
609         trace_e1000e_rx_rss_disabled();
610         return;
611     }
612 
613     info->enabled = true;
614 
615     info->type = e1000e_rss_get_hash_type(core, pkt);
616 
617     trace_e1000e_rx_rss_type(info->type);
618 
619     if (info->type == E1000_MRQ_RSS_TYPE_NONE) {
620         info->hash = 0;
621         info->queue = 0;
622         return;
623     }
624 
625     info->hash = e1000e_rss_calc_hash(core, pkt, info);
626     info->queue = E1000_RSS_QUEUE(&core->mac[RETA], info->hash);
627 }
628 
629 static bool
630 e1000e_setup_tx_offloads(E1000ECore *core, struct e1000e_tx *tx)
631 {
632     if (tx->props.tse && tx->cptse) {
633         if (!net_tx_pkt_build_vheader(tx->tx_pkt, true, true, tx->props.mss)) {
634             return false;
635         }
636 
637         net_tx_pkt_update_ip_checksums(tx->tx_pkt);
638         e1000x_inc_reg_if_not_full(core->mac, TSCTC);
639         return true;
640     }
641 
642     if (tx->sum_needed & E1000_TXD_POPTS_TXSM) {
643         if (!net_tx_pkt_build_vheader(tx->tx_pkt, false, true, 0)) {
644             return false;
645         }
646     }
647 
648     if (tx->sum_needed & E1000_TXD_POPTS_IXSM) {
649         net_tx_pkt_update_ip_hdr_checksum(tx->tx_pkt);
650     }
651 
652     return true;
653 }
654 
655 static void e1000e_tx_pkt_callback(void *core,
656                                    const struct iovec *iov,
657                                    int iovcnt,
658                                    const struct iovec *virt_iov,
659                                    int virt_iovcnt)
660 {
661     e1000e_receive_internal(core, virt_iov, virt_iovcnt, true);
662 }
663 
664 static bool
665 e1000e_tx_pkt_send(E1000ECore *core, struct e1000e_tx *tx, int queue_index)
666 {
667     int target_queue = MIN(core->max_queue_num, queue_index);
668     NetClientState *queue = qemu_get_subqueue(core->owner_nic, target_queue);
669 
670     if (!e1000e_setup_tx_offloads(core, tx)) {
671         return false;
672     }
673 
674     net_tx_pkt_dump(tx->tx_pkt);
675 
676     if ((core->phy[0][MII_BMCR] & MII_BMCR_LOOPBACK) ||
677         ((core->mac[RCTL] & E1000_RCTL_LBM_MAC) == E1000_RCTL_LBM_MAC)) {
678         return net_tx_pkt_send_custom(tx->tx_pkt, false,
679                                       e1000e_tx_pkt_callback, core);
680     } else {
681         return net_tx_pkt_send(tx->tx_pkt, queue);
682     }
683 }
684 
685 static void
686 e1000e_on_tx_done_update_stats(E1000ECore *core, struct NetTxPkt *tx_pkt)
687 {
688     static const int PTCregs[6] = { PTC64, PTC127, PTC255, PTC511,
689                                     PTC1023, PTC1522 };
690 
691     size_t tot_len = net_tx_pkt_get_total_len(tx_pkt) + 4;
692 
693     e1000x_increase_size_stats(core->mac, PTCregs, tot_len);
694     e1000x_inc_reg_if_not_full(core->mac, TPT);
695     e1000x_grow_8reg_if_not_full(core->mac, TOTL, tot_len);
696 
697     switch (net_tx_pkt_get_packet_type(tx_pkt)) {
698     case ETH_PKT_BCAST:
699         e1000x_inc_reg_if_not_full(core->mac, BPTC);
700         break;
701     case ETH_PKT_MCAST:
702         e1000x_inc_reg_if_not_full(core->mac, MPTC);
703         break;
704     case ETH_PKT_UCAST:
705         break;
706     default:
707         g_assert_not_reached();
708     }
709 
710     e1000x_inc_reg_if_not_full(core->mac, GPTC);
711     e1000x_grow_8reg_if_not_full(core->mac, GOTCL, tot_len);
712 }
713 
714 static void
715 e1000e_process_tx_desc(E1000ECore *core,
716                        struct e1000e_tx *tx,
717                        struct e1000_tx_desc *dp,
718                        int queue_index)
719 {
720     uint32_t txd_lower = le32_to_cpu(dp->lower.data);
721     uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D);
722     unsigned int split_size = txd_lower & 0xffff;
723     uint64_t addr;
724     struct e1000_context_desc *xp = (struct e1000_context_desc *)dp;
725     bool eop = txd_lower & E1000_TXD_CMD_EOP;
726 
727     if (dtype == E1000_TXD_CMD_DEXT) { /* context descriptor */
728         e1000x_read_tx_ctx_descr(xp, &tx->props);
729         e1000e_process_snap_option(core, le32_to_cpu(xp->cmd_and_length));
730         return;
731     } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) {
732         /* data descriptor */
733         tx->sum_needed = le32_to_cpu(dp->upper.data) >> 8;
734         tx->cptse = (txd_lower & E1000_TXD_CMD_TSE) ? 1 : 0;
735         e1000e_process_ts_option(core, dp);
736     } else {
737         /* legacy descriptor */
738         e1000e_process_ts_option(core, dp);
739         tx->cptse = 0;
740     }
741 
742     addr = le64_to_cpu(dp->buffer_addr);
743 
744     if (!tx->skip_cp) {
745         if (!net_tx_pkt_add_raw_fragment_pci(tx->tx_pkt, core->owner,
746                                              addr, split_size)) {
747             tx->skip_cp = true;
748         }
749     }
750 
751     if (eop) {
752         if (!tx->skip_cp && net_tx_pkt_parse(tx->tx_pkt)) {
753             if (e1000x_vlan_enabled(core->mac) &&
754                 e1000x_is_vlan_txd(txd_lower)) {
755                 net_tx_pkt_setup_vlan_header_ex(tx->tx_pkt,
756                     le16_to_cpu(dp->upper.fields.special), core->mac[VET]);
757             }
758             if (e1000e_tx_pkt_send(core, tx, queue_index)) {
759                 e1000e_on_tx_done_update_stats(core, tx->tx_pkt);
760             }
761         }
762 
763         tx->skip_cp = false;
764         net_tx_pkt_reset(tx->tx_pkt, net_tx_pkt_unmap_frag_pci, core->owner);
765 
766         tx->sum_needed = 0;
767         tx->cptse = 0;
768     }
769 }
770 
771 static inline uint32_t
772 e1000e_tx_wb_interrupt_cause(E1000ECore *core, int queue_idx)
773 {
774     if (!msix_enabled(core->owner)) {
775         return E1000_ICR_TXDW;
776     }
777 
778     return (queue_idx == 0) ? E1000_ICR_TXQ0 : E1000_ICR_TXQ1;
779 }
780 
781 static inline uint32_t
782 e1000e_rx_wb_interrupt_cause(E1000ECore *core, int queue_idx,
783                              bool min_threshold_hit)
784 {
785     if (!msix_enabled(core->owner)) {
786         return E1000_ICS_RXT0 | (min_threshold_hit ? E1000_ICS_RXDMT0 : 0);
787     }
788 
789     return (queue_idx == 0) ? E1000_ICR_RXQ0 : E1000_ICR_RXQ1;
790 }
791 
792 static uint32_t
793 e1000e_txdesc_writeback(E1000ECore *core, dma_addr_t base,
794                         struct e1000_tx_desc *dp, bool *ide, int queue_idx)
795 {
796     uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data);
797 
798     if (!(txd_lower & E1000_TXD_CMD_RS) &&
799         !(core->mac[IVAR] & E1000_IVAR_TX_INT_EVERY_WB)) {
800         return 0;
801     }
802 
803     *ide = (txd_lower & E1000_TXD_CMD_IDE) ? true : false;
804 
805     txd_upper = le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD;
806 
807     dp->upper.data = cpu_to_le32(txd_upper);
808     pci_dma_write(core->owner, base + ((char *)&dp->upper - (char *)dp),
809                   &dp->upper, sizeof(dp->upper));
810     return e1000e_tx_wb_interrupt_cause(core, queue_idx);
811 }
812 
813 typedef struct E1000ERingInfo {
814     int dbah;
815     int dbal;
816     int dlen;
817     int dh;
818     int dt;
819     int idx;
820 } E1000ERingInfo;
821 
822 static inline bool
823 e1000e_ring_empty(E1000ECore *core, const E1000ERingInfo *r)
824 {
825     return core->mac[r->dh] == core->mac[r->dt] ||
826                 core->mac[r->dt] >= core->mac[r->dlen] / E1000_RING_DESC_LEN;
827 }
828 
829 static inline uint64_t
830 e1000e_ring_base(E1000ECore *core, const E1000ERingInfo *r)
831 {
832     uint64_t bah = core->mac[r->dbah];
833     uint64_t bal = core->mac[r->dbal];
834 
835     return (bah << 32) + bal;
836 }
837 
838 static inline uint64_t
839 e1000e_ring_head_descr(E1000ECore *core, const E1000ERingInfo *r)
840 {
841     return e1000e_ring_base(core, r) + E1000_RING_DESC_LEN * core->mac[r->dh];
842 }
843 
844 static inline void
845 e1000e_ring_advance(E1000ECore *core, const E1000ERingInfo *r, uint32_t count)
846 {
847     core->mac[r->dh] += count;
848 
849     if (core->mac[r->dh] * E1000_RING_DESC_LEN >= core->mac[r->dlen]) {
850         core->mac[r->dh] = 0;
851     }
852 }
853 
854 static inline uint32_t
855 e1000e_ring_free_descr_num(E1000ECore *core, const E1000ERingInfo *r)
856 {
857     trace_e1000e_ring_free_space(r->idx, core->mac[r->dlen],
858                                  core->mac[r->dh],  core->mac[r->dt]);
859 
860     if (core->mac[r->dh] <= core->mac[r->dt]) {
861         return core->mac[r->dt] - core->mac[r->dh];
862     }
863 
864     if (core->mac[r->dh] > core->mac[r->dt]) {
865         return core->mac[r->dlen] / E1000_RING_DESC_LEN +
866                core->mac[r->dt] - core->mac[r->dh];
867     }
868 
869     g_assert_not_reached();
870     return 0;
871 }
872 
873 static inline bool
874 e1000e_ring_enabled(E1000ECore *core, const E1000ERingInfo *r)
875 {
876     return core->mac[r->dlen] > 0;
877 }
878 
879 static inline uint32_t
880 e1000e_ring_len(E1000ECore *core, const E1000ERingInfo *r)
881 {
882     return core->mac[r->dlen];
883 }
884 
885 typedef struct E1000E_TxRing_st {
886     const E1000ERingInfo *i;
887     struct e1000e_tx *tx;
888 } E1000E_TxRing;
889 
890 static inline int
891 e1000e_mq_queue_idx(int base_reg_idx, int reg_idx)
892 {
893     return (reg_idx - base_reg_idx) / (0x100 >> 2);
894 }
895 
896 static inline void
897 e1000e_tx_ring_init(E1000ECore *core, E1000E_TxRing *txr, int idx)
898 {
899     static const E1000ERingInfo i[E1000E_NUM_QUEUES] = {
900         { TDBAH,  TDBAL,  TDLEN,  TDH,  TDT, 0 },
901         { TDBAH1, TDBAL1, TDLEN1, TDH1, TDT1, 1 }
902     };
903 
904     assert(idx < ARRAY_SIZE(i));
905 
906     txr->i     = &i[idx];
907     txr->tx    = &core->tx[idx];
908 }
909 
910 typedef struct E1000E_RxRing_st {
911     const E1000ERingInfo *i;
912 } E1000E_RxRing;
913 
914 static inline void
915 e1000e_rx_ring_init(E1000ECore *core, E1000E_RxRing *rxr, int idx)
916 {
917     static const E1000ERingInfo i[E1000E_NUM_QUEUES] = {
918         { RDBAH0, RDBAL0, RDLEN0, RDH0, RDT0, 0 },
919         { RDBAH1, RDBAL1, RDLEN1, RDH1, RDT1, 1 }
920     };
921 
922     assert(idx < ARRAY_SIZE(i));
923 
924     rxr->i      = &i[idx];
925 }
926 
927 static void
928 e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr)
929 {
930     dma_addr_t base;
931     struct e1000_tx_desc desc;
932     bool ide = false;
933     const E1000ERingInfo *txi = txr->i;
934     uint32_t cause = E1000_ICS_TXQE;
935 
936     if (!(core->mac[TCTL] & E1000_TCTL_EN)) {
937         trace_e1000e_tx_disabled();
938         return;
939     }
940 
941     while (!e1000e_ring_empty(core, txi)) {
942         base = e1000e_ring_head_descr(core, txi);
943 
944         pci_dma_read(core->owner, base, &desc, sizeof(desc));
945 
946         trace_e1000e_tx_descr((void *)(intptr_t)desc.buffer_addr,
947                               desc.lower.data, desc.upper.data);
948 
949         e1000e_process_tx_desc(core, txr->tx, &desc, txi->idx);
950         cause |= e1000e_txdesc_writeback(core, base, &desc, &ide, txi->idx);
951 
952         e1000e_ring_advance(core, txi, 1);
953     }
954 
955     if (!ide || !e1000e_intrmgr_delay_tx_causes(core, &cause)) {
956         e1000e_set_interrupt_cause(core, cause);
957     }
958 
959     net_tx_pkt_reset(txr->tx->tx_pkt, net_tx_pkt_unmap_frag_pci, core->owner);
960 }
961 
962 static bool
963 e1000e_has_rxbufs(E1000ECore *core, const E1000ERingInfo *r,
964                   size_t total_size)
965 {
966     uint32_t bufs = e1000e_ring_free_descr_num(core, r);
967 
968     trace_e1000e_rx_has_buffers(r->idx, bufs, total_size,
969                                 core->rx_desc_buf_size);
970 
971     return total_size <= bufs / (core->rx_desc_len / E1000_MIN_RX_DESC_LEN) *
972                          core->rx_desc_buf_size;
973 }
974 
975 void
976 e1000e_start_recv(E1000ECore *core)
977 {
978     int i;
979 
980     trace_e1000e_rx_start_recv();
981 
982     for (i = 0; i <= core->max_queue_num; i++) {
983         qemu_flush_queued_packets(qemu_get_subqueue(core->owner_nic, i));
984     }
985 }
986 
987 bool
988 e1000e_can_receive(E1000ECore *core)
989 {
990     int i;
991 
992     if (!e1000x_rx_ready(core->owner, core->mac)) {
993         return false;
994     }
995 
996     for (i = 0; i < E1000E_NUM_QUEUES; i++) {
997         E1000E_RxRing rxr;
998 
999         e1000e_rx_ring_init(core, &rxr, i);
1000         if (e1000e_ring_enabled(core, rxr.i) &&
1001             e1000e_has_rxbufs(core, rxr.i, 1)) {
1002             trace_e1000e_rx_can_recv();
1003             return true;
1004         }
1005     }
1006 
1007     trace_e1000e_rx_can_recv_rings_full();
1008     return false;
1009 }
1010 
1011 ssize_t
1012 e1000e_receive(E1000ECore *core, const uint8_t *buf, size_t size)
1013 {
1014     const struct iovec iov = {
1015         .iov_base = (uint8_t *)buf,
1016         .iov_len = size
1017     };
1018 
1019     return e1000e_receive_iov(core, &iov, 1);
1020 }
1021 
1022 static inline bool
1023 e1000e_rx_l3_cso_enabled(E1000ECore *core)
1024 {
1025     return !!(core->mac[RXCSUM] & E1000_RXCSUM_IPOFLD);
1026 }
1027 
1028 static inline bool
1029 e1000e_rx_l4_cso_enabled(E1000ECore *core)
1030 {
1031     return !!(core->mac[RXCSUM] & E1000_RXCSUM_TUOFLD);
1032 }
1033 
1034 static bool
1035 e1000e_receive_filter(E1000ECore *core, const void *buf)
1036 {
1037     return (!e1000x_is_vlan_packet(buf, core->mac[VET]) ||
1038             e1000x_rx_vlan_filter(core->mac, PKT_GET_VLAN_HDR(buf))) &&
1039            e1000x_rx_group_filter(core->mac, buf);
1040 }
1041 
1042 static inline void
1043 e1000e_read_lgcy_rx_descr(E1000ECore *core, struct e1000_rx_desc *desc,
1044                           hwaddr *buff_addr)
1045 {
1046     *buff_addr = le64_to_cpu(desc->buffer_addr);
1047 }
1048 
1049 static inline void
1050 e1000e_read_ext_rx_descr(E1000ECore *core, union e1000_rx_desc_extended *desc,
1051                          hwaddr *buff_addr)
1052 {
1053     *buff_addr = le64_to_cpu(desc->read.buffer_addr);
1054 }
1055 
1056 static inline void
1057 e1000e_read_ps_rx_descr(E1000ECore *core,
1058                         union e1000_rx_desc_packet_split *desc,
1059                         hwaddr buff_addr[MAX_PS_BUFFERS])
1060 {
1061     int i;
1062 
1063     for (i = 0; i < MAX_PS_BUFFERS; i++) {
1064         buff_addr[i] = le64_to_cpu(desc->read.buffer_addr[i]);
1065     }
1066 
1067     trace_e1000e_rx_desc_ps_read(buff_addr[0], buff_addr[1],
1068                                  buff_addr[2], buff_addr[3]);
1069 }
1070 
1071 static inline void
1072 e1000e_read_rx_descr(E1000ECore *core, union e1000_rx_desc_union *desc,
1073                      hwaddr buff_addr[MAX_PS_BUFFERS])
1074 {
1075     if (e1000e_rx_use_legacy_descriptor(core)) {
1076         e1000e_read_lgcy_rx_descr(core, &desc->legacy, &buff_addr[0]);
1077         buff_addr[1] = buff_addr[2] = buff_addr[3] = 0;
1078     } else {
1079         if (core->mac[RCTL] & E1000_RCTL_DTYP_PS) {
1080             e1000e_read_ps_rx_descr(core, &desc->packet_split, buff_addr);
1081         } else {
1082             e1000e_read_ext_rx_descr(core, &desc->extended, &buff_addr[0]);
1083             buff_addr[1] = buff_addr[2] = buff_addr[3] = 0;
1084         }
1085     }
1086 }
1087 
1088 static void
1089 e1000e_verify_csum_in_sw(E1000ECore *core,
1090                          struct NetRxPkt *pkt,
1091                          uint32_t *status_flags,
1092                          EthL4HdrProto l4hdr_proto)
1093 {
1094     bool csum_valid;
1095     uint32_t csum_error;
1096 
1097     if (e1000e_rx_l3_cso_enabled(core)) {
1098         if (!net_rx_pkt_validate_l3_csum(pkt, &csum_valid)) {
1099             trace_e1000e_rx_metadata_l3_csum_validation_failed();
1100         } else {
1101             csum_error = csum_valid ? 0 : E1000_RXDEXT_STATERR_IPE;
1102             *status_flags |= E1000_RXD_STAT_IPCS | csum_error;
1103         }
1104     } else {
1105         trace_e1000e_rx_metadata_l3_cso_disabled();
1106     }
1107 
1108     if (!e1000e_rx_l4_cso_enabled(core)) {
1109         trace_e1000e_rx_metadata_l4_cso_disabled();
1110         return;
1111     }
1112 
1113     if (l4hdr_proto != ETH_L4_HDR_PROTO_TCP &&
1114         l4hdr_proto != ETH_L4_HDR_PROTO_UDP) {
1115         return;
1116     }
1117 
1118     if (!net_rx_pkt_validate_l4_csum(pkt, &csum_valid)) {
1119         trace_e1000e_rx_metadata_l4_csum_validation_failed();
1120         return;
1121     }
1122 
1123     csum_error = csum_valid ? 0 : E1000_RXDEXT_STATERR_TCPE;
1124     *status_flags |= E1000_RXD_STAT_TCPCS | csum_error;
1125 
1126     if (l4hdr_proto == ETH_L4_HDR_PROTO_UDP) {
1127         *status_flags |= E1000_RXD_STAT_UDPCS;
1128     }
1129 }
1130 
1131 static inline bool
1132 e1000e_is_tcp_ack(E1000ECore *core, struct NetRxPkt *rx_pkt)
1133 {
1134     if (!net_rx_pkt_is_tcp_ack(rx_pkt)) {
1135         return false;
1136     }
1137 
1138     if (core->mac[RFCTL] & E1000_RFCTL_ACK_DATA_DIS) {
1139         return !net_rx_pkt_has_tcp_data(rx_pkt);
1140     }
1141 
1142     return true;
1143 }
1144 
1145 static void
1146 e1000e_build_rx_metadata(E1000ECore *core,
1147                          struct NetRxPkt *pkt,
1148                          bool is_eop,
1149                          const E1000E_RSSInfo *rss_info,
1150                          uint32_t *rss, uint32_t *mrq,
1151                          uint32_t *status_flags,
1152                          uint16_t *ip_id,
1153                          uint16_t *vlan_tag)
1154 {
1155     struct virtio_net_hdr *vhdr;
1156     bool hasip4, hasip6;
1157     EthL4HdrProto l4hdr_proto;
1158     uint32_t pkt_type;
1159 
1160     *status_flags = E1000_RXD_STAT_DD;
1161 
1162     /* No additional metadata needed for non-EOP descriptors */
1163     if (!is_eop) {
1164         goto func_exit;
1165     }
1166 
1167     *status_flags |= E1000_RXD_STAT_EOP;
1168 
1169     net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto);
1170     trace_e1000e_rx_metadata_protocols(hasip4, hasip6, l4hdr_proto);
1171 
1172     /* VLAN state */
1173     if (net_rx_pkt_is_vlan_stripped(pkt)) {
1174         *status_flags |= E1000_RXD_STAT_VP;
1175         *vlan_tag = cpu_to_le16(net_rx_pkt_get_vlan_tag(pkt));
1176         trace_e1000e_rx_metadata_vlan(*vlan_tag);
1177     }
1178 
1179     /* Packet parsing results */
1180     if ((core->mac[RXCSUM] & E1000_RXCSUM_PCSD) != 0) {
1181         if (rss_info->enabled) {
1182             *rss = cpu_to_le32(rss_info->hash);
1183             *mrq = cpu_to_le32(rss_info->type | (rss_info->queue << 8));
1184             trace_e1000e_rx_metadata_rss(*rss, *mrq);
1185         }
1186     } else if (hasip4) {
1187             *status_flags |= E1000_RXD_STAT_IPIDV;
1188             *ip_id = cpu_to_le16(net_rx_pkt_get_ip_id(pkt));
1189             trace_e1000e_rx_metadata_ip_id(*ip_id);
1190     }
1191 
1192     if (l4hdr_proto == ETH_L4_HDR_PROTO_TCP && e1000e_is_tcp_ack(core, pkt)) {
1193         *status_flags |= E1000_RXD_STAT_ACK;
1194         trace_e1000e_rx_metadata_ack();
1195     }
1196 
1197     if (hasip6 && (core->mac[RFCTL] & E1000_RFCTL_IPV6_DIS)) {
1198         trace_e1000e_rx_metadata_ipv6_filtering_disabled();
1199         pkt_type = E1000_RXD_PKT_MAC;
1200     } else if (l4hdr_proto == ETH_L4_HDR_PROTO_TCP ||
1201                l4hdr_proto == ETH_L4_HDR_PROTO_UDP) {
1202         pkt_type = hasip4 ? E1000_RXD_PKT_IP4_XDP : E1000_RXD_PKT_IP6_XDP;
1203     } else if (hasip4 || hasip6) {
1204         pkt_type = hasip4 ? E1000_RXD_PKT_IP4 : E1000_RXD_PKT_IP6;
1205     } else {
1206         pkt_type = E1000_RXD_PKT_MAC;
1207     }
1208 
1209     *status_flags |= E1000_RXD_PKT_TYPE(pkt_type);
1210     trace_e1000e_rx_metadata_pkt_type(pkt_type);
1211 
1212     /* RX CSO information */
1213     if (hasip6 && (core->mac[RFCTL] & E1000_RFCTL_IPV6_XSUM_DIS)) {
1214         trace_e1000e_rx_metadata_ipv6_sum_disabled();
1215         goto func_exit;
1216     }
1217 
1218     vhdr = net_rx_pkt_get_vhdr(pkt);
1219 
1220     if (!(vhdr->flags & VIRTIO_NET_HDR_F_DATA_VALID) &&
1221         !(vhdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)) {
1222         trace_e1000e_rx_metadata_virthdr_no_csum_info();
1223         e1000e_verify_csum_in_sw(core, pkt, status_flags, l4hdr_proto);
1224         goto func_exit;
1225     }
1226 
1227     if (e1000e_rx_l3_cso_enabled(core)) {
1228         *status_flags |= hasip4 ? E1000_RXD_STAT_IPCS : 0;
1229     } else {
1230         trace_e1000e_rx_metadata_l3_cso_disabled();
1231     }
1232 
1233     if (e1000e_rx_l4_cso_enabled(core)) {
1234         switch (l4hdr_proto) {
1235         case ETH_L4_HDR_PROTO_TCP:
1236             *status_flags |= E1000_RXD_STAT_TCPCS;
1237             break;
1238 
1239         case ETH_L4_HDR_PROTO_UDP:
1240             *status_flags |= E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS;
1241             break;
1242 
1243         default:
1244             break;
1245         }
1246     } else {
1247         trace_e1000e_rx_metadata_l4_cso_disabled();
1248     }
1249 
1250 func_exit:
1251     trace_e1000e_rx_metadata_status_flags(*status_flags);
1252     *status_flags = cpu_to_le32(*status_flags);
1253 }
1254 
1255 static inline void
1256 e1000e_write_lgcy_rx_descr(E1000ECore *core, struct e1000_rx_desc *desc,
1257                            struct NetRxPkt *pkt,
1258                            const E1000E_RSSInfo *rss_info,
1259                            uint16_t length)
1260 {
1261     uint32_t status_flags, rss, mrq;
1262     uint16_t ip_id;
1263 
1264     assert(!rss_info->enabled);
1265 
1266     desc->length = cpu_to_le16(length);
1267     desc->csum = 0;
1268 
1269     e1000e_build_rx_metadata(core, pkt, pkt != NULL,
1270                              rss_info,
1271                              &rss, &mrq,
1272                              &status_flags, &ip_id,
1273                              &desc->special);
1274     desc->errors = (uint8_t) (le32_to_cpu(status_flags) >> 24);
1275     desc->status = (uint8_t) le32_to_cpu(status_flags);
1276 }
1277 
1278 static inline void
1279 e1000e_write_ext_rx_descr(E1000ECore *core, union e1000_rx_desc_extended *desc,
1280                           struct NetRxPkt *pkt,
1281                           const E1000E_RSSInfo *rss_info,
1282                           uint16_t length)
1283 {
1284     memset(&desc->wb, 0, sizeof(desc->wb));
1285 
1286     desc->wb.upper.length = cpu_to_le16(length);
1287 
1288     e1000e_build_rx_metadata(core, pkt, pkt != NULL,
1289                              rss_info,
1290                              &desc->wb.lower.hi_dword.rss,
1291                              &desc->wb.lower.mrq,
1292                              &desc->wb.upper.status_error,
1293                              &desc->wb.lower.hi_dword.csum_ip.ip_id,
1294                              &desc->wb.upper.vlan);
1295 }
1296 
1297 static inline void
1298 e1000e_write_ps_rx_descr(E1000ECore *core,
1299                          union e1000_rx_desc_packet_split *desc,
1300                          struct NetRxPkt *pkt,
1301                          const E1000E_RSSInfo *rss_info,
1302                          size_t ps_hdr_len,
1303                          uint16_t(*written)[MAX_PS_BUFFERS])
1304 {
1305     int i;
1306 
1307     memset(&desc->wb, 0, sizeof(desc->wb));
1308 
1309     desc->wb.middle.length0 = cpu_to_le16((*written)[0]);
1310 
1311     for (i = 0; i < PS_PAGE_BUFFERS; i++) {
1312         desc->wb.upper.length[i] = cpu_to_le16((*written)[i + 1]);
1313     }
1314 
1315     e1000e_build_rx_metadata(core, pkt, pkt != NULL,
1316                              rss_info,
1317                              &desc->wb.lower.hi_dword.rss,
1318                              &desc->wb.lower.mrq,
1319                              &desc->wb.middle.status_error,
1320                              &desc->wb.lower.hi_dword.csum_ip.ip_id,
1321                              &desc->wb.middle.vlan);
1322 
1323     desc->wb.upper.header_status =
1324         cpu_to_le16(ps_hdr_len | (ps_hdr_len ? E1000_RXDPS_HDRSTAT_HDRSP : 0));
1325 
1326     trace_e1000e_rx_desc_ps_write((*written)[0], (*written)[1],
1327                                   (*written)[2], (*written)[3]);
1328 }
1329 
1330 static inline void
1331 e1000e_write_rx_descr(E1000ECore *core, union e1000_rx_desc_union *desc,
1332 struct NetRxPkt *pkt, const E1000E_RSSInfo *rss_info,
1333     size_t ps_hdr_len, uint16_t(*written)[MAX_PS_BUFFERS])
1334 {
1335     if (e1000e_rx_use_legacy_descriptor(core)) {
1336         assert(ps_hdr_len == 0);
1337         e1000e_write_lgcy_rx_descr(core, &desc->legacy, pkt, rss_info,
1338                                    (*written)[0]);
1339     } else {
1340         if (core->mac[RCTL] & E1000_RCTL_DTYP_PS) {
1341             e1000e_write_ps_rx_descr(core, &desc->packet_split, pkt, rss_info,
1342                                       ps_hdr_len, written);
1343         } else {
1344             assert(ps_hdr_len == 0);
1345             e1000e_write_ext_rx_descr(core, &desc->extended, pkt, rss_info,
1346                                        (*written)[0]);
1347         }
1348     }
1349 }
1350 
1351 static inline void
1352 e1000e_pci_dma_write_rx_desc(E1000ECore *core, dma_addr_t addr,
1353                              union e1000_rx_desc_union *desc, dma_addr_t len)
1354 {
1355     PCIDevice *dev = core->owner;
1356 
1357     if (e1000e_rx_use_legacy_descriptor(core)) {
1358         struct e1000_rx_desc *d = &desc->legacy;
1359         size_t offset = offsetof(struct e1000_rx_desc, status);
1360         uint8_t status = d->status;
1361 
1362         d->status &= ~E1000_RXD_STAT_DD;
1363         pci_dma_write(dev, addr, desc, len);
1364 
1365         if (status & E1000_RXD_STAT_DD) {
1366             d->status = status;
1367             pci_dma_write(dev, addr + offset, &status, sizeof(status));
1368         }
1369     } else {
1370         if (core->mac[RCTL] & E1000_RCTL_DTYP_PS) {
1371             union e1000_rx_desc_packet_split *d = &desc->packet_split;
1372             size_t offset = offsetof(union e1000_rx_desc_packet_split,
1373                 wb.middle.status_error);
1374             uint32_t status = d->wb.middle.status_error;
1375 
1376             d->wb.middle.status_error &= ~E1000_RXD_STAT_DD;
1377             pci_dma_write(dev, addr, desc, len);
1378 
1379             if (status & E1000_RXD_STAT_DD) {
1380                 d->wb.middle.status_error = status;
1381                 pci_dma_write(dev, addr + offset, &status, sizeof(status));
1382             }
1383         } else {
1384             union e1000_rx_desc_extended *d = &desc->extended;
1385             size_t offset = offsetof(union e1000_rx_desc_extended,
1386                 wb.upper.status_error);
1387             uint32_t status = d->wb.upper.status_error;
1388 
1389             d->wb.upper.status_error &= ~E1000_RXD_STAT_DD;
1390             pci_dma_write(dev, addr, desc, len);
1391 
1392             if (status & E1000_RXD_STAT_DD) {
1393                 d->wb.upper.status_error = status;
1394                 pci_dma_write(dev, addr + offset, &status, sizeof(status));
1395             }
1396         }
1397     }
1398 }
1399 
1400 typedef struct e1000e_ba_state_st {
1401     uint16_t written[MAX_PS_BUFFERS];
1402     uint8_t cur_idx;
1403 } e1000e_ba_state;
1404 
1405 static inline void
1406 e1000e_write_hdr_to_rx_buffers(E1000ECore *core,
1407                                hwaddr ba[MAX_PS_BUFFERS],
1408                                e1000e_ba_state *bastate,
1409                                const char *data,
1410                                dma_addr_t data_len)
1411 {
1412     assert(data_len <= core->rxbuf_sizes[0] - bastate->written[0]);
1413 
1414     pci_dma_write(core->owner, ba[0] + bastate->written[0], data, data_len);
1415     bastate->written[0] += data_len;
1416 
1417     bastate->cur_idx = 1;
1418 }
1419 
1420 static void
1421 e1000e_write_payload_frag_to_rx_buffers(E1000ECore *core,
1422                                         hwaddr ba[MAX_PS_BUFFERS],
1423                                         e1000e_ba_state *bastate,
1424                                         const char *data,
1425                                         dma_addr_t data_len)
1426 {
1427     while (data_len > 0) {
1428         uint32_t cur_buf_len = core->rxbuf_sizes[bastate->cur_idx];
1429         uint32_t cur_buf_bytes_left = cur_buf_len -
1430                                       bastate->written[bastate->cur_idx];
1431         uint32_t bytes_to_write = MIN(data_len, cur_buf_bytes_left);
1432 
1433         trace_e1000e_rx_desc_buff_write(bastate->cur_idx,
1434                                         ba[bastate->cur_idx],
1435                                         bastate->written[bastate->cur_idx],
1436                                         data,
1437                                         bytes_to_write);
1438 
1439         pci_dma_write(core->owner,
1440             ba[bastate->cur_idx] + bastate->written[bastate->cur_idx],
1441             data, bytes_to_write);
1442 
1443         bastate->written[bastate->cur_idx] += bytes_to_write;
1444         data += bytes_to_write;
1445         data_len -= bytes_to_write;
1446 
1447         if (bastate->written[bastate->cur_idx] == cur_buf_len) {
1448             bastate->cur_idx++;
1449         }
1450 
1451         assert(bastate->cur_idx < MAX_PS_BUFFERS);
1452     }
1453 }
1454 
1455 static void
1456 e1000e_update_rx_stats(E1000ECore *core, size_t pkt_size, size_t pkt_fcs_size)
1457 {
1458     eth_pkt_types_e pkt_type = net_rx_pkt_get_packet_type(core->rx_pkt);
1459     e1000x_update_rx_total_stats(core->mac, pkt_type, pkt_size, pkt_fcs_size);
1460 }
1461 
1462 static inline bool
1463 e1000e_rx_descr_threshold_hit(E1000ECore *core, const E1000ERingInfo *rxi)
1464 {
1465     return e1000e_ring_free_descr_num(core, rxi) ==
1466            e1000e_ring_len(core, rxi) >> core->rxbuf_min_shift;
1467 }
1468 
1469 static bool
1470 e1000e_do_ps(E1000ECore *core, struct NetRxPkt *pkt, size_t *hdr_len)
1471 {
1472     bool hasip4, hasip6;
1473     EthL4HdrProto l4hdr_proto;
1474     bool fragment;
1475 
1476     if (!e1000e_rx_use_ps_descriptor(core)) {
1477         return false;
1478     }
1479 
1480     net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto);
1481 
1482     if (hasip4) {
1483         fragment = net_rx_pkt_get_ip4_info(pkt)->fragment;
1484     } else if (hasip6) {
1485         fragment = net_rx_pkt_get_ip6_info(pkt)->fragment;
1486     } else {
1487         return false;
1488     }
1489 
1490     if (fragment && (core->mac[RFCTL] & E1000_RFCTL_IPFRSP_DIS)) {
1491         return false;
1492     }
1493 
1494     if (l4hdr_proto == ETH_L4_HDR_PROTO_TCP ||
1495         l4hdr_proto == ETH_L4_HDR_PROTO_UDP) {
1496         *hdr_len = net_rx_pkt_get_l5_hdr_offset(pkt);
1497     } else {
1498         *hdr_len = net_rx_pkt_get_l4_hdr_offset(pkt);
1499     }
1500 
1501     if ((*hdr_len > core->rxbuf_sizes[0]) ||
1502         (*hdr_len > net_rx_pkt_get_total_len(pkt))) {
1503         return false;
1504     }
1505 
1506     return true;
1507 }
1508 
1509 static void
1510 e1000e_write_packet_to_guest(E1000ECore *core, struct NetRxPkt *pkt,
1511                              const E1000E_RxRing *rxr,
1512                              const E1000E_RSSInfo *rss_info)
1513 {
1514     PCIDevice *d = core->owner;
1515     dma_addr_t base;
1516     union e1000_rx_desc_union desc;
1517     size_t desc_size;
1518     size_t desc_offset = 0;
1519     size_t iov_ofs = 0;
1520 
1521     struct iovec *iov = net_rx_pkt_get_iovec(pkt);
1522     size_t size = net_rx_pkt_get_total_len(pkt);
1523     size_t total_size = size + e1000x_fcs_len(core->mac);
1524     const E1000ERingInfo *rxi;
1525     size_t ps_hdr_len = 0;
1526     bool do_ps = e1000e_do_ps(core, pkt, &ps_hdr_len);
1527     bool is_first = true;
1528 
1529     rxi = rxr->i;
1530 
1531     do {
1532         hwaddr ba[MAX_PS_BUFFERS];
1533         e1000e_ba_state bastate = { { 0 } };
1534         bool is_last = false;
1535 
1536         desc_size = total_size - desc_offset;
1537 
1538         if (desc_size > core->rx_desc_buf_size) {
1539             desc_size = core->rx_desc_buf_size;
1540         }
1541 
1542         if (e1000e_ring_empty(core, rxi)) {
1543             return;
1544         }
1545 
1546         base = e1000e_ring_head_descr(core, rxi);
1547 
1548         pci_dma_read(d, base, &desc, core->rx_desc_len);
1549 
1550         trace_e1000e_rx_descr(rxi->idx, base, core->rx_desc_len);
1551 
1552         e1000e_read_rx_descr(core, &desc, ba);
1553 
1554         if (ba[0]) {
1555             if (desc_offset < size) {
1556                 static const uint32_t fcs_pad;
1557                 size_t iov_copy;
1558                 size_t copy_size = size - desc_offset;
1559                 if (copy_size > core->rx_desc_buf_size) {
1560                     copy_size = core->rx_desc_buf_size;
1561                 }
1562 
1563                 /* For PS mode copy the packet header first */
1564                 if (do_ps) {
1565                     if (is_first) {
1566                         size_t ps_hdr_copied = 0;
1567                         do {
1568                             iov_copy = MIN(ps_hdr_len - ps_hdr_copied,
1569                                            iov->iov_len - iov_ofs);
1570 
1571                             e1000e_write_hdr_to_rx_buffers(core, ba, &bastate,
1572                                                       iov->iov_base, iov_copy);
1573 
1574                             copy_size -= iov_copy;
1575                             ps_hdr_copied += iov_copy;
1576 
1577                             iov_ofs += iov_copy;
1578                             if (iov_ofs == iov->iov_len) {
1579                                 iov++;
1580                                 iov_ofs = 0;
1581                             }
1582                         } while (ps_hdr_copied < ps_hdr_len);
1583 
1584                         is_first = false;
1585                     } else {
1586                         /* Leave buffer 0 of each descriptor except first */
1587                         /* empty as per spec 7.1.5.1                      */
1588                         e1000e_write_hdr_to_rx_buffers(core, ba, &bastate,
1589                                                        NULL, 0);
1590                     }
1591                 }
1592 
1593                 /* Copy packet payload */
1594                 while (copy_size) {
1595                     iov_copy = MIN(copy_size, iov->iov_len - iov_ofs);
1596 
1597                     e1000e_write_payload_frag_to_rx_buffers(core, ba, &bastate,
1598                                                             iov->iov_base +
1599                                                             iov_ofs,
1600                                                             iov_copy);
1601 
1602                     copy_size -= iov_copy;
1603                     iov_ofs += iov_copy;
1604                     if (iov_ofs == iov->iov_len) {
1605                         iov++;
1606                         iov_ofs = 0;
1607                     }
1608                 }
1609 
1610                 if (desc_offset + desc_size >= total_size) {
1611                     /* Simulate FCS checksum presence in the last descriptor */
1612                     e1000e_write_payload_frag_to_rx_buffers(core, ba, &bastate,
1613                           (const char *) &fcs_pad, e1000x_fcs_len(core->mac));
1614                 }
1615             }
1616         } else { /* as per intel docs; skip descriptors with null buf addr */
1617             trace_e1000e_rx_null_descriptor();
1618         }
1619         desc_offset += desc_size;
1620         if (desc_offset >= total_size) {
1621             is_last = true;
1622         }
1623 
1624         e1000e_write_rx_descr(core, &desc, is_last ? core->rx_pkt : NULL,
1625                            rss_info, do_ps ? ps_hdr_len : 0, &bastate.written);
1626         e1000e_pci_dma_write_rx_desc(core, base, &desc, core->rx_desc_len);
1627 
1628         e1000e_ring_advance(core, rxi,
1629                             core->rx_desc_len / E1000_MIN_RX_DESC_LEN);
1630 
1631     } while (desc_offset < total_size);
1632 
1633     e1000e_update_rx_stats(core, size, total_size);
1634 }
1635 
1636 static inline void
1637 e1000e_rx_fix_l4_csum(E1000ECore *core, struct NetRxPkt *pkt)
1638 {
1639     struct virtio_net_hdr *vhdr = net_rx_pkt_get_vhdr(pkt);
1640 
1641     if (vhdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1642         net_rx_pkt_fix_l4_csum(pkt);
1643     }
1644 }
1645 
1646 ssize_t
1647 e1000e_receive_iov(E1000ECore *core, const struct iovec *iov, int iovcnt)
1648 {
1649     return e1000e_receive_internal(core, iov, iovcnt, core->has_vnet);
1650 }
1651 
1652 static ssize_t
1653 e1000e_receive_internal(E1000ECore *core, const struct iovec *iov, int iovcnt,
1654                         bool has_vnet)
1655 {
1656     uint32_t causes = 0;
1657     uint8_t buf[ETH_ZLEN];
1658     struct iovec min_iov;
1659     size_t size, orig_size;
1660     size_t iov_ofs = 0;
1661     E1000E_RxRing rxr;
1662     E1000E_RSSInfo rss_info;
1663     size_t total_size;
1664     ssize_t retval;
1665     bool rdmts_hit;
1666 
1667     trace_e1000e_rx_receive_iov(iovcnt);
1668 
1669     if (!e1000x_hw_rx_enabled(core->mac)) {
1670         return -1;
1671     }
1672 
1673     /* Pull virtio header in */
1674     if (has_vnet) {
1675         net_rx_pkt_set_vhdr_iovec(core->rx_pkt, iov, iovcnt);
1676         iov_ofs = sizeof(struct virtio_net_hdr);
1677     } else {
1678         net_rx_pkt_unset_vhdr(core->rx_pkt);
1679     }
1680 
1681     orig_size = iov_size(iov, iovcnt);
1682     size = orig_size - iov_ofs;
1683 
1684     /* Pad to minimum Ethernet frame length */
1685     if (size < sizeof(buf)) {
1686         iov_to_buf(iov, iovcnt, iov_ofs, buf, size);
1687         memset(&buf[size], 0, sizeof(buf) - size);
1688         e1000x_inc_reg_if_not_full(core->mac, RUC);
1689         min_iov.iov_base = buf;
1690         min_iov.iov_len = size = sizeof(buf);
1691         iovcnt = 1;
1692         iov = &min_iov;
1693         iov_ofs = 0;
1694     } else {
1695         iov_to_buf(iov, iovcnt, iov_ofs, buf, ETH_HLEN + 4);
1696     }
1697 
1698     /* Discard oversized packets if !LPE and !SBP. */
1699     if (e1000x_is_oversized(core->mac, size)) {
1700         return orig_size;
1701     }
1702 
1703     net_rx_pkt_set_packet_type(core->rx_pkt,
1704         get_eth_packet_type(PKT_GET_ETH_HDR(buf)));
1705 
1706     if (!e1000e_receive_filter(core, buf)) {
1707         trace_e1000e_rx_flt_dropped();
1708         return orig_size;
1709     }
1710 
1711     net_rx_pkt_attach_iovec_ex(core->rx_pkt, iov, iovcnt, iov_ofs,
1712                                e1000x_vlan_enabled(core->mac) ? 0 : -1,
1713                                core->mac[VET], 0);
1714 
1715     e1000e_rss_parse_packet(core, core->rx_pkt, &rss_info);
1716     e1000e_rx_ring_init(core, &rxr, rss_info.queue);
1717 
1718     total_size = net_rx_pkt_get_total_len(core->rx_pkt) +
1719         e1000x_fcs_len(core->mac);
1720 
1721     if (e1000e_has_rxbufs(core, rxr.i, total_size)) {
1722         e1000e_rx_fix_l4_csum(core, core->rx_pkt);
1723 
1724         e1000e_write_packet_to_guest(core, core->rx_pkt, &rxr, &rss_info);
1725 
1726         retval = orig_size;
1727 
1728         /* Perform small receive detection (RSRPD) */
1729         if (total_size < core->mac[RSRPD]) {
1730             causes |= E1000_ICS_SRPD;
1731         }
1732 
1733         /* Perform ACK receive detection */
1734         if  (!(core->mac[RFCTL] & E1000_RFCTL_ACK_DIS) &&
1735              (e1000e_is_tcp_ack(core, core->rx_pkt))) {
1736             causes |= E1000_ICS_ACK;
1737         }
1738 
1739         /* Check if receive descriptor minimum threshold hit */
1740         rdmts_hit = e1000e_rx_descr_threshold_hit(core, rxr.i);
1741         causes |= e1000e_rx_wb_interrupt_cause(core, rxr.i->idx, rdmts_hit);
1742 
1743         trace_e1000e_rx_written_to_guest(rxr.i->idx);
1744     } else {
1745         causes |= E1000_ICS_RXO;
1746         retval = 0;
1747 
1748         trace_e1000e_rx_not_written_to_guest(rxr.i->idx);
1749     }
1750 
1751     if (!e1000e_intrmgr_delay_rx_causes(core, &causes)) {
1752         trace_e1000e_rx_interrupt_set(causes);
1753         e1000e_set_interrupt_cause(core, causes);
1754     } else {
1755         trace_e1000e_rx_interrupt_delayed(causes);
1756     }
1757 
1758     return retval;
1759 }
1760 
1761 static inline bool
1762 e1000e_have_autoneg(E1000ECore *core)
1763 {
1764     return core->phy[0][MII_BMCR] & MII_BMCR_AUTOEN;
1765 }
1766 
1767 static void e1000e_update_flowctl_status(E1000ECore *core)
1768 {
1769     if (e1000e_have_autoneg(core) &&
1770         core->phy[0][MII_BMSR] & MII_BMSR_AN_COMP) {
1771         trace_e1000e_link_autoneg_flowctl(true);
1772         core->mac[CTRL] |= E1000_CTRL_TFCE | E1000_CTRL_RFCE;
1773     } else {
1774         trace_e1000e_link_autoneg_flowctl(false);
1775     }
1776 }
1777 
1778 static inline void
1779 e1000e_link_down(E1000ECore *core)
1780 {
1781     e1000x_update_regs_on_link_down(core->mac, core->phy[0]);
1782     e1000e_update_flowctl_status(core);
1783 }
1784 
1785 static inline void
1786 e1000e_set_phy_ctrl(E1000ECore *core, int index, uint16_t val)
1787 {
1788     /* bits 0-5 reserved; MII_BMCR_[ANRESTART,RESET] are self clearing */
1789     core->phy[0][MII_BMCR] = val & ~(0x3f |
1790                                      MII_BMCR_RESET |
1791                                      MII_BMCR_ANRESTART);
1792 
1793     if ((val & MII_BMCR_ANRESTART) &&
1794         e1000e_have_autoneg(core)) {
1795         e1000x_restart_autoneg(core->mac, core->phy[0], core->autoneg_timer);
1796     }
1797 }
1798 
1799 static void
1800 e1000e_set_phy_oem_bits(E1000ECore *core, int index, uint16_t val)
1801 {
1802     core->phy[0][PHY_OEM_BITS] = val & ~BIT(10);
1803 
1804     if (val & BIT(10)) {
1805         e1000x_restart_autoneg(core->mac, core->phy[0], core->autoneg_timer);
1806     }
1807 }
1808 
1809 static void
1810 e1000e_set_phy_page(E1000ECore *core, int index, uint16_t val)
1811 {
1812     core->phy[0][PHY_PAGE] = val & PHY_PAGE_RW_MASK;
1813 }
1814 
1815 void
1816 e1000e_core_set_link_status(E1000ECore *core)
1817 {
1818     NetClientState *nc = qemu_get_queue(core->owner_nic);
1819     uint32_t old_status = core->mac[STATUS];
1820 
1821     trace_e1000e_link_status_changed(nc->link_down ? false : true);
1822 
1823     if (nc->link_down) {
1824         e1000x_update_regs_on_link_down(core->mac, core->phy[0]);
1825     } else {
1826         if (e1000e_have_autoneg(core) &&
1827             !(core->phy[0][MII_BMSR] & MII_BMSR_AN_COMP)) {
1828             e1000x_restart_autoneg(core->mac, core->phy[0],
1829                                    core->autoneg_timer);
1830         } else {
1831             e1000x_update_regs_on_link_up(core->mac, core->phy[0]);
1832             e1000e_start_recv(core);
1833         }
1834     }
1835 
1836     if (core->mac[STATUS] != old_status) {
1837         e1000e_set_interrupt_cause(core, E1000_ICR_LSC);
1838     }
1839 }
1840 
1841 static void
1842 e1000e_set_ctrl(E1000ECore *core, int index, uint32_t val)
1843 {
1844     trace_e1000e_core_ctrl_write(index, val);
1845 
1846     /* RST is self clearing */
1847     core->mac[CTRL] = val & ~E1000_CTRL_RST;
1848     core->mac[CTRL_DUP] = core->mac[CTRL];
1849 
1850     trace_e1000e_link_set_params(
1851         !!(val & E1000_CTRL_ASDE),
1852         (val & E1000_CTRL_SPD_SEL) >> E1000_CTRL_SPD_SHIFT,
1853         !!(val & E1000_CTRL_FRCSPD),
1854         !!(val & E1000_CTRL_FRCDPX),
1855         !!(val & E1000_CTRL_RFCE),
1856         !!(val & E1000_CTRL_TFCE));
1857 
1858     if (val & E1000_CTRL_RST) {
1859         trace_e1000e_core_ctrl_sw_reset();
1860         e1000e_reset(core, true);
1861     }
1862 
1863     if (val & E1000_CTRL_PHY_RST) {
1864         trace_e1000e_core_ctrl_phy_reset();
1865         core->mac[STATUS] |= E1000_STATUS_PHYRA;
1866     }
1867 }
1868 
1869 static void
1870 e1000e_set_rfctl(E1000ECore *core, int index, uint32_t val)
1871 {
1872     trace_e1000e_rx_set_rfctl(val);
1873 
1874     if (!(val & E1000_RFCTL_ISCSI_DIS)) {
1875         trace_e1000e_wrn_iscsi_filtering_not_supported();
1876     }
1877 
1878     if (!(val & E1000_RFCTL_NFSW_DIS)) {
1879         trace_e1000e_wrn_nfsw_filtering_not_supported();
1880     }
1881 
1882     if (!(val & E1000_RFCTL_NFSR_DIS)) {
1883         trace_e1000e_wrn_nfsr_filtering_not_supported();
1884     }
1885 
1886     core->mac[RFCTL] = val;
1887 }
1888 
1889 static void
1890 e1000e_calc_per_desc_buf_size(E1000ECore *core)
1891 {
1892     int i;
1893     core->rx_desc_buf_size = 0;
1894 
1895     for (i = 0; i < ARRAY_SIZE(core->rxbuf_sizes); i++) {
1896         core->rx_desc_buf_size += core->rxbuf_sizes[i];
1897     }
1898 }
1899 
1900 static void
1901 e1000e_parse_rxbufsize(E1000ECore *core)
1902 {
1903     uint32_t rctl = core->mac[RCTL];
1904 
1905     memset(core->rxbuf_sizes, 0, sizeof(core->rxbuf_sizes));
1906 
1907     if (rctl & E1000_RCTL_DTYP_MASK) {
1908         uint32_t bsize;
1909 
1910         bsize = core->mac[PSRCTL] & E1000_PSRCTL_BSIZE0_MASK;
1911         core->rxbuf_sizes[0] = (bsize >> E1000_PSRCTL_BSIZE0_SHIFT) * 128;
1912 
1913         bsize = core->mac[PSRCTL] & E1000_PSRCTL_BSIZE1_MASK;
1914         core->rxbuf_sizes[1] = (bsize >> E1000_PSRCTL_BSIZE1_SHIFT) * 1024;
1915 
1916         bsize = core->mac[PSRCTL] & E1000_PSRCTL_BSIZE2_MASK;
1917         core->rxbuf_sizes[2] = (bsize >> E1000_PSRCTL_BSIZE2_SHIFT) * 1024;
1918 
1919         bsize = core->mac[PSRCTL] & E1000_PSRCTL_BSIZE3_MASK;
1920         core->rxbuf_sizes[3] = (bsize >> E1000_PSRCTL_BSIZE3_SHIFT) * 1024;
1921     } else if (rctl & E1000_RCTL_FLXBUF_MASK) {
1922         int flxbuf = rctl & E1000_RCTL_FLXBUF_MASK;
1923         core->rxbuf_sizes[0] = (flxbuf >> E1000_RCTL_FLXBUF_SHIFT) * 1024;
1924     } else {
1925         core->rxbuf_sizes[0] = e1000x_rxbufsize(rctl);
1926     }
1927 
1928     trace_e1000e_rx_desc_buff_sizes(core->rxbuf_sizes[0], core->rxbuf_sizes[1],
1929                                     core->rxbuf_sizes[2], core->rxbuf_sizes[3]);
1930 
1931     e1000e_calc_per_desc_buf_size(core);
1932 }
1933 
1934 static void
1935 e1000e_calc_rxdesclen(E1000ECore *core)
1936 {
1937     if (e1000e_rx_use_legacy_descriptor(core)) {
1938         core->rx_desc_len = sizeof(struct e1000_rx_desc);
1939     } else {
1940         if (core->mac[RCTL] & E1000_RCTL_DTYP_PS) {
1941             core->rx_desc_len = sizeof(union e1000_rx_desc_packet_split);
1942         } else {
1943             core->rx_desc_len = sizeof(union e1000_rx_desc_extended);
1944         }
1945     }
1946     trace_e1000e_rx_desc_len(core->rx_desc_len);
1947 }
1948 
1949 static void
1950 e1000e_set_rx_control(E1000ECore *core, int index, uint32_t val)
1951 {
1952     core->mac[RCTL] = val;
1953     trace_e1000e_rx_set_rctl(core->mac[RCTL]);
1954 
1955     if (val & E1000_RCTL_EN) {
1956         e1000e_parse_rxbufsize(core);
1957         e1000e_calc_rxdesclen(core);
1958         core->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1 +
1959                                 E1000_RING_DESC_LEN_SHIFT;
1960 
1961         e1000e_start_recv(core);
1962     }
1963 }
1964 
1965 static
1966 void(*e1000e_phyreg_writeops[E1000E_PHY_PAGES][E1000E_PHY_PAGE_SIZE])
1967 (E1000ECore *, int, uint16_t) = {
1968     [0] = {
1969         [MII_BMCR]     = e1000e_set_phy_ctrl,
1970         [PHY_PAGE]     = e1000e_set_phy_page,
1971         [PHY_OEM_BITS] = e1000e_set_phy_oem_bits
1972     }
1973 };
1974 
1975 static inline bool
1976 e1000e_postpone_interrupt(E1000IntrDelayTimer *timer)
1977 {
1978     if (timer->running) {
1979         trace_e1000e_irq_postponed_by_xitr(timer->delay_reg << 2);
1980 
1981         return true;
1982     }
1983 
1984     if (timer->core->mac[timer->delay_reg] != 0) {
1985         e1000e_intrmgr_rearm_timer(timer);
1986     }
1987 
1988     return false;
1989 }
1990 
1991 static inline bool
1992 e1000e_itr_should_postpone(E1000ECore *core)
1993 {
1994     return e1000e_postpone_interrupt(&core->itr);
1995 }
1996 
1997 static inline bool
1998 e1000e_eitr_should_postpone(E1000ECore *core, int idx)
1999 {
2000     return e1000e_postpone_interrupt(&core->eitr[idx]);
2001 }
2002 
2003 static void
2004 e1000e_msix_notify_one(E1000ECore *core, uint32_t cause, uint32_t int_cfg)
2005 {
2006     uint32_t effective_eiac;
2007 
2008     if (E1000_IVAR_ENTRY_VALID(int_cfg)) {
2009         uint32_t vec = E1000_IVAR_ENTRY_VEC(int_cfg);
2010         if (vec < E1000E_MSIX_VEC_NUM) {
2011             if (!e1000e_eitr_should_postpone(core, vec)) {
2012                 trace_e1000e_irq_msix_notify_vec(vec);
2013                 msix_notify(core->owner, vec);
2014             }
2015         } else {
2016             trace_e1000e_wrn_msix_vec_wrong(cause, int_cfg);
2017         }
2018     } else {
2019         trace_e1000e_wrn_msix_invalid(cause, int_cfg);
2020     }
2021 
2022     if (core->mac[CTRL_EXT] & E1000_CTRL_EXT_EIAME) {
2023         trace_e1000e_irq_iam_clear_eiame(core->mac[IAM], cause);
2024         core->mac[IAM] &= ~cause;
2025     }
2026 
2027     trace_e1000e_irq_icr_clear_eiac(core->mac[ICR], core->mac[EIAC]);
2028 
2029     effective_eiac = core->mac[EIAC] & cause;
2030 
2031     core->mac[ICR] &= ~effective_eiac;
2032 
2033     if (!(core->mac[CTRL_EXT] & E1000_CTRL_EXT_IAME)) {
2034         core->mac[IMS] &= ~effective_eiac;
2035     }
2036 }
2037 
2038 static void
2039 e1000e_msix_notify(E1000ECore *core, uint32_t causes)
2040 {
2041     if (causes & E1000_ICR_RXQ0) {
2042         e1000e_msix_notify_one(core, E1000_ICR_RXQ0,
2043                                E1000_IVAR_RXQ0(core->mac[IVAR]));
2044     }
2045 
2046     if (causes & E1000_ICR_RXQ1) {
2047         e1000e_msix_notify_one(core, E1000_ICR_RXQ1,
2048                                E1000_IVAR_RXQ1(core->mac[IVAR]));
2049     }
2050 
2051     if (causes & E1000_ICR_TXQ0) {
2052         e1000e_msix_notify_one(core, E1000_ICR_TXQ0,
2053                                E1000_IVAR_TXQ0(core->mac[IVAR]));
2054     }
2055 
2056     if (causes & E1000_ICR_TXQ1) {
2057         e1000e_msix_notify_one(core, E1000_ICR_TXQ1,
2058                                E1000_IVAR_TXQ1(core->mac[IVAR]));
2059     }
2060 
2061     if (causes & E1000_ICR_OTHER) {
2062         e1000e_msix_notify_one(core, E1000_ICR_OTHER,
2063                                E1000_IVAR_OTHER(core->mac[IVAR]));
2064     }
2065 }
2066 
2067 static void
2068 e1000e_msix_clear_one(E1000ECore *core, uint32_t cause, uint32_t int_cfg)
2069 {
2070     if (E1000_IVAR_ENTRY_VALID(int_cfg)) {
2071         uint32_t vec = E1000_IVAR_ENTRY_VEC(int_cfg);
2072         if (vec < E1000E_MSIX_VEC_NUM) {
2073             trace_e1000e_irq_msix_pending_clearing(cause, int_cfg, vec);
2074             msix_clr_pending(core->owner, vec);
2075         } else {
2076             trace_e1000e_wrn_msix_vec_wrong(cause, int_cfg);
2077         }
2078     } else {
2079         trace_e1000e_wrn_msix_invalid(cause, int_cfg);
2080     }
2081 }
2082 
2083 static void
2084 e1000e_msix_clear(E1000ECore *core, uint32_t causes)
2085 {
2086     if (causes & E1000_ICR_RXQ0) {
2087         e1000e_msix_clear_one(core, E1000_ICR_RXQ0,
2088                               E1000_IVAR_RXQ0(core->mac[IVAR]));
2089     }
2090 
2091     if (causes & E1000_ICR_RXQ1) {
2092         e1000e_msix_clear_one(core, E1000_ICR_RXQ1,
2093                               E1000_IVAR_RXQ1(core->mac[IVAR]));
2094     }
2095 
2096     if (causes & E1000_ICR_TXQ0) {
2097         e1000e_msix_clear_one(core, E1000_ICR_TXQ0,
2098                               E1000_IVAR_TXQ0(core->mac[IVAR]));
2099     }
2100 
2101     if (causes & E1000_ICR_TXQ1) {
2102         e1000e_msix_clear_one(core, E1000_ICR_TXQ1,
2103                               E1000_IVAR_TXQ1(core->mac[IVAR]));
2104     }
2105 
2106     if (causes & E1000_ICR_OTHER) {
2107         e1000e_msix_clear_one(core, E1000_ICR_OTHER,
2108                               E1000_IVAR_OTHER(core->mac[IVAR]));
2109     }
2110 }
2111 
2112 static inline void
2113 e1000e_fix_icr_asserted(E1000ECore *core)
2114 {
2115     core->mac[ICR] &= ~E1000_ICR_ASSERTED;
2116     if (core->mac[ICR]) {
2117         core->mac[ICR] |= E1000_ICR_ASSERTED;
2118     }
2119 
2120     trace_e1000e_irq_fix_icr_asserted(core->mac[ICR]);
2121 }
2122 
2123 static void e1000e_raise_interrupts(E1000ECore *core,
2124                                     size_t index, uint32_t causes)
2125 {
2126     bool is_msix = msix_enabled(core->owner);
2127     uint32_t old_causes = core->mac[IMS] & core->mac[ICR];
2128     uint32_t raised_causes;
2129 
2130     trace_e1000e_irq_set(index << 2,
2131                          core->mac[index], core->mac[index] | causes);
2132 
2133     core->mac[index] |= causes;
2134 
2135     /* Set ICR[OTHER] for MSI-X */
2136     if (is_msix) {
2137         if (core->mac[ICR] & E1000_ICR_OTHER_CAUSES) {
2138             core->mac[ICR] |= E1000_ICR_OTHER;
2139             trace_e1000e_irq_add_msi_other(core->mac[ICR]);
2140         }
2141     }
2142 
2143     e1000e_fix_icr_asserted(core);
2144 
2145     /*
2146      * Make sure ICR and ICS registers have the same value.
2147      * The spec says that the ICS register is write-only.  However in practice,
2148      * on real hardware ICS is readable, and for reads it has the same value as
2149      * ICR (except that ICS does not have the clear on read behaviour of ICR).
2150      *
2151      * The VxWorks PRO/1000 driver uses this behaviour.
2152      */
2153     core->mac[ICS] = core->mac[ICR];
2154 
2155     trace_e1000e_irq_pending_interrupts(core->mac[ICR] & core->mac[IMS],
2156                                         core->mac[ICR], core->mac[IMS]);
2157 
2158     raised_causes = core->mac[IMS] & core->mac[ICR] & ~old_causes;
2159     if (!raised_causes) {
2160         return;
2161     }
2162 
2163     if (is_msix) {
2164         e1000e_msix_notify(core, raised_causes & ~E1000_ICR_ASSERTED);
2165     } else if (!e1000e_itr_should_postpone(core)) {
2166         if (msi_enabled(core->owner)) {
2167             trace_e1000e_irq_msi_notify(raised_causes);
2168             msi_notify(core->owner, 0);
2169         } else {
2170             e1000e_raise_legacy_irq(core);
2171         }
2172     }
2173 }
2174 
2175 static void e1000e_lower_interrupts(E1000ECore *core,
2176                                     size_t index, uint32_t causes)
2177 {
2178     trace_e1000e_irq_clear(index << 2,
2179                            core->mac[index], core->mac[index] & ~causes);
2180 
2181     core->mac[index] &= ~causes;
2182 
2183     /*
2184      * Make sure ICR and ICS registers have the same value.
2185      * The spec says that the ICS register is write-only.  However in practice,
2186      * on real hardware ICS is readable, and for reads it has the same value as
2187      * ICR (except that ICS does not have the clear on read behaviour of ICR).
2188      *
2189      * The VxWorks PRO/1000 driver uses this behaviour.
2190      */
2191     core->mac[ICS] = core->mac[ICR];
2192 
2193     trace_e1000e_irq_pending_interrupts(core->mac[ICR] & core->mac[IMS],
2194                                         core->mac[ICR], core->mac[IMS]);
2195 
2196     if (!(core->mac[IMS] & core->mac[ICR]) &&
2197         !msix_enabled(core->owner) && !msi_enabled(core->owner)) {
2198         e1000e_lower_legacy_irq(core);
2199     }
2200 }
2201 
2202 static void
2203 e1000e_set_interrupt_cause(E1000ECore *core, uint32_t val)
2204 {
2205     val |= e1000e_intmgr_collect_delayed_causes(core);
2206     e1000e_raise_interrupts(core, ICR, val);
2207 }
2208 
2209 static inline void
2210 e1000e_autoneg_timer(void *opaque)
2211 {
2212     E1000ECore *core = opaque;
2213     if (!qemu_get_queue(core->owner_nic)->link_down) {
2214         e1000x_update_regs_on_autoneg_done(core->mac, core->phy[0]);
2215         e1000e_start_recv(core);
2216 
2217         e1000e_update_flowctl_status(core);
2218         /* signal link status change to the guest */
2219         e1000e_set_interrupt_cause(core, E1000_ICR_LSC);
2220     }
2221 }
2222 
2223 static inline uint16_t
2224 e1000e_get_reg_index_with_offset(const uint16_t *mac_reg_access, hwaddr addr)
2225 {
2226     uint16_t index = (addr & 0x1ffff) >> 2;
2227     return index + (mac_reg_access[index] & 0xfffe);
2228 }
2229 
2230 static const char e1000e_phy_regcap[E1000E_PHY_PAGES][0x20] = {
2231     [0] = {
2232         [MII_BMCR]              = PHY_ANYPAGE | PHY_RW,
2233         [MII_BMSR]              = PHY_ANYPAGE | PHY_R,
2234         [MII_PHYID1]            = PHY_ANYPAGE | PHY_R,
2235         [MII_PHYID2]            = PHY_ANYPAGE | PHY_R,
2236         [MII_ANAR]              = PHY_ANYPAGE | PHY_RW,
2237         [MII_ANLPAR]            = PHY_ANYPAGE | PHY_R,
2238         [MII_ANER]              = PHY_ANYPAGE | PHY_R,
2239         [MII_ANNP]              = PHY_ANYPAGE | PHY_RW,
2240         [MII_ANLPRNP]           = PHY_ANYPAGE | PHY_R,
2241         [MII_CTRL1000]          = PHY_ANYPAGE | PHY_RW,
2242         [MII_STAT1000]          = PHY_ANYPAGE | PHY_R,
2243         [MII_EXTSTAT]           = PHY_ANYPAGE | PHY_R,
2244         [PHY_PAGE]              = PHY_ANYPAGE | PHY_RW,
2245 
2246         [PHY_COPPER_CTRL1]      = PHY_RW,
2247         [PHY_COPPER_STAT1]      = PHY_R,
2248         [PHY_COPPER_CTRL3]      = PHY_RW,
2249         [PHY_RX_ERR_CNTR]       = PHY_R,
2250         [PHY_OEM_BITS]          = PHY_RW,
2251         [PHY_BIAS_1]            = PHY_RW,
2252         [PHY_BIAS_2]            = PHY_RW,
2253         [PHY_COPPER_INT_ENABLE] = PHY_RW,
2254         [PHY_COPPER_STAT2]      = PHY_R,
2255         [PHY_COPPER_CTRL2]      = PHY_RW
2256     },
2257     [2] = {
2258         [PHY_MAC_CTRL1]         = PHY_RW,
2259         [PHY_MAC_INT_ENABLE]    = PHY_RW,
2260         [PHY_MAC_STAT]          = PHY_R,
2261         [PHY_MAC_CTRL2]         = PHY_RW
2262     },
2263     [3] = {
2264         [PHY_LED_03_FUNC_CTRL1] = PHY_RW,
2265         [PHY_LED_03_POL_CTRL]   = PHY_RW,
2266         [PHY_LED_TIMER_CTRL]    = PHY_RW,
2267         [PHY_LED_45_CTRL]       = PHY_RW
2268     },
2269     [5] = {
2270         [PHY_1000T_SKEW]        = PHY_R,
2271         [PHY_1000T_SWAP]        = PHY_R
2272     },
2273     [6] = {
2274         [PHY_CRC_COUNTERS]      = PHY_R
2275     }
2276 };
2277 
2278 static bool
2279 e1000e_phy_reg_check_cap(E1000ECore *core, uint32_t addr,
2280                          char cap, uint8_t *page)
2281 {
2282     *page =
2283         (e1000e_phy_regcap[0][addr] & PHY_ANYPAGE) ? 0
2284                                                     : core->phy[0][PHY_PAGE];
2285 
2286     if (*page >= E1000E_PHY_PAGES) {
2287         return false;
2288     }
2289 
2290     return e1000e_phy_regcap[*page][addr] & cap;
2291 }
2292 
2293 static void
2294 e1000e_phy_reg_write(E1000ECore *core, uint8_t page,
2295                      uint32_t addr, uint16_t data)
2296 {
2297     assert(page < E1000E_PHY_PAGES);
2298     assert(addr < E1000E_PHY_PAGE_SIZE);
2299 
2300     if (e1000e_phyreg_writeops[page][addr]) {
2301         e1000e_phyreg_writeops[page][addr](core, addr, data);
2302     } else {
2303         core->phy[page][addr] = data;
2304     }
2305 }
2306 
2307 static void
2308 e1000e_set_mdic(E1000ECore *core, int index, uint32_t val)
2309 {
2310     uint32_t data = val & E1000_MDIC_DATA_MASK;
2311     uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT);
2312     uint8_t page;
2313 
2314     if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) { /* phy # */
2315         val = core->mac[MDIC] | E1000_MDIC_ERROR;
2316     } else if (val & E1000_MDIC_OP_READ) {
2317         if (!e1000e_phy_reg_check_cap(core, addr, PHY_R, &page)) {
2318             trace_e1000e_core_mdic_read_unhandled(page, addr);
2319             val |= E1000_MDIC_ERROR;
2320         } else {
2321             val = (val ^ data) | core->phy[page][addr];
2322             trace_e1000e_core_mdic_read(page, addr, val);
2323         }
2324     } else if (val & E1000_MDIC_OP_WRITE) {
2325         if (!e1000e_phy_reg_check_cap(core, addr, PHY_W, &page)) {
2326             trace_e1000e_core_mdic_write_unhandled(page, addr);
2327             val |= E1000_MDIC_ERROR;
2328         } else {
2329             trace_e1000e_core_mdic_write(page, addr, data);
2330             e1000e_phy_reg_write(core, page, addr, data);
2331         }
2332     }
2333     core->mac[MDIC] = val | E1000_MDIC_READY;
2334 
2335     if (val & E1000_MDIC_INT_EN) {
2336         e1000e_set_interrupt_cause(core, E1000_ICR_MDAC);
2337     }
2338 }
2339 
2340 static void
2341 e1000e_set_rdt(E1000ECore *core, int index, uint32_t val)
2342 {
2343     core->mac[index] = val & 0xffff;
2344     trace_e1000e_rx_set_rdt(e1000e_mq_queue_idx(RDT0, index), val);
2345     e1000e_start_recv(core);
2346 }
2347 
2348 static void
2349 e1000e_set_status(E1000ECore *core, int index, uint32_t val)
2350 {
2351     if ((val & E1000_STATUS_PHYRA) == 0) {
2352         core->mac[index] &= ~E1000_STATUS_PHYRA;
2353     }
2354 }
2355 
2356 static void
2357 e1000e_set_ctrlext(E1000ECore *core, int index, uint32_t val)
2358 {
2359     trace_e1000e_link_set_ext_params(!!(val & E1000_CTRL_EXT_ASDCHK),
2360                                      !!(val & E1000_CTRL_EXT_SPD_BYPS));
2361 
2362     /* Zero self-clearing bits */
2363     val &= ~(E1000_CTRL_EXT_ASDCHK | E1000_CTRL_EXT_EE_RST);
2364     core->mac[CTRL_EXT] = val;
2365 }
2366 
2367 static void
2368 e1000e_set_pbaclr(E1000ECore *core, int index, uint32_t val)
2369 {
2370     int i;
2371 
2372     core->mac[PBACLR] = val & E1000_PBACLR_VALID_MASK;
2373 
2374     if (!msix_enabled(core->owner)) {
2375         return;
2376     }
2377 
2378     for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
2379         if (core->mac[PBACLR] & BIT(i)) {
2380             msix_clr_pending(core->owner, i);
2381         }
2382     }
2383 }
2384 
2385 static void
2386 e1000e_set_fcrth(E1000ECore *core, int index, uint32_t val)
2387 {
2388     core->mac[FCRTH] = val & 0xFFF8;
2389 }
2390 
2391 static void
2392 e1000e_set_fcrtl(E1000ECore *core, int index, uint32_t val)
2393 {
2394     core->mac[FCRTL] = val & 0x8000FFF8;
2395 }
2396 
2397 #define E1000E_LOW_BITS_SET_FUNC(num)                                \
2398     static void                                                      \
2399     e1000e_set_##num##bit(E1000ECore *core, int index, uint32_t val) \
2400     {                                                                \
2401         core->mac[index] = val & (BIT(num) - 1);                     \
2402     }
2403 
2404 E1000E_LOW_BITS_SET_FUNC(4)
2405 E1000E_LOW_BITS_SET_FUNC(6)
2406 E1000E_LOW_BITS_SET_FUNC(11)
2407 E1000E_LOW_BITS_SET_FUNC(12)
2408 E1000E_LOW_BITS_SET_FUNC(13)
2409 E1000E_LOW_BITS_SET_FUNC(16)
2410 
2411 static void
2412 e1000e_set_vet(E1000ECore *core, int index, uint32_t val)
2413 {
2414     core->mac[VET] = val & 0xffff;
2415     trace_e1000e_vlan_vet(core->mac[VET]);
2416 }
2417 
2418 static void
2419 e1000e_set_dlen(E1000ECore *core, int index, uint32_t val)
2420 {
2421     core->mac[index] = val & E1000_XDLEN_MASK;
2422 }
2423 
2424 static void
2425 e1000e_set_dbal(E1000ECore *core, int index, uint32_t val)
2426 {
2427     core->mac[index] = val & E1000_XDBAL_MASK;
2428 }
2429 
2430 static void
2431 e1000e_set_tctl(E1000ECore *core, int index, uint32_t val)
2432 {
2433     E1000E_TxRing txr;
2434     core->mac[index] = val;
2435 
2436     if (core->mac[TARC0] & E1000_TARC_ENABLE) {
2437         e1000e_tx_ring_init(core, &txr, 0);
2438         e1000e_start_xmit(core, &txr);
2439     }
2440 
2441     if (core->mac[TARC1] & E1000_TARC_ENABLE) {
2442         e1000e_tx_ring_init(core, &txr, 1);
2443         e1000e_start_xmit(core, &txr);
2444     }
2445 }
2446 
2447 static void
2448 e1000e_set_tdt(E1000ECore *core, int index, uint32_t val)
2449 {
2450     E1000E_TxRing txr;
2451     int qidx = e1000e_mq_queue_idx(TDT, index);
2452     uint32_t tarc_reg = (qidx == 0) ? TARC0 : TARC1;
2453 
2454     core->mac[index] = val & 0xffff;
2455 
2456     if (core->mac[tarc_reg] & E1000_TARC_ENABLE) {
2457         e1000e_tx_ring_init(core, &txr, qidx);
2458         e1000e_start_xmit(core, &txr);
2459     }
2460 }
2461 
2462 static void
2463 e1000e_set_ics(E1000ECore *core, int index, uint32_t val)
2464 {
2465     trace_e1000e_irq_write_ics(val);
2466     e1000e_set_interrupt_cause(core, val);
2467 }
2468 
2469 static void
2470 e1000e_set_icr(E1000ECore *core, int index, uint32_t val)
2471 {
2472     if ((core->mac[ICR] & E1000_ICR_ASSERTED) &&
2473         (core->mac[CTRL_EXT] & E1000_CTRL_EXT_IAME)) {
2474         trace_e1000e_irq_icr_process_iame();
2475         e1000e_lower_interrupts(core, IMS, core->mac[IAM]);
2476     }
2477 
2478     /*
2479      * Windows driver expects that the "receive overrun" bit and other
2480      * ones to be cleared when the "Other" bit (#24) is cleared.
2481      */
2482     if (val & E1000_ICR_OTHER) {
2483         val |= E1000_ICR_OTHER_CAUSES;
2484     }
2485     e1000e_lower_interrupts(core, ICR, val);
2486 }
2487 
2488 static void
2489 e1000e_set_imc(E1000ECore *core, int index, uint32_t val)
2490 {
2491     trace_e1000e_irq_ims_clear_set_imc(val);
2492     e1000e_lower_interrupts(core, IMS, val);
2493 }
2494 
2495 static void
2496 e1000e_set_ims(E1000ECore *core, int index, uint32_t val)
2497 {
2498     static const uint32_t ims_ext_mask =
2499         E1000_IMS_RXQ0 | E1000_IMS_RXQ1 |
2500         E1000_IMS_TXQ0 | E1000_IMS_TXQ1 |
2501         E1000_IMS_OTHER;
2502 
2503     static const uint32_t ims_valid_mask =
2504         E1000_IMS_TXDW      | E1000_IMS_TXQE    | E1000_IMS_LSC  |
2505         E1000_IMS_RXDMT0    | E1000_IMS_RXO     | E1000_IMS_RXT0 |
2506         E1000_IMS_MDAC      | E1000_IMS_TXD_LOW | E1000_IMS_SRPD |
2507         E1000_IMS_ACK       | E1000_IMS_MNG     | E1000_IMS_RXQ0 |
2508         E1000_IMS_RXQ1      | E1000_IMS_TXQ0    | E1000_IMS_TXQ1 |
2509         E1000_IMS_OTHER;
2510 
2511     uint32_t valid_val = val & ims_valid_mask;
2512 
2513     if ((valid_val & ims_ext_mask) &&
2514         (core->mac[CTRL_EXT] & E1000_CTRL_EXT_PBA_CLR) &&
2515         msix_enabled(core->owner)) {
2516         e1000e_msix_clear(core, valid_val);
2517     }
2518 
2519     if ((valid_val == ims_valid_mask) &&
2520         (core->mac[CTRL_EXT] & E1000_CTRL_EXT_INT_TIMERS_CLEAR_ENA)) {
2521         trace_e1000e_irq_fire_all_timers(val);
2522         e1000e_intrmgr_fire_all_timers(core);
2523     }
2524 
2525     e1000e_raise_interrupts(core, IMS, valid_val);
2526 }
2527 
2528 static void
2529 e1000e_set_rdtr(E1000ECore *core, int index, uint32_t val)
2530 {
2531     e1000e_set_16bit(core, index, val);
2532 
2533     if ((val & E1000_RDTR_FPD) && (core->rdtr.running)) {
2534         trace_e1000e_irq_rdtr_fpd_running();
2535         e1000e_intrmgr_fire_delayed_interrupts(core);
2536     } else {
2537         trace_e1000e_irq_rdtr_fpd_not_running();
2538     }
2539 }
2540 
2541 static void
2542 e1000e_set_tidv(E1000ECore *core, int index, uint32_t val)
2543 {
2544     e1000e_set_16bit(core, index, val);
2545 
2546     if ((val & E1000_TIDV_FPD) && (core->tidv.running)) {
2547         trace_e1000e_irq_tidv_fpd_running();
2548         e1000e_intrmgr_fire_delayed_interrupts(core);
2549     } else {
2550         trace_e1000e_irq_tidv_fpd_not_running();
2551     }
2552 }
2553 
2554 static uint32_t
2555 e1000e_mac_readreg(E1000ECore *core, int index)
2556 {
2557     return core->mac[index];
2558 }
2559 
2560 static uint32_t
2561 e1000e_mac_ics_read(E1000ECore *core, int index)
2562 {
2563     trace_e1000e_irq_read_ics(core->mac[ICS]);
2564     return core->mac[ICS];
2565 }
2566 
2567 static uint32_t
2568 e1000e_mac_ims_read(E1000ECore *core, int index)
2569 {
2570     trace_e1000e_irq_read_ims(core->mac[IMS]);
2571     return core->mac[IMS];
2572 }
2573 
2574 static uint32_t
2575 e1000e_mac_swsm_read(E1000ECore *core, int index)
2576 {
2577     uint32_t val = core->mac[SWSM];
2578     core->mac[SWSM] = val | E1000_SWSM_SMBI;
2579     return val;
2580 }
2581 
2582 static uint32_t
2583 e1000e_mac_itr_read(E1000ECore *core, int index)
2584 {
2585     return core->itr_guest_value;
2586 }
2587 
2588 static uint32_t
2589 e1000e_mac_eitr_read(E1000ECore *core, int index)
2590 {
2591     return core->eitr_guest_value[index - EITR];
2592 }
2593 
2594 static uint32_t
2595 e1000e_mac_icr_read(E1000ECore *core, int index)
2596 {
2597     uint32_t ret = core->mac[ICR];
2598 
2599     if (core->mac[IMS] == 0) {
2600         trace_e1000e_irq_icr_clear_zero_ims();
2601         e1000e_lower_interrupts(core, ICR, 0xffffffff);
2602     }
2603 
2604     if (!msix_enabled(core->owner)) {
2605         trace_e1000e_irq_icr_clear_nonmsix_icr_read();
2606         e1000e_lower_interrupts(core, ICR, 0xffffffff);
2607     }
2608 
2609     if (core->mac[ICR] & E1000_ICR_ASSERTED) {
2610         if (core->mac[CTRL_EXT] & E1000_CTRL_EXT_IAME) {
2611             trace_e1000e_irq_icr_clear_iame();
2612             e1000e_lower_interrupts(core, ICR, 0xffffffff);
2613             trace_e1000e_irq_icr_process_iame();
2614             e1000e_lower_interrupts(core, IMS, core->mac[IAM]);
2615         }
2616 
2617         /*
2618          * The datasheet does not say what happens when interrupt was asserted
2619          * (ICR.INT_ASSERT=1) and auto mask is *not* active.
2620          * However, section of 13.3.27 the PCIe* GbE Controllers Open Source
2621          * Software Developer’s Manual, which were written for older devices,
2622          * namely 631xESB/632xESB, 82563EB/82564EB, 82571EB/82572EI &
2623          * 82573E/82573V/82573L, does say:
2624          * > If IMS = 0b, then the ICR register is always clear-on-read. If IMS
2625          * > is not 0b, but some ICR bit is set where the corresponding IMS bit
2626          * > is not set, then a read does not clear the ICR register. For
2627          * > example, if IMS = 10101010b and ICR = 01010101b, then a read to the
2628          * > ICR register does not clear it. If IMS = 10101010b and
2629          * > ICR = 0101011b, then a read to the ICR register clears it entirely
2630          * > (ICR.INT_ASSERTED = 1b).
2631          *
2632          * Linux does no longer activate auto mask since commit
2633          * 0a8047ac68e50e4ccbadcfc6b6b070805b976885 and the real hardware
2634          * clears ICR even in such a case so we also should do so.
2635          */
2636         if (core->mac[ICR] & core->mac[IMS]) {
2637             trace_e1000e_irq_icr_clear_icr_bit_ims(core->mac[ICR],
2638                                                    core->mac[IMS]);
2639             e1000e_lower_interrupts(core, ICR, 0xffffffff);
2640         }
2641     }
2642 
2643     return ret;
2644 }
2645 
2646 static uint32_t
2647 e1000e_mac_read_clr4(E1000ECore *core, int index)
2648 {
2649     uint32_t ret = core->mac[index];
2650 
2651     core->mac[index] = 0;
2652     return ret;
2653 }
2654 
2655 static uint32_t
2656 e1000e_mac_read_clr8(E1000ECore *core, int index)
2657 {
2658     uint32_t ret = core->mac[index];
2659 
2660     core->mac[index] = 0;
2661     core->mac[index - 1] = 0;
2662     return ret;
2663 }
2664 
2665 static uint32_t
2666 e1000e_get_ctrl(E1000ECore *core, int index)
2667 {
2668     uint32_t val = core->mac[CTRL];
2669 
2670     trace_e1000e_link_read_params(
2671         !!(val & E1000_CTRL_ASDE),
2672         (val & E1000_CTRL_SPD_SEL) >> E1000_CTRL_SPD_SHIFT,
2673         !!(val & E1000_CTRL_FRCSPD),
2674         !!(val & E1000_CTRL_FRCDPX),
2675         !!(val & E1000_CTRL_RFCE),
2676         !!(val & E1000_CTRL_TFCE));
2677 
2678     return val;
2679 }
2680 
2681 static uint32_t
2682 e1000e_get_status(E1000ECore *core, int index)
2683 {
2684     uint32_t res = core->mac[STATUS];
2685 
2686     if (!(core->mac[CTRL] & E1000_CTRL_GIO_MASTER_DISABLE)) {
2687         res |= E1000_STATUS_GIO_MASTER_ENABLE;
2688     }
2689 
2690     if (core->mac[CTRL] & E1000_CTRL_FRCDPX) {
2691         res |= (core->mac[CTRL] & E1000_CTRL_FD) ? E1000_STATUS_FD : 0;
2692     } else {
2693         res |= E1000_STATUS_FD;
2694     }
2695 
2696     if ((core->mac[CTRL] & E1000_CTRL_FRCSPD) ||
2697         (core->mac[CTRL_EXT] & E1000_CTRL_EXT_SPD_BYPS)) {
2698         switch (core->mac[CTRL] & E1000_CTRL_SPD_SEL) {
2699         case E1000_CTRL_SPD_10:
2700             res |= E1000_STATUS_SPEED_10;
2701             break;
2702         case E1000_CTRL_SPD_100:
2703             res |= E1000_STATUS_SPEED_100;
2704             break;
2705         case E1000_CTRL_SPD_1000:
2706         default:
2707             res |= E1000_STATUS_SPEED_1000;
2708             break;
2709         }
2710     } else {
2711         res |= E1000_STATUS_SPEED_1000;
2712     }
2713 
2714     trace_e1000e_link_status(
2715         !!(res & E1000_STATUS_LU),
2716         !!(res & E1000_STATUS_FD),
2717         (res & E1000_STATUS_SPEED_MASK) >> E1000_STATUS_SPEED_SHIFT,
2718         (res & E1000_STATUS_ASDV) >> E1000_STATUS_ASDV_SHIFT);
2719 
2720     return res;
2721 }
2722 
2723 static uint32_t
2724 e1000e_get_tarc(E1000ECore *core, int index)
2725 {
2726     return core->mac[index] & ((BIT(11) - 1) |
2727                                 BIT(27)      |
2728                                 BIT(28)      |
2729                                 BIT(29)      |
2730                                 BIT(30));
2731 }
2732 
2733 static void
2734 e1000e_mac_writereg(E1000ECore *core, int index, uint32_t val)
2735 {
2736     core->mac[index] = val;
2737 }
2738 
2739 static void
2740 e1000e_mac_setmacaddr(E1000ECore *core, int index, uint32_t val)
2741 {
2742     uint32_t macaddr[2];
2743 
2744     core->mac[index] = val;
2745 
2746     macaddr[0] = cpu_to_le32(core->mac[RA]);
2747     macaddr[1] = cpu_to_le32(core->mac[RA + 1]);
2748     qemu_format_nic_info_str(qemu_get_queue(core->owner_nic),
2749         (uint8_t *) macaddr);
2750 
2751     trace_e1000e_mac_set_sw(MAC_ARG(macaddr));
2752 }
2753 
2754 static void
2755 e1000e_set_eecd(E1000ECore *core, int index, uint32_t val)
2756 {
2757     static const uint32_t ro_bits = E1000_EECD_PRES          |
2758                                     E1000_EECD_AUTO_RD       |
2759                                     E1000_EECD_SIZE_EX_MASK;
2760 
2761     core->mac[EECD] = (core->mac[EECD] & ro_bits) | (val & ~ro_bits);
2762 }
2763 
2764 static void
2765 e1000e_set_eerd(E1000ECore *core, int index, uint32_t val)
2766 {
2767     uint32_t addr = (val >> E1000_EERW_ADDR_SHIFT) & E1000_EERW_ADDR_MASK;
2768     uint32_t flags = 0;
2769     uint32_t data = 0;
2770 
2771     if ((addr < E1000E_EEPROM_SIZE) && (val & E1000_EERW_START)) {
2772         data = core->eeprom[addr];
2773         flags = E1000_EERW_DONE;
2774     }
2775 
2776     core->mac[EERD] = flags                           |
2777                       (addr << E1000_EERW_ADDR_SHIFT) |
2778                       (data << E1000_EERW_DATA_SHIFT);
2779 }
2780 
2781 static void
2782 e1000e_set_eewr(E1000ECore *core, int index, uint32_t val)
2783 {
2784     uint32_t addr = (val >> E1000_EERW_ADDR_SHIFT) & E1000_EERW_ADDR_MASK;
2785     uint32_t data = (val >> E1000_EERW_DATA_SHIFT) & E1000_EERW_DATA_MASK;
2786     uint32_t flags = 0;
2787 
2788     if ((addr < E1000E_EEPROM_SIZE) && (val & E1000_EERW_START)) {
2789         core->eeprom[addr] = data;
2790         flags = E1000_EERW_DONE;
2791     }
2792 
2793     core->mac[EERD] = flags                           |
2794                       (addr << E1000_EERW_ADDR_SHIFT) |
2795                       (data << E1000_EERW_DATA_SHIFT);
2796 }
2797 
2798 static void
2799 e1000e_set_rxdctl(E1000ECore *core, int index, uint32_t val)
2800 {
2801     core->mac[RXDCTL] = core->mac[RXDCTL1] = val;
2802 }
2803 
2804 static void
2805 e1000e_set_itr(E1000ECore *core, int index, uint32_t val)
2806 {
2807     uint32_t interval = val & 0xffff;
2808 
2809     trace_e1000e_irq_itr_set(val);
2810 
2811     core->itr_guest_value = interval;
2812     core->mac[index] = MAX(interval, E1000E_MIN_XITR);
2813 }
2814 
2815 static void
2816 e1000e_set_eitr(E1000ECore *core, int index, uint32_t val)
2817 {
2818     uint32_t interval = val & 0xffff;
2819     uint32_t eitr_num = index - EITR;
2820 
2821     trace_e1000e_irq_eitr_set(eitr_num, val);
2822 
2823     core->eitr_guest_value[eitr_num] = interval;
2824     core->mac[index] = MAX(interval, E1000E_MIN_XITR);
2825 }
2826 
2827 static void
2828 e1000e_set_psrctl(E1000ECore *core, int index, uint32_t val)
2829 {
2830     if (core->mac[RCTL] & E1000_RCTL_DTYP_MASK) {
2831 
2832         if ((val & E1000_PSRCTL_BSIZE0_MASK) == 0) {
2833             qemu_log_mask(LOG_GUEST_ERROR,
2834                           "e1000e: PSRCTL.BSIZE0 cannot be zero");
2835             return;
2836         }
2837 
2838         if ((val & E1000_PSRCTL_BSIZE1_MASK) == 0) {
2839             qemu_log_mask(LOG_GUEST_ERROR,
2840                           "e1000e: PSRCTL.BSIZE1 cannot be zero");
2841             return;
2842         }
2843     }
2844 
2845     core->mac[PSRCTL] = val;
2846 }
2847 
2848 static void
2849 e1000e_update_rx_offloads(E1000ECore *core)
2850 {
2851     int cso_state = e1000e_rx_l4_cso_enabled(core);
2852 
2853     trace_e1000e_rx_set_cso(cso_state);
2854 
2855     if (core->has_vnet) {
2856         qemu_set_offload(qemu_get_queue(core->owner_nic)->peer,
2857                          cso_state, 0, 0, 0, 0, 0, 0);
2858     }
2859 }
2860 
2861 static void
2862 e1000e_set_rxcsum(E1000ECore *core, int index, uint32_t val)
2863 {
2864     core->mac[RXCSUM] = val;
2865     e1000e_update_rx_offloads(core);
2866 }
2867 
2868 static void
2869 e1000e_set_gcr(E1000ECore *core, int index, uint32_t val)
2870 {
2871     uint32_t ro_bits = core->mac[GCR] & E1000_GCR_RO_BITS;
2872     core->mac[GCR] = (val & ~E1000_GCR_RO_BITS) | ro_bits;
2873 }
2874 
2875 static uint32_t e1000e_get_systiml(E1000ECore *core, int index)
2876 {
2877     e1000x_timestamp(core->mac, core->timadj, SYSTIML, SYSTIMH);
2878     return core->mac[SYSTIML];
2879 }
2880 
2881 static uint32_t e1000e_get_rxsatrh(E1000ECore *core, int index)
2882 {
2883     core->mac[TSYNCRXCTL] &= ~E1000_TSYNCRXCTL_VALID;
2884     return core->mac[RXSATRH];
2885 }
2886 
2887 static uint32_t e1000e_get_txstmph(E1000ECore *core, int index)
2888 {
2889     core->mac[TSYNCTXCTL] &= ~E1000_TSYNCTXCTL_VALID;
2890     return core->mac[TXSTMPH];
2891 }
2892 
2893 static void e1000e_set_timinca(E1000ECore *core, int index, uint32_t val)
2894 {
2895     e1000x_set_timinca(core->mac, &core->timadj, val);
2896 }
2897 
2898 static void e1000e_set_timadjh(E1000ECore *core, int index, uint32_t val)
2899 {
2900     core->mac[TIMADJH] = val;
2901     core->timadj += core->mac[TIMADJL] | ((int64_t)core->mac[TIMADJH] << 32);
2902 }
2903 
2904 #define e1000e_getreg(x)    [x] = e1000e_mac_readreg
2905 typedef uint32_t (*readops)(E1000ECore *, int);
2906 static const readops e1000e_macreg_readops[] = {
2907     e1000e_getreg(PBA),
2908     e1000e_getreg(WUFC),
2909     e1000e_getreg(MANC),
2910     e1000e_getreg(TOTL),
2911     e1000e_getreg(RDT0),
2912     e1000e_getreg(RDBAH0),
2913     e1000e_getreg(TDBAL1),
2914     e1000e_getreg(RDLEN0),
2915     e1000e_getreg(RDH1),
2916     e1000e_getreg(LATECOL),
2917     e1000e_getreg(SEQEC),
2918     e1000e_getreg(XONTXC),
2919     e1000e_getreg(AIT),
2920     e1000e_getreg(TDFH),
2921     e1000e_getreg(TDFT),
2922     e1000e_getreg(TDFHS),
2923     e1000e_getreg(TDFTS),
2924     e1000e_getreg(TDFPC),
2925     e1000e_getreg(WUS),
2926     e1000e_getreg(PBS),
2927     e1000e_getreg(RDFH),
2928     e1000e_getreg(RDFT),
2929     e1000e_getreg(RDFHS),
2930     e1000e_getreg(RDFTS),
2931     e1000e_getreg(RDFPC),
2932     e1000e_getreg(GORCL),
2933     e1000e_getreg(MGTPRC),
2934     e1000e_getreg(EERD),
2935     e1000e_getreg(EIAC),
2936     e1000e_getreg(PSRCTL),
2937     e1000e_getreg(MANC2H),
2938     e1000e_getreg(RXCSUM),
2939     e1000e_getreg(GSCL_3),
2940     e1000e_getreg(GSCN_2),
2941     e1000e_getreg(RSRPD),
2942     e1000e_getreg(RDBAL1),
2943     e1000e_getreg(FCAH),
2944     e1000e_getreg(FCRTH),
2945     e1000e_getreg(FLOP),
2946     e1000e_getreg(FLASHT),
2947     e1000e_getreg(RXSTMPH),
2948     e1000e_getreg(TXSTMPL),
2949     e1000e_getreg(TIMADJL),
2950     e1000e_getreg(TXDCTL),
2951     e1000e_getreg(RDH0),
2952     e1000e_getreg(TDT1),
2953     e1000e_getreg(TNCRS),
2954     e1000e_getreg(RJC),
2955     e1000e_getreg(IAM),
2956     e1000e_getreg(GSCL_2),
2957     e1000e_getreg(RDBAH1),
2958     e1000e_getreg(FLSWDATA),
2959     e1000e_getreg(TIPG),
2960     e1000e_getreg(FLMNGCTL),
2961     e1000e_getreg(FLMNGCNT),
2962     e1000e_getreg(TSYNCTXCTL),
2963     e1000e_getreg(EXTCNF_SIZE),
2964     e1000e_getreg(EXTCNF_CTRL),
2965     e1000e_getreg(EEMNGDATA),
2966     e1000e_getreg(CTRL_EXT),
2967     e1000e_getreg(SYSTIMH),
2968     e1000e_getreg(EEMNGCTL),
2969     e1000e_getreg(FLMNGDATA),
2970     e1000e_getreg(TSYNCRXCTL),
2971     e1000e_getreg(TDH),
2972     e1000e_getreg(LEDCTL),
2973     e1000e_getreg(TCTL),
2974     e1000e_getreg(TDBAL),
2975     e1000e_getreg(TDLEN),
2976     e1000e_getreg(TDH1),
2977     e1000e_getreg(RADV),
2978     e1000e_getreg(ECOL),
2979     e1000e_getreg(DC),
2980     e1000e_getreg(RLEC),
2981     e1000e_getreg(XOFFTXC),
2982     e1000e_getreg(RFC),
2983     e1000e_getreg(RNBC),
2984     e1000e_getreg(MGTPTC),
2985     e1000e_getreg(TIMINCA),
2986     e1000e_getreg(RXCFGL),
2987     e1000e_getreg(MFUTP01),
2988     e1000e_getreg(FACTPS),
2989     e1000e_getreg(GSCL_1),
2990     e1000e_getreg(GSCN_0),
2991     e1000e_getreg(GCR2),
2992     e1000e_getreg(RDT1),
2993     e1000e_getreg(PBACLR),
2994     e1000e_getreg(FCTTV),
2995     e1000e_getreg(EEWR),
2996     e1000e_getreg(FLSWCTL),
2997     e1000e_getreg(RXDCTL1),
2998     e1000e_getreg(RXSATRL),
2999     e1000e_getreg(RXUDP),
3000     e1000e_getreg(TORL),
3001     e1000e_getreg(TDLEN1),
3002     e1000e_getreg(MCC),
3003     e1000e_getreg(WUC),
3004     e1000e_getreg(EECD),
3005     e1000e_getreg(MFUTP23),
3006     e1000e_getreg(RAID),
3007     e1000e_getreg(FCRTV),
3008     e1000e_getreg(TXDCTL1),
3009     e1000e_getreg(RCTL),
3010     e1000e_getreg(TDT),
3011     e1000e_getreg(MDIC),
3012     e1000e_getreg(FCRUC),
3013     e1000e_getreg(VET),
3014     e1000e_getreg(RDBAL0),
3015     e1000e_getreg(TDBAH1),
3016     e1000e_getreg(RDTR),
3017     e1000e_getreg(SCC),
3018     e1000e_getreg(COLC),
3019     e1000e_getreg(CEXTERR),
3020     e1000e_getreg(XOFFRXC),
3021     e1000e_getreg(IPAV),
3022     e1000e_getreg(GOTCL),
3023     e1000e_getreg(MGTPDC),
3024     e1000e_getreg(GCR),
3025     e1000e_getreg(IVAR),
3026     e1000e_getreg(POEMB),
3027     e1000e_getreg(MFVAL),
3028     e1000e_getreg(FUNCTAG),
3029     e1000e_getreg(GSCL_4),
3030     e1000e_getreg(GSCN_3),
3031     e1000e_getreg(MRQC),
3032     e1000e_getreg(RDLEN1),
3033     e1000e_getreg(FCT),
3034     e1000e_getreg(FLA),
3035     e1000e_getreg(FLOL),
3036     e1000e_getreg(RXDCTL),
3037     e1000e_getreg(RXSTMPL),
3038     e1000e_getreg(TIMADJH),
3039     e1000e_getreg(FCRTL),
3040     e1000e_getreg(TDBAH),
3041     e1000e_getreg(TADV),
3042     e1000e_getreg(XONRXC),
3043     e1000e_getreg(TSCTFC),
3044     e1000e_getreg(RFCTL),
3045     e1000e_getreg(GSCN_1),
3046     e1000e_getreg(FCAL),
3047     e1000e_getreg(FLSWCNT),
3048 
3049     [TOTH]    = e1000e_mac_read_clr8,
3050     [GOTCH]   = e1000e_mac_read_clr8,
3051     [PRC64]   = e1000e_mac_read_clr4,
3052     [PRC255]  = e1000e_mac_read_clr4,
3053     [PRC1023] = e1000e_mac_read_clr4,
3054     [PTC64]   = e1000e_mac_read_clr4,
3055     [PTC255]  = e1000e_mac_read_clr4,
3056     [PTC1023] = e1000e_mac_read_clr4,
3057     [GPRC]    = e1000e_mac_read_clr4,
3058     [TPT]     = e1000e_mac_read_clr4,
3059     [RUC]     = e1000e_mac_read_clr4,
3060     [BPRC]    = e1000e_mac_read_clr4,
3061     [MPTC]    = e1000e_mac_read_clr4,
3062     [IAC]     = e1000e_mac_read_clr4,
3063     [ICR]     = e1000e_mac_icr_read,
3064     [STATUS]  = e1000e_get_status,
3065     [TARC0]   = e1000e_get_tarc,
3066     [ICS]     = e1000e_mac_ics_read,
3067     [TORH]    = e1000e_mac_read_clr8,
3068     [GORCH]   = e1000e_mac_read_clr8,
3069     [PRC127]  = e1000e_mac_read_clr4,
3070     [PRC511]  = e1000e_mac_read_clr4,
3071     [PRC1522] = e1000e_mac_read_clr4,
3072     [PTC127]  = e1000e_mac_read_clr4,
3073     [PTC511]  = e1000e_mac_read_clr4,
3074     [PTC1522] = e1000e_mac_read_clr4,
3075     [GPTC]    = e1000e_mac_read_clr4,
3076     [TPR]     = e1000e_mac_read_clr4,
3077     [ROC]     = e1000e_mac_read_clr4,
3078     [MPRC]    = e1000e_mac_read_clr4,
3079     [BPTC]    = e1000e_mac_read_clr4,
3080     [TSCTC]   = e1000e_mac_read_clr4,
3081     [ITR]     = e1000e_mac_itr_read,
3082     [CTRL]    = e1000e_get_ctrl,
3083     [TARC1]   = e1000e_get_tarc,
3084     [SWSM]    = e1000e_mac_swsm_read,
3085     [IMS]     = e1000e_mac_ims_read,
3086     [SYSTIML] = e1000e_get_systiml,
3087     [RXSATRH] = e1000e_get_rxsatrh,
3088     [TXSTMPH] = e1000e_get_txstmph,
3089 
3090     [CRCERRS ... MPC]      = e1000e_mac_readreg,
3091     [IP6AT ... IP6AT + 3]  = e1000e_mac_readreg,
3092     [IP4AT ... IP4AT + 6]  = e1000e_mac_readreg,
3093     [RA ... RA + 31]       = e1000e_mac_readreg,
3094     [WUPM ... WUPM + 31]   = e1000e_mac_readreg,
3095     [MTA ... MTA + E1000_MC_TBL_SIZE - 1] = e1000e_mac_readreg,
3096     [VFTA ... VFTA + E1000_VLAN_FILTER_TBL_SIZE - 1]  = e1000e_mac_readreg,
3097     [FFMT ... FFMT + 254]  = e1000e_mac_readreg,
3098     [FFVT ... FFVT + 254]  = e1000e_mac_readreg,
3099     [MDEF ... MDEF + 7]    = e1000e_mac_readreg,
3100     [FFLT ... FFLT + 10]   = e1000e_mac_readreg,
3101     [FTFT ... FTFT + 254]  = e1000e_mac_readreg,
3102     [PBM ... PBM + 10239]  = e1000e_mac_readreg,
3103     [RETA ... RETA + 31]   = e1000e_mac_readreg,
3104     [RSSRK ... RSSRK + 31] = e1000e_mac_readreg,
3105     [MAVTV0 ... MAVTV3]    = e1000e_mac_readreg,
3106     [EITR...EITR + E1000E_MSIX_VEC_NUM - 1] = e1000e_mac_eitr_read
3107 };
3108 enum { E1000E_NREADOPS = ARRAY_SIZE(e1000e_macreg_readops) };
3109 
3110 #define e1000e_putreg(x)    [x] = e1000e_mac_writereg
3111 typedef void (*writeops)(E1000ECore *, int, uint32_t);
3112 static const writeops e1000e_macreg_writeops[] = {
3113     e1000e_putreg(PBA),
3114     e1000e_putreg(SWSM),
3115     e1000e_putreg(WUFC),
3116     e1000e_putreg(RDBAH1),
3117     e1000e_putreg(TDBAH),
3118     e1000e_putreg(TXDCTL),
3119     e1000e_putreg(RDBAH0),
3120     e1000e_putreg(LEDCTL),
3121     e1000e_putreg(FCAL),
3122     e1000e_putreg(FCRUC),
3123     e1000e_putreg(WUC),
3124     e1000e_putreg(WUS),
3125     e1000e_putreg(IPAV),
3126     e1000e_putreg(TDBAH1),
3127     e1000e_putreg(IAM),
3128     e1000e_putreg(EIAC),
3129     e1000e_putreg(IVAR),
3130     e1000e_putreg(TARC0),
3131     e1000e_putreg(TARC1),
3132     e1000e_putreg(FLSWDATA),
3133     e1000e_putreg(POEMB),
3134     e1000e_putreg(MFUTP01),
3135     e1000e_putreg(MFUTP23),
3136     e1000e_putreg(MANC),
3137     e1000e_putreg(MANC2H),
3138     e1000e_putreg(MFVAL),
3139     e1000e_putreg(EXTCNF_CTRL),
3140     e1000e_putreg(FACTPS),
3141     e1000e_putreg(FUNCTAG),
3142     e1000e_putreg(GSCL_1),
3143     e1000e_putreg(GSCL_2),
3144     e1000e_putreg(GSCL_3),
3145     e1000e_putreg(GSCL_4),
3146     e1000e_putreg(GSCN_0),
3147     e1000e_putreg(GSCN_1),
3148     e1000e_putreg(GSCN_2),
3149     e1000e_putreg(GSCN_3),
3150     e1000e_putreg(GCR2),
3151     e1000e_putreg(MRQC),
3152     e1000e_putreg(FLOP),
3153     e1000e_putreg(FLOL),
3154     e1000e_putreg(FLSWCTL),
3155     e1000e_putreg(FLSWCNT),
3156     e1000e_putreg(FLA),
3157     e1000e_putreg(RXDCTL1),
3158     e1000e_putreg(TXDCTL1),
3159     e1000e_putreg(TIPG),
3160     e1000e_putreg(RXSTMPH),
3161     e1000e_putreg(RXSTMPL),
3162     e1000e_putreg(RXSATRL),
3163     e1000e_putreg(RXSATRH),
3164     e1000e_putreg(TXSTMPL),
3165     e1000e_putreg(TXSTMPH),
3166     e1000e_putreg(SYSTIML),
3167     e1000e_putreg(SYSTIMH),
3168     e1000e_putreg(TIMADJL),
3169     e1000e_putreg(RXUDP),
3170     e1000e_putreg(RXCFGL),
3171     e1000e_putreg(TSYNCRXCTL),
3172     e1000e_putreg(TSYNCTXCTL),
3173     e1000e_putreg(EXTCNF_SIZE),
3174     e1000e_putreg(EEMNGCTL),
3175     e1000e_putreg(RA),
3176 
3177     [TDH1]     = e1000e_set_16bit,
3178     [TDT1]     = e1000e_set_tdt,
3179     [TCTL]     = e1000e_set_tctl,
3180     [TDT]      = e1000e_set_tdt,
3181     [MDIC]     = e1000e_set_mdic,
3182     [ICS]      = e1000e_set_ics,
3183     [TDH]      = e1000e_set_16bit,
3184     [RDH0]     = e1000e_set_16bit,
3185     [RDT0]     = e1000e_set_rdt,
3186     [IMC]      = e1000e_set_imc,
3187     [IMS]      = e1000e_set_ims,
3188     [ICR]      = e1000e_set_icr,
3189     [EECD]     = e1000e_set_eecd,
3190     [RCTL]     = e1000e_set_rx_control,
3191     [CTRL]     = e1000e_set_ctrl,
3192     [RDTR]     = e1000e_set_rdtr,
3193     [RADV]     = e1000e_set_16bit,
3194     [TADV]     = e1000e_set_16bit,
3195     [ITR]      = e1000e_set_itr,
3196     [EERD]     = e1000e_set_eerd,
3197     [AIT]      = e1000e_set_16bit,
3198     [TDFH]     = e1000e_set_13bit,
3199     [TDFT]     = e1000e_set_13bit,
3200     [TDFHS]    = e1000e_set_13bit,
3201     [TDFTS]    = e1000e_set_13bit,
3202     [TDFPC]    = e1000e_set_13bit,
3203     [RDFH]     = e1000e_set_13bit,
3204     [RDFHS]    = e1000e_set_13bit,
3205     [RDFT]     = e1000e_set_13bit,
3206     [RDFTS]    = e1000e_set_13bit,
3207     [RDFPC]    = e1000e_set_13bit,
3208     [PBS]      = e1000e_set_6bit,
3209     [GCR]      = e1000e_set_gcr,
3210     [PSRCTL]   = e1000e_set_psrctl,
3211     [RXCSUM]   = e1000e_set_rxcsum,
3212     [RAID]     = e1000e_set_16bit,
3213     [RSRPD]    = e1000e_set_12bit,
3214     [TIDV]     = e1000e_set_tidv,
3215     [TDLEN1]   = e1000e_set_dlen,
3216     [TDLEN]    = e1000e_set_dlen,
3217     [RDLEN0]   = e1000e_set_dlen,
3218     [RDLEN1]   = e1000e_set_dlen,
3219     [TDBAL]    = e1000e_set_dbal,
3220     [TDBAL1]   = e1000e_set_dbal,
3221     [RDBAL0]   = e1000e_set_dbal,
3222     [RDBAL1]   = e1000e_set_dbal,
3223     [RDH1]     = e1000e_set_16bit,
3224     [RDT1]     = e1000e_set_rdt,
3225     [STATUS]   = e1000e_set_status,
3226     [PBACLR]   = e1000e_set_pbaclr,
3227     [CTRL_EXT] = e1000e_set_ctrlext,
3228     [FCAH]     = e1000e_set_16bit,
3229     [FCT]      = e1000e_set_16bit,
3230     [FCTTV]    = e1000e_set_16bit,
3231     [FCRTV]    = e1000e_set_16bit,
3232     [FCRTH]    = e1000e_set_fcrth,
3233     [FCRTL]    = e1000e_set_fcrtl,
3234     [VET]      = e1000e_set_vet,
3235     [RXDCTL]   = e1000e_set_rxdctl,
3236     [FLASHT]   = e1000e_set_16bit,
3237     [EEWR]     = e1000e_set_eewr,
3238     [CTRL_DUP] = e1000e_set_ctrl,
3239     [RFCTL]    = e1000e_set_rfctl,
3240     [RA + 1]   = e1000e_mac_setmacaddr,
3241     [TIMINCA]  = e1000e_set_timinca,
3242     [TIMADJH]  = e1000e_set_timadjh,
3243 
3244     [IP6AT ... IP6AT + 3]    = e1000e_mac_writereg,
3245     [IP4AT ... IP4AT + 6]    = e1000e_mac_writereg,
3246     [RA + 2 ... RA + 31]     = e1000e_mac_writereg,
3247     [WUPM ... WUPM + 31]     = e1000e_mac_writereg,
3248     [MTA ... MTA + E1000_MC_TBL_SIZE - 1] = e1000e_mac_writereg,
3249     [VFTA ... VFTA + E1000_VLAN_FILTER_TBL_SIZE - 1]    = e1000e_mac_writereg,
3250     [FFMT ... FFMT + 254]    = e1000e_set_4bit,
3251     [FFVT ... FFVT + 254]    = e1000e_mac_writereg,
3252     [PBM ... PBM + 10239]    = e1000e_mac_writereg,
3253     [MDEF ... MDEF + 7]      = e1000e_mac_writereg,
3254     [FFLT ... FFLT + 10]     = e1000e_set_11bit,
3255     [FTFT ... FTFT + 254]    = e1000e_mac_writereg,
3256     [RETA ... RETA + 31]     = e1000e_mac_writereg,
3257     [RSSRK ... RSSRK + 31]   = e1000e_mac_writereg,
3258     [MAVTV0 ... MAVTV3]      = e1000e_mac_writereg,
3259     [EITR...EITR + E1000E_MSIX_VEC_NUM - 1] = e1000e_set_eitr
3260 };
3261 enum { E1000E_NWRITEOPS = ARRAY_SIZE(e1000e_macreg_writeops) };
3262 
3263 enum { MAC_ACCESS_PARTIAL = 1 };
3264 
3265 /*
3266  * The array below combines alias offsets of the index values for the
3267  * MAC registers that have aliases, with the indication of not fully
3268  * implemented registers (lowest bit). This combination is possible
3269  * because all of the offsets are even.
3270  */
3271 static const uint16_t mac_reg_access[E1000E_MAC_SIZE] = {
3272     /* Alias index offsets */
3273     [FCRTL_A] = 0x07fe, [FCRTH_A] = 0x0802,
3274     [RDH0_A]  = 0x09bc, [RDT0_A]  = 0x09bc, [RDTR_A] = 0x09c6,
3275     [RDFH_A]  = 0xe904, [RDFT_A]  = 0xe904,
3276     [TDH_A]   = 0x0cf8, [TDT_A]   = 0x0cf8, [TIDV_A] = 0x0cf8,
3277     [TDFH_A]  = 0xed00, [TDFT_A]  = 0xed00,
3278     [RA_A ... RA_A + 31]      = 0x14f0,
3279     [VFTA_A ... VFTA_A + E1000_VLAN_FILTER_TBL_SIZE - 1] = 0x1400,
3280     [RDBAL0_A ... RDLEN0_A] = 0x09bc,
3281     [TDBAL_A ... TDLEN_A]   = 0x0cf8,
3282     /* Access options */
3283     [RDFH]  = MAC_ACCESS_PARTIAL,    [RDFT]  = MAC_ACCESS_PARTIAL,
3284     [RDFHS] = MAC_ACCESS_PARTIAL,    [RDFTS] = MAC_ACCESS_PARTIAL,
3285     [RDFPC] = MAC_ACCESS_PARTIAL,
3286     [TDFH]  = MAC_ACCESS_PARTIAL,    [TDFT]  = MAC_ACCESS_PARTIAL,
3287     [TDFHS] = MAC_ACCESS_PARTIAL,    [TDFTS] = MAC_ACCESS_PARTIAL,
3288     [TDFPC] = MAC_ACCESS_PARTIAL,    [EECD]  = MAC_ACCESS_PARTIAL,
3289     [PBM]   = MAC_ACCESS_PARTIAL,    [FLA]   = MAC_ACCESS_PARTIAL,
3290     [FCAL]  = MAC_ACCESS_PARTIAL,    [FCAH]  = MAC_ACCESS_PARTIAL,
3291     [FCT]   = MAC_ACCESS_PARTIAL,    [FCTTV] = MAC_ACCESS_PARTIAL,
3292     [FCRTV] = MAC_ACCESS_PARTIAL,    [FCRTL] = MAC_ACCESS_PARTIAL,
3293     [FCRTH] = MAC_ACCESS_PARTIAL,    [TXDCTL] = MAC_ACCESS_PARTIAL,
3294     [TXDCTL1] = MAC_ACCESS_PARTIAL,
3295     [MAVTV0 ... MAVTV3] = MAC_ACCESS_PARTIAL
3296 };
3297 
3298 void
3299 e1000e_core_write(E1000ECore *core, hwaddr addr, uint64_t val, unsigned size)
3300 {
3301     uint16_t index = e1000e_get_reg_index_with_offset(mac_reg_access, addr);
3302 
3303     if (index < E1000E_NWRITEOPS && e1000e_macreg_writeops[index]) {
3304         if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
3305             trace_e1000e_wrn_regs_write_trivial(index << 2);
3306         }
3307         trace_e1000e_core_write(index << 2, size, val);
3308         e1000e_macreg_writeops[index](core, index, val);
3309     } else if (index < E1000E_NREADOPS && e1000e_macreg_readops[index]) {
3310         trace_e1000e_wrn_regs_write_ro(index << 2, size, val);
3311     } else {
3312         trace_e1000e_wrn_regs_write_unknown(index << 2, size, val);
3313     }
3314 }
3315 
3316 uint64_t
3317 e1000e_core_read(E1000ECore *core, hwaddr addr, unsigned size)
3318 {
3319     uint64_t val;
3320     uint16_t index = e1000e_get_reg_index_with_offset(mac_reg_access, addr);
3321 
3322     if (index < E1000E_NREADOPS && e1000e_macreg_readops[index]) {
3323         if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
3324             trace_e1000e_wrn_regs_read_trivial(index << 2);
3325         }
3326         val = e1000e_macreg_readops[index](core, index);
3327         trace_e1000e_core_read(index << 2, size, val);
3328         return val;
3329     } else {
3330         trace_e1000e_wrn_regs_read_unknown(index << 2, size);
3331     }
3332     return 0;
3333 }
3334 
3335 static inline void
3336 e1000e_autoneg_pause(E1000ECore *core)
3337 {
3338     timer_del(core->autoneg_timer);
3339 }
3340 
3341 static void
3342 e1000e_autoneg_resume(E1000ECore *core)
3343 {
3344     if (e1000e_have_autoneg(core) &&
3345         !(core->phy[0][MII_BMSR] & MII_BMSR_AN_COMP)) {
3346         qemu_get_queue(core->owner_nic)->link_down = false;
3347         timer_mod(core->autoneg_timer,
3348                   qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
3349     }
3350 }
3351 
3352 static void
3353 e1000e_vm_state_change(void *opaque, bool running, RunState state)
3354 {
3355     E1000ECore *core = opaque;
3356 
3357     if (running) {
3358         trace_e1000e_vm_state_running();
3359         e1000e_intrmgr_resume(core);
3360         e1000e_autoneg_resume(core);
3361     } else {
3362         trace_e1000e_vm_state_stopped();
3363         e1000e_autoneg_pause(core);
3364         e1000e_intrmgr_pause(core);
3365     }
3366 }
3367 
3368 void
3369 e1000e_core_pci_realize(E1000ECore     *core,
3370                         const uint16_t *eeprom_templ,
3371                         uint32_t        eeprom_size,
3372                         const uint8_t  *macaddr)
3373 {
3374     int i;
3375 
3376     core->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
3377                                        e1000e_autoneg_timer, core);
3378     e1000e_intrmgr_pci_realize(core);
3379 
3380     core->vmstate =
3381         qemu_add_vm_change_state_handler(e1000e_vm_state_change, core);
3382 
3383     for (i = 0; i < E1000E_NUM_QUEUES; i++) {
3384         net_tx_pkt_init(&core->tx[i].tx_pkt, E1000E_MAX_TX_FRAGS);
3385     }
3386 
3387     net_rx_pkt_init(&core->rx_pkt);
3388 
3389     e1000x_core_prepare_eeprom(core->eeprom,
3390                                eeprom_templ,
3391                                eeprom_size,
3392                                PCI_DEVICE_GET_CLASS(core->owner)->device_id,
3393                                macaddr);
3394     e1000e_update_rx_offloads(core);
3395 }
3396 
3397 void
3398 e1000e_core_pci_uninit(E1000ECore *core)
3399 {
3400     int i;
3401 
3402     timer_free(core->autoneg_timer);
3403 
3404     e1000e_intrmgr_pci_unint(core);
3405 
3406     qemu_del_vm_change_state_handler(core->vmstate);
3407 
3408     for (i = 0; i < E1000E_NUM_QUEUES; i++) {
3409         net_tx_pkt_uninit(core->tx[i].tx_pkt);
3410     }
3411 
3412     net_rx_pkt_uninit(core->rx_pkt);
3413 }
3414 
3415 static const uint16_t
3416 e1000e_phy_reg_init[E1000E_PHY_PAGES][E1000E_PHY_PAGE_SIZE] = {
3417     [0] = {
3418         [MII_BMCR] = MII_BMCR_SPEED1000 |
3419                      MII_BMCR_FD        |
3420                      MII_BMCR_AUTOEN,
3421 
3422         [MII_BMSR] = MII_BMSR_EXTCAP    |
3423                      MII_BMSR_LINK_ST   |
3424                      MII_BMSR_AUTONEG   |
3425                      MII_BMSR_MFPS      |
3426                      MII_BMSR_EXTSTAT   |
3427                      MII_BMSR_10T_HD    |
3428                      MII_BMSR_10T_FD    |
3429                      MII_BMSR_100TX_HD  |
3430                      MII_BMSR_100TX_FD,
3431 
3432         [MII_PHYID1]            = 0x141,
3433         [MII_PHYID2]            = E1000_PHY_ID2_82574x,
3434         [MII_ANAR]              = MII_ANAR_CSMACD | MII_ANAR_10 |
3435                                   MII_ANAR_10FD | MII_ANAR_TX |
3436                                   MII_ANAR_TXFD | MII_ANAR_PAUSE |
3437                                   MII_ANAR_PAUSE_ASYM,
3438         [MII_ANLPAR]            = MII_ANLPAR_10 | MII_ANLPAR_10FD |
3439                                   MII_ANLPAR_TX | MII_ANLPAR_TXFD |
3440                                   MII_ANLPAR_T4 | MII_ANLPAR_PAUSE,
3441         [MII_ANER]              = MII_ANER_NP | MII_ANER_NWAY,
3442         [MII_ANNP]              = 1 | MII_ANNP_MP,
3443         [MII_CTRL1000]          = MII_CTRL1000_HALF | MII_CTRL1000_FULL |
3444                                   MII_CTRL1000_PORT | MII_CTRL1000_MASTER,
3445         [MII_STAT1000]          = MII_STAT1000_HALF | MII_STAT1000_FULL |
3446                                   MII_STAT1000_ROK | MII_STAT1000_LOK,
3447         [MII_EXTSTAT]           = MII_EXTSTAT_1000T_HD | MII_EXTSTAT_1000T_FD,
3448 
3449         [PHY_COPPER_CTRL1]      = BIT(5) | BIT(6) | BIT(8) | BIT(9) |
3450                                   BIT(12) | BIT(13),
3451         [PHY_COPPER_STAT1]      = BIT(3) | BIT(10) | BIT(11) | BIT(13) | BIT(15)
3452     },
3453     [2] = {
3454         [PHY_MAC_CTRL1]         = BIT(3) | BIT(7),
3455         [PHY_MAC_CTRL2]         = BIT(1) | BIT(2) | BIT(6) | BIT(12)
3456     },
3457     [3] = {
3458         [PHY_LED_TIMER_CTRL]    = BIT(0) | BIT(2) | BIT(14)
3459     }
3460 };
3461 
3462 static const uint32_t e1000e_mac_reg_init[] = {
3463     [PBA]           =     0x00140014,
3464     [LEDCTL]        =  BIT(1) | BIT(8) | BIT(9) | BIT(15) | BIT(17) | BIT(18),
3465     [EXTCNF_CTRL]   = BIT(3),
3466     [EEMNGCTL]      = BIT(31),
3467     [FLASHT]        = 0x2,
3468     [FLSWCTL]       = BIT(30) | BIT(31),
3469     [FLOL]          = BIT(0),
3470     [RXDCTL]        = BIT(16),
3471     [RXDCTL1]       = BIT(16),
3472     [TIPG]          = 0x8 | (0x8 << 10) | (0x6 << 20),
3473     [RXCFGL]        = 0x88F7,
3474     [RXUDP]         = 0x319,
3475     [CTRL]          = E1000_CTRL_FD | E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN0 |
3476                       E1000_CTRL_SPD_1000 | E1000_CTRL_SLU |
3477                       E1000_CTRL_ADVD3WUC,
3478     [STATUS]        =  E1000_STATUS_ASDV_1000 | E1000_STATUS_LU,
3479     [PSRCTL]        = (2 << E1000_PSRCTL_BSIZE0_SHIFT) |
3480                       (4 << E1000_PSRCTL_BSIZE1_SHIFT) |
3481                       (4 << E1000_PSRCTL_BSIZE2_SHIFT),
3482     [TARC0]         = 0x3 | E1000_TARC_ENABLE,
3483     [TARC1]         = 0x3 | E1000_TARC_ENABLE,
3484     [EECD]          = E1000_EECD_AUTO_RD | E1000_EECD_PRES,
3485     [EERD]          = E1000_EERW_DONE,
3486     [EEWR]          = E1000_EERW_DONE,
3487     [GCR]           = E1000_L0S_ADJUST |
3488                       E1000_L1_ENTRY_LATENCY_MSB |
3489                       E1000_L1_ENTRY_LATENCY_LSB,
3490     [TDFH]          = 0x600,
3491     [TDFT]          = 0x600,
3492     [TDFHS]         = 0x600,
3493     [TDFTS]         = 0x600,
3494     [POEMB]         = 0x30D,
3495     [PBS]           = 0x028,
3496     [MANC]          = E1000_MANC_DIS_IP_CHK_ARP,
3497     [FACTPS]        = E1000_FACTPS_LAN0_ON | 0x20000000,
3498     [SWSM]          = 1,
3499     [RXCSUM]        = E1000_RXCSUM_IPOFLD | E1000_RXCSUM_TUOFLD,
3500     [ITR]           = E1000E_MIN_XITR,
3501     [EITR...EITR + E1000E_MSIX_VEC_NUM - 1] = E1000E_MIN_XITR,
3502 };
3503 
3504 static void e1000e_reset(E1000ECore *core, bool sw)
3505 {
3506     int i;
3507 
3508     timer_del(core->autoneg_timer);
3509 
3510     e1000e_intrmgr_reset(core);
3511 
3512     memset(core->phy, 0, sizeof core->phy);
3513     memcpy(core->phy, e1000e_phy_reg_init, sizeof e1000e_phy_reg_init);
3514 
3515     for (i = 0; i < E1000E_MAC_SIZE; i++) {
3516         if (sw && (i == PBA || i == PBS || i == FLA)) {
3517             continue;
3518         }
3519 
3520         core->mac[i] = i < ARRAY_SIZE(e1000e_mac_reg_init) ?
3521                        e1000e_mac_reg_init[i] : 0;
3522     }
3523 
3524     core->rxbuf_min_shift = 1 + E1000_RING_DESC_LEN_SHIFT;
3525 
3526     if (qemu_get_queue(core->owner_nic)->link_down) {
3527         e1000e_link_down(core);
3528     }
3529 
3530     e1000x_reset_mac_addr(core->owner_nic, core->mac, core->permanent_mac);
3531 
3532     for (i = 0; i < ARRAY_SIZE(core->tx); i++) {
3533         memset(&core->tx[i].props, 0, sizeof(core->tx[i].props));
3534         core->tx[i].skip_cp = false;
3535     }
3536 }
3537 
3538 void
3539 e1000e_core_reset(E1000ECore *core)
3540 {
3541     e1000e_reset(core, false);
3542 }
3543 
3544 void e1000e_core_pre_save(E1000ECore *core)
3545 {
3546     int i;
3547     NetClientState *nc = qemu_get_queue(core->owner_nic);
3548 
3549     /*
3550      * If link is down and auto-negotiation is supported and ongoing,
3551      * complete auto-negotiation immediately. This allows us to look
3552      * at MII_BMSR_AN_COMP to infer link status on load.
3553      */
3554     if (nc->link_down && e1000e_have_autoneg(core)) {
3555         core->phy[0][MII_BMSR] |= MII_BMSR_AN_COMP;
3556         e1000e_update_flowctl_status(core);
3557     }
3558 
3559     for (i = 0; i < ARRAY_SIZE(core->tx); i++) {
3560         if (net_tx_pkt_has_fragments(core->tx[i].tx_pkt)) {
3561             core->tx[i].skip_cp = true;
3562         }
3563     }
3564 }
3565 
3566 int
3567 e1000e_core_post_load(E1000ECore *core)
3568 {
3569     NetClientState *nc = qemu_get_queue(core->owner_nic);
3570 
3571     /*
3572      * nc.link_down can't be migrated, so infer link_down according
3573      * to link status bit in core.mac[STATUS].
3574      */
3575     nc->link_down = (core->mac[STATUS] & E1000_STATUS_LU) == 0;
3576 
3577     return 0;
3578 }
3579