xref: /openbmc/qemu/hw/net/igb_core.c (revision 6aa262f8e39d2fcb5c0088badc6ee930e52bb36c)
1 /*
2  * Core code for QEMU igb emulation
3  *
4  * Datasheet:
5  * https://www.intel.com/content/dam/www/public/us/en/documents/datasheets/82576eg-gbe-datasheet.pdf
6  *
7  * Copyright (c) 2020-2023 Red Hat, Inc.
8  * Copyright (c) 2015 Ravello Systems LTD (http://ravellosystems.com)
9  * Developed by Daynix Computing LTD (http://www.daynix.com)
10  *
11  * Authors:
12  * Akihiko Odaki <akihiko.odaki@daynix.com>
13  * Gal Hammmer <gal.hammer@sap.com>
14  * Marcel Apfelbaum <marcel.apfelbaum@gmail.com>
15  * Dmitry Fleytman <dmitry@daynix.com>
16  * Leonid Bloch <leonid@daynix.com>
17  * Yan Vugenfirer <yan@daynix.com>
18  *
19  * Based on work done by:
20  * Nir Peleg, Tutis Systems Ltd. for Qumranet Inc.
21  * Copyright (c) 2008 Qumranet
22  * Based on work done by:
23  * Copyright (c) 2007 Dan Aloni
24  * Copyright (c) 2004 Antony T Curtis
25  *
26  * This library is free software; you can redistribute it and/or
27  * modify it under the terms of the GNU Lesser General Public
28  * License as published by the Free Software Foundation; either
29  * version 2.1 of the License, or (at your option) any later version.
30  *
31  * This library is distributed in the hope that it will be useful,
32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
34  * Lesser General Public License for more details.
35  *
36  * You should have received a copy of the GNU Lesser General Public
37  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
38  */
39 
40 #include "qemu/osdep.h"
41 #include "qemu/log.h"
42 #include "net/net.h"
43 #include "net/tap.h"
44 #include "hw/net/mii.h"
45 #include "hw/pci/msi.h"
46 #include "hw/pci/msix.h"
47 #include "sysemu/runstate.h"
48 
49 #include "net_tx_pkt.h"
50 #include "net_rx_pkt.h"
51 
52 #include "igb_common.h"
53 #include "e1000x_common.h"
54 #include "igb_core.h"
55 
56 #include "trace.h"
57 
58 #define E1000E_MAX_TX_FRAGS (64)
59 
60 union e1000_rx_desc_union {
61     struct e1000_rx_desc legacy;
62     union e1000_adv_rx_desc adv;
63 };
64 
65 typedef struct IGBTxPktVmdqCallbackContext {
66     IGBCore *core;
67     NetClientState *nc;
68 } IGBTxPktVmdqCallbackContext;
69 
70 typedef struct L2Header {
71     struct eth_header eth;
72     struct vlan_header vlan[2];
73 } L2Header;
74 
75 static ssize_t
76 igb_receive_internal(IGBCore *core, const struct iovec *iov, int iovcnt,
77                      bool has_vnet, bool *external_tx);
78 
79 static inline void
80 igb_set_interrupt_cause(IGBCore *core, uint32_t val);
81 
82 static void igb_update_interrupt_state(IGBCore *core);
83 static void igb_reset(IGBCore *core, bool sw);
84 
85 static inline void
86 igb_raise_legacy_irq(IGBCore *core)
87 {
88     trace_e1000e_irq_legacy_notify(true);
89     e1000x_inc_reg_if_not_full(core->mac, IAC);
90     pci_set_irq(core->owner, 1);
91 }
92 
93 static inline void
94 igb_lower_legacy_irq(IGBCore *core)
95 {
96     trace_e1000e_irq_legacy_notify(false);
97     pci_set_irq(core->owner, 0);
98 }
99 
100 static void igb_msix_notify(IGBCore *core, unsigned int cause)
101 {
102     PCIDevice *dev = core->owner;
103     uint16_t vfn;
104     uint32_t effective_eiac;
105     unsigned int vector;
106 
107     vfn = 8 - (cause + 2) / IGBVF_MSIX_VEC_NUM;
108     if (vfn < pcie_sriov_num_vfs(core->owner)) {
109         dev = pcie_sriov_get_vf_at_index(core->owner, vfn);
110         assert(dev);
111         vector = (cause + 2) % IGBVF_MSIX_VEC_NUM;
112     } else if (cause >= IGB_MSIX_VEC_NUM) {
113         qemu_log_mask(LOG_GUEST_ERROR,
114                       "igb: Tried to use vector unavailable for PF");
115         return;
116     } else {
117         vector = cause;
118     }
119 
120     msix_notify(dev, vector);
121 
122     trace_e1000e_irq_icr_clear_eiac(core->mac[EICR], core->mac[EIAC]);
123     effective_eiac = core->mac[EIAC] & BIT(cause);
124     core->mac[EICR] &= ~effective_eiac;
125 }
126 
127 static inline void
128 igb_intrmgr_rearm_timer(IGBIntrDelayTimer *timer)
129 {
130     int64_t delay_ns = (int64_t) timer->core->mac[timer->delay_reg] *
131                                  timer->delay_resolution_ns;
132 
133     trace_e1000e_irq_rearm_timer(timer->delay_reg << 2, delay_ns);
134 
135     timer_mod(timer->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + delay_ns);
136 
137     timer->running = true;
138 }
139 
140 static void
141 igb_intmgr_timer_resume(IGBIntrDelayTimer *timer)
142 {
143     if (timer->running) {
144         igb_intrmgr_rearm_timer(timer);
145     }
146 }
147 
148 static void
149 igb_intmgr_timer_pause(IGBIntrDelayTimer *timer)
150 {
151     if (timer->running) {
152         timer_del(timer->timer);
153     }
154 }
155 
156 static void
157 igb_intrmgr_on_msix_throttling_timer(void *opaque)
158 {
159     IGBIntrDelayTimer *timer = opaque;
160     int idx = timer - &timer->core->eitr[0];
161 
162     timer->running = false;
163 
164     trace_e1000e_irq_msix_notify_postponed_vec(idx);
165     igb_msix_notify(timer->core, idx);
166 }
167 
168 static void
169 igb_intrmgr_initialize_all_timers(IGBCore *core, bool create)
170 {
171     int i;
172 
173     for (i = 0; i < IGB_INTR_NUM; i++) {
174         core->eitr[i].core = core;
175         core->eitr[i].delay_reg = EITR0 + i;
176         core->eitr[i].delay_resolution_ns = E1000_INTR_DELAY_NS_RES;
177     }
178 
179     if (!create) {
180         return;
181     }
182 
183     for (i = 0; i < IGB_INTR_NUM; i++) {
184         core->eitr[i].timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
185                                            igb_intrmgr_on_msix_throttling_timer,
186                                            &core->eitr[i]);
187     }
188 }
189 
190 static void
191 igb_intrmgr_resume(IGBCore *core)
192 {
193     int i;
194 
195     for (i = 0; i < IGB_INTR_NUM; i++) {
196         igb_intmgr_timer_resume(&core->eitr[i]);
197     }
198 }
199 
200 static void
201 igb_intrmgr_pause(IGBCore *core)
202 {
203     int i;
204 
205     for (i = 0; i < IGB_INTR_NUM; i++) {
206         igb_intmgr_timer_pause(&core->eitr[i]);
207     }
208 }
209 
210 static void
211 igb_intrmgr_reset(IGBCore *core)
212 {
213     int i;
214 
215     for (i = 0; i < IGB_INTR_NUM; i++) {
216         if (core->eitr[i].running) {
217             timer_del(core->eitr[i].timer);
218             igb_intrmgr_on_msix_throttling_timer(&core->eitr[i]);
219         }
220     }
221 }
222 
223 static void
224 igb_intrmgr_pci_unint(IGBCore *core)
225 {
226     int i;
227 
228     for (i = 0; i < IGB_INTR_NUM; i++) {
229         timer_free(core->eitr[i].timer);
230     }
231 }
232 
233 static void
234 igb_intrmgr_pci_realize(IGBCore *core)
235 {
236     igb_intrmgr_initialize_all_timers(core, true);
237 }
238 
239 static inline bool
240 igb_rx_csum_enabled(IGBCore *core)
241 {
242     return (core->mac[RXCSUM] & E1000_RXCSUM_PCSD) ? false : true;
243 }
244 
245 static inline bool
246 igb_rx_use_legacy_descriptor(IGBCore *core)
247 {
248     /*
249      * TODO: If SRRCTL[n],DESCTYPE = 000b, the 82576 uses the legacy Rx
250      * descriptor.
251      */
252     return false;
253 }
254 
255 static inline bool
256 igb_rss_enabled(IGBCore *core)
257 {
258     return (core->mac[MRQC] & 3) == E1000_MRQC_ENABLE_RSS_MQ &&
259            !igb_rx_csum_enabled(core) &&
260            !igb_rx_use_legacy_descriptor(core);
261 }
262 
263 typedef struct E1000E_RSSInfo_st {
264     bool enabled;
265     uint32_t hash;
266     uint32_t queue;
267     uint32_t type;
268 } E1000E_RSSInfo;
269 
270 static uint32_t
271 igb_rss_get_hash_type(IGBCore *core, struct NetRxPkt *pkt)
272 {
273     bool hasip4, hasip6;
274     EthL4HdrProto l4hdr_proto;
275 
276     assert(igb_rss_enabled(core));
277 
278     net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto);
279 
280     if (hasip4) {
281         trace_e1000e_rx_rss_ip4(l4hdr_proto, core->mac[MRQC],
282                                 E1000_MRQC_EN_TCPIPV4(core->mac[MRQC]),
283                                 E1000_MRQC_EN_IPV4(core->mac[MRQC]));
284 
285         if (l4hdr_proto == ETH_L4_HDR_PROTO_TCP &&
286             E1000_MRQC_EN_TCPIPV4(core->mac[MRQC])) {
287             return E1000_MRQ_RSS_TYPE_IPV4TCP;
288         }
289 
290         if (l4hdr_proto == ETH_L4_HDR_PROTO_UDP &&
291             (core->mac[MRQC] & E1000_MRQC_RSS_FIELD_IPV4_UDP)) {
292             return E1000_MRQ_RSS_TYPE_IPV4UDP;
293         }
294 
295         if (E1000_MRQC_EN_IPV4(core->mac[MRQC])) {
296             return E1000_MRQ_RSS_TYPE_IPV4;
297         }
298     } else if (hasip6) {
299         eth_ip6_hdr_info *ip6info = net_rx_pkt_get_ip6_info(pkt);
300 
301         bool ex_dis = core->mac[RFCTL] & E1000_RFCTL_IPV6_EX_DIS;
302         bool new_ex_dis = core->mac[RFCTL] & E1000_RFCTL_NEW_IPV6_EXT_DIS;
303 
304         /*
305          * Following two traces must not be combined because resulting
306          * event will have 11 arguments totally and some trace backends
307          * (at least "ust") have limitation of maximum 10 arguments per
308          * event. Events with more arguments fail to compile for
309          * backends like these.
310          */
311         trace_e1000e_rx_rss_ip6_rfctl(core->mac[RFCTL]);
312         trace_e1000e_rx_rss_ip6(ex_dis, new_ex_dis, l4hdr_proto,
313                                 ip6info->has_ext_hdrs,
314                                 ip6info->rss_ex_dst_valid,
315                                 ip6info->rss_ex_src_valid,
316                                 core->mac[MRQC],
317                                 E1000_MRQC_EN_TCPIPV6EX(core->mac[MRQC]),
318                                 E1000_MRQC_EN_IPV6EX(core->mac[MRQC]),
319                                 E1000_MRQC_EN_IPV6(core->mac[MRQC]));
320 
321         if ((!ex_dis || !ip6info->has_ext_hdrs) &&
322             (!new_ex_dis || !(ip6info->rss_ex_dst_valid ||
323                               ip6info->rss_ex_src_valid))) {
324 
325             if (l4hdr_proto == ETH_L4_HDR_PROTO_TCP &&
326                 E1000_MRQC_EN_TCPIPV6EX(core->mac[MRQC])) {
327                 return E1000_MRQ_RSS_TYPE_IPV6TCPEX;
328             }
329 
330             if (l4hdr_proto == ETH_L4_HDR_PROTO_UDP &&
331                 (core->mac[MRQC] & E1000_MRQC_RSS_FIELD_IPV6_UDP)) {
332                 return E1000_MRQ_RSS_TYPE_IPV6UDP;
333             }
334 
335             if (E1000_MRQC_EN_IPV6EX(core->mac[MRQC])) {
336                 return E1000_MRQ_RSS_TYPE_IPV6EX;
337             }
338 
339         }
340 
341         if (E1000_MRQC_EN_IPV6(core->mac[MRQC])) {
342             return E1000_MRQ_RSS_TYPE_IPV6;
343         }
344 
345     }
346 
347     return E1000_MRQ_RSS_TYPE_NONE;
348 }
349 
350 static uint32_t
351 igb_rss_calc_hash(IGBCore *core, struct NetRxPkt *pkt, E1000E_RSSInfo *info)
352 {
353     NetRxPktRssType type;
354 
355     assert(igb_rss_enabled(core));
356 
357     switch (info->type) {
358     case E1000_MRQ_RSS_TYPE_IPV4:
359         type = NetPktRssIpV4;
360         break;
361     case E1000_MRQ_RSS_TYPE_IPV4TCP:
362         type = NetPktRssIpV4Tcp;
363         break;
364     case E1000_MRQ_RSS_TYPE_IPV6TCPEX:
365         type = NetPktRssIpV6TcpEx;
366         break;
367     case E1000_MRQ_RSS_TYPE_IPV6:
368         type = NetPktRssIpV6;
369         break;
370     case E1000_MRQ_RSS_TYPE_IPV6EX:
371         type = NetPktRssIpV6Ex;
372         break;
373     case E1000_MRQ_RSS_TYPE_IPV4UDP:
374         type = NetPktRssIpV4Udp;
375         break;
376     case E1000_MRQ_RSS_TYPE_IPV6UDP:
377         type = NetPktRssIpV6Udp;
378         break;
379     default:
380         assert(false);
381         return 0;
382     }
383 
384     return net_rx_pkt_calc_rss_hash(pkt, type, (uint8_t *) &core->mac[RSSRK]);
385 }
386 
387 static void
388 igb_rss_parse_packet(IGBCore *core, struct NetRxPkt *pkt, bool tx,
389                      E1000E_RSSInfo *info)
390 {
391     trace_e1000e_rx_rss_started();
392 
393     if (tx || !igb_rss_enabled(core)) {
394         info->enabled = false;
395         info->hash = 0;
396         info->queue = 0;
397         info->type = 0;
398         trace_e1000e_rx_rss_disabled();
399         return;
400     }
401 
402     info->enabled = true;
403 
404     info->type = igb_rss_get_hash_type(core, pkt);
405 
406     trace_e1000e_rx_rss_type(info->type);
407 
408     if (info->type == E1000_MRQ_RSS_TYPE_NONE) {
409         info->hash = 0;
410         info->queue = 0;
411         return;
412     }
413 
414     info->hash = igb_rss_calc_hash(core, pkt, info);
415     info->queue = E1000_RSS_QUEUE(&core->mac[RETA], info->hash);
416 }
417 
418 static void
419 igb_tx_insert_vlan(IGBCore *core, uint16_t qn, struct igb_tx *tx,
420     uint16_t vlan, bool insert_vlan)
421 {
422     if (core->mac[MRQC] & 1) {
423         uint16_t pool = qn % IGB_NUM_VM_POOLS;
424 
425         if (core->mac[VMVIR0 + pool] & E1000_VMVIR_VLANA_DEFAULT) {
426             /* always insert default VLAN */
427             insert_vlan = true;
428             vlan = core->mac[VMVIR0 + pool] & 0xffff;
429         } else if (core->mac[VMVIR0 + pool] & E1000_VMVIR_VLANA_NEVER) {
430             insert_vlan = false;
431         }
432     }
433 
434     if (insert_vlan) {
435         net_tx_pkt_setup_vlan_header_ex(tx->tx_pkt, vlan,
436             core->mac[VET] & 0xffff);
437     }
438 }
439 
440 static bool
441 igb_setup_tx_offloads(IGBCore *core, struct igb_tx *tx)
442 {
443     uint32_t idx = (tx->first_olinfo_status >> 4) & 1;
444 
445     if (tx->first_cmd_type_len & E1000_ADVTXD_DCMD_TSE) {
446         uint32_t mss = tx->ctx[idx].mss_l4len_idx >> E1000_ADVTXD_MSS_SHIFT;
447         if (!net_tx_pkt_build_vheader(tx->tx_pkt, true, true, mss)) {
448             return false;
449         }
450 
451         net_tx_pkt_update_ip_checksums(tx->tx_pkt);
452         e1000x_inc_reg_if_not_full(core->mac, TSCTC);
453         return true;
454     }
455 
456     if ((tx->first_olinfo_status & E1000_ADVTXD_POTS_TXSM) &&
457         !((tx->ctx[idx].type_tucmd_mlhl & E1000_ADVTXD_TUCMD_L4T_SCTP) ?
458           net_tx_pkt_update_sctp_checksum(tx->tx_pkt) :
459           net_tx_pkt_build_vheader(tx->tx_pkt, false, true, 0))) {
460         return false;
461     }
462 
463     if (tx->first_olinfo_status & E1000_ADVTXD_POTS_IXSM) {
464         net_tx_pkt_update_ip_hdr_checksum(tx->tx_pkt);
465     }
466 
467     return true;
468 }
469 
470 static void igb_tx_pkt_mac_callback(void *core,
471                                     const struct iovec *iov,
472                                     int iovcnt,
473                                     const struct iovec *virt_iov,
474                                     int virt_iovcnt)
475 {
476     igb_receive_internal(core, virt_iov, virt_iovcnt, true, NULL);
477 }
478 
479 static void igb_tx_pkt_vmdq_callback(void *opaque,
480                                      const struct iovec *iov,
481                                      int iovcnt,
482                                      const struct iovec *virt_iov,
483                                      int virt_iovcnt)
484 {
485     IGBTxPktVmdqCallbackContext *context = opaque;
486     bool external_tx;
487 
488     igb_receive_internal(context->core, virt_iov, virt_iovcnt, true,
489                          &external_tx);
490 
491     if (external_tx) {
492         if (context->core->has_vnet) {
493             qemu_sendv_packet(context->nc, virt_iov, virt_iovcnt);
494         } else {
495             qemu_sendv_packet(context->nc, iov, iovcnt);
496         }
497     }
498 }
499 
500 /* TX Packets Switching (7.10.3.6) */
501 static bool igb_tx_pkt_switch(IGBCore *core, struct igb_tx *tx,
502                               NetClientState *nc)
503 {
504     IGBTxPktVmdqCallbackContext context;
505 
506     /* TX switching is only used to serve VM to VM traffic. */
507     if (!(core->mac[MRQC] & 1)) {
508         goto send_out;
509     }
510 
511     /* TX switching requires DTXSWC.Loopback_en bit enabled. */
512     if (!(core->mac[DTXSWC] & E1000_DTXSWC_VMDQ_LOOPBACK_EN)) {
513         goto send_out;
514     }
515 
516     context.core = core;
517     context.nc = nc;
518 
519     return net_tx_pkt_send_custom(tx->tx_pkt, false,
520                                   igb_tx_pkt_vmdq_callback, &context);
521 
522 send_out:
523     return net_tx_pkt_send(tx->tx_pkt, nc);
524 }
525 
526 static bool
527 igb_tx_pkt_send(IGBCore *core, struct igb_tx *tx, int queue_index)
528 {
529     int target_queue = MIN(core->max_queue_num, queue_index);
530     NetClientState *queue = qemu_get_subqueue(core->owner_nic, target_queue);
531 
532     if (!igb_setup_tx_offloads(core, tx)) {
533         return false;
534     }
535 
536     net_tx_pkt_dump(tx->tx_pkt);
537 
538     if ((core->phy[MII_BMCR] & MII_BMCR_LOOPBACK) ||
539         ((core->mac[RCTL] & E1000_RCTL_LBM_MAC) == E1000_RCTL_LBM_MAC)) {
540         return net_tx_pkt_send_custom(tx->tx_pkt, false,
541                                       igb_tx_pkt_mac_callback, core);
542     } else {
543         return igb_tx_pkt_switch(core, tx, queue);
544     }
545 }
546 
547 static void
548 igb_on_tx_done_update_stats(IGBCore *core, struct NetTxPkt *tx_pkt, int qn)
549 {
550     static const int PTCregs[6] = { PTC64, PTC127, PTC255, PTC511,
551                                     PTC1023, PTC1522 };
552 
553     size_t tot_len = net_tx_pkt_get_total_len(tx_pkt) + 4;
554 
555     e1000x_increase_size_stats(core->mac, PTCregs, tot_len);
556     e1000x_inc_reg_if_not_full(core->mac, TPT);
557     e1000x_grow_8reg_if_not_full(core->mac, TOTL, tot_len);
558 
559     switch (net_tx_pkt_get_packet_type(tx_pkt)) {
560     case ETH_PKT_BCAST:
561         e1000x_inc_reg_if_not_full(core->mac, BPTC);
562         break;
563     case ETH_PKT_MCAST:
564         e1000x_inc_reg_if_not_full(core->mac, MPTC);
565         break;
566     case ETH_PKT_UCAST:
567         break;
568     default:
569         g_assert_not_reached();
570     }
571 
572     e1000x_inc_reg_if_not_full(core->mac, GPTC);
573     e1000x_grow_8reg_if_not_full(core->mac, GOTCL, tot_len);
574 
575     if (core->mac[MRQC] & 1) {
576         uint16_t pool = qn % IGB_NUM_VM_POOLS;
577 
578         core->mac[PVFGOTC0 + (pool * 64)] += tot_len;
579         core->mac[PVFGPTC0 + (pool * 64)]++;
580     }
581 }
582 
583 static void
584 igb_process_tx_desc(IGBCore *core,
585                     PCIDevice *dev,
586                     struct igb_tx *tx,
587                     union e1000_adv_tx_desc *tx_desc,
588                     int queue_index)
589 {
590     struct e1000_adv_tx_context_desc *tx_ctx_desc;
591     uint32_t cmd_type_len;
592     uint32_t idx;
593     uint64_t buffer_addr;
594     uint16_t length;
595 
596     cmd_type_len = le32_to_cpu(tx_desc->read.cmd_type_len);
597 
598     if (cmd_type_len & E1000_ADVTXD_DCMD_DEXT) {
599         if ((cmd_type_len & E1000_ADVTXD_DTYP_DATA) ==
600             E1000_ADVTXD_DTYP_DATA) {
601             /* advanced transmit data descriptor */
602             if (tx->first) {
603                 tx->first_cmd_type_len = cmd_type_len;
604                 tx->first_olinfo_status = le32_to_cpu(tx_desc->read.olinfo_status);
605                 tx->first = false;
606             }
607         } else if ((cmd_type_len & E1000_ADVTXD_DTYP_CTXT) ==
608                    E1000_ADVTXD_DTYP_CTXT) {
609             /* advanced transmit context descriptor */
610             tx_ctx_desc = (struct e1000_adv_tx_context_desc *)tx_desc;
611             idx = (le32_to_cpu(tx_ctx_desc->mss_l4len_idx) >> 4) & 1;
612             tx->ctx[idx].vlan_macip_lens = le32_to_cpu(tx_ctx_desc->vlan_macip_lens);
613             tx->ctx[idx].seqnum_seed = le32_to_cpu(tx_ctx_desc->seqnum_seed);
614             tx->ctx[idx].type_tucmd_mlhl = le32_to_cpu(tx_ctx_desc->type_tucmd_mlhl);
615             tx->ctx[idx].mss_l4len_idx = le32_to_cpu(tx_ctx_desc->mss_l4len_idx);
616             return;
617         } else {
618             /* unknown descriptor type */
619             return;
620         }
621     } else {
622         /* legacy descriptor */
623 
624         /* TODO: Implement a support for legacy descriptors (7.2.2.1). */
625     }
626 
627     buffer_addr = le64_to_cpu(tx_desc->read.buffer_addr);
628     length = cmd_type_len & 0xFFFF;
629 
630     if (!tx->skip_cp) {
631         if (!net_tx_pkt_add_raw_fragment_pci(tx->tx_pkt, dev,
632                                              buffer_addr, length)) {
633             tx->skip_cp = true;
634         }
635     }
636 
637     if (cmd_type_len & E1000_TXD_CMD_EOP) {
638         if (!tx->skip_cp && net_tx_pkt_parse(tx->tx_pkt)) {
639             idx = (tx->first_olinfo_status >> 4) & 1;
640             igb_tx_insert_vlan(core, queue_index, tx,
641                 tx->ctx[idx].vlan_macip_lens >> IGB_TX_FLAGS_VLAN_SHIFT,
642                 !!(tx->first_cmd_type_len & E1000_TXD_CMD_VLE));
643 
644             if (igb_tx_pkt_send(core, tx, queue_index)) {
645                 igb_on_tx_done_update_stats(core, tx->tx_pkt, queue_index);
646             }
647         }
648 
649         tx->first = true;
650         tx->skip_cp = false;
651         net_tx_pkt_reset(tx->tx_pkt, net_tx_pkt_unmap_frag_pci, dev);
652     }
653 }
654 
655 static uint32_t igb_tx_wb_eic(IGBCore *core, int queue_idx)
656 {
657     uint32_t n, ent = 0;
658 
659     n = igb_ivar_entry_tx(queue_idx);
660     ent = (core->mac[IVAR0 + n / 4] >> (8 * (n % 4))) & 0xff;
661 
662     return (ent & E1000_IVAR_VALID) ? BIT(ent & 0x1f) : 0;
663 }
664 
665 static uint32_t igb_rx_wb_eic(IGBCore *core, int queue_idx)
666 {
667     uint32_t n, ent = 0;
668 
669     n = igb_ivar_entry_rx(queue_idx);
670     ent = (core->mac[IVAR0 + n / 4] >> (8 * (n % 4))) & 0xff;
671 
672     return (ent & E1000_IVAR_VALID) ? BIT(ent & 0x1f) : 0;
673 }
674 
675 typedef struct E1000E_RingInfo_st {
676     int dbah;
677     int dbal;
678     int dlen;
679     int dh;
680     int dt;
681     int idx;
682 } E1000E_RingInfo;
683 
684 static inline bool
685 igb_ring_empty(IGBCore *core, const E1000E_RingInfo *r)
686 {
687     return core->mac[r->dh] == core->mac[r->dt] ||
688                 core->mac[r->dt] >= core->mac[r->dlen] / E1000_RING_DESC_LEN;
689 }
690 
691 static inline uint64_t
692 igb_ring_base(IGBCore *core, const E1000E_RingInfo *r)
693 {
694     uint64_t bah = core->mac[r->dbah];
695     uint64_t bal = core->mac[r->dbal];
696 
697     return (bah << 32) + bal;
698 }
699 
700 static inline uint64_t
701 igb_ring_head_descr(IGBCore *core, const E1000E_RingInfo *r)
702 {
703     return igb_ring_base(core, r) + E1000_RING_DESC_LEN * core->mac[r->dh];
704 }
705 
706 static inline void
707 igb_ring_advance(IGBCore *core, const E1000E_RingInfo *r, uint32_t count)
708 {
709     core->mac[r->dh] += count;
710 
711     if (core->mac[r->dh] * E1000_RING_DESC_LEN >= core->mac[r->dlen]) {
712         core->mac[r->dh] = 0;
713     }
714 }
715 
716 static inline uint32_t
717 igb_ring_free_descr_num(IGBCore *core, const E1000E_RingInfo *r)
718 {
719     trace_e1000e_ring_free_space(r->idx, core->mac[r->dlen],
720                                  core->mac[r->dh],  core->mac[r->dt]);
721 
722     if (core->mac[r->dh] <= core->mac[r->dt]) {
723         return core->mac[r->dt] - core->mac[r->dh];
724     }
725 
726     if (core->mac[r->dh] > core->mac[r->dt]) {
727         return core->mac[r->dlen] / E1000_RING_DESC_LEN +
728                core->mac[r->dt] - core->mac[r->dh];
729     }
730 
731     g_assert_not_reached();
732     return 0;
733 }
734 
735 static inline bool
736 igb_ring_enabled(IGBCore *core, const E1000E_RingInfo *r)
737 {
738     return core->mac[r->dlen] > 0;
739 }
740 
741 typedef struct IGB_TxRing_st {
742     const E1000E_RingInfo *i;
743     struct igb_tx *tx;
744 } IGB_TxRing;
745 
746 static inline int
747 igb_mq_queue_idx(int base_reg_idx, int reg_idx)
748 {
749     return (reg_idx - base_reg_idx) / 16;
750 }
751 
752 static inline void
753 igb_tx_ring_init(IGBCore *core, IGB_TxRing *txr, int idx)
754 {
755     static const E1000E_RingInfo i[IGB_NUM_QUEUES] = {
756         { TDBAH0, TDBAL0, TDLEN0, TDH0, TDT0, 0 },
757         { TDBAH1, TDBAL1, TDLEN1, TDH1, TDT1, 1 },
758         { TDBAH2, TDBAL2, TDLEN2, TDH2, TDT2, 2 },
759         { TDBAH3, TDBAL3, TDLEN3, TDH3, TDT3, 3 },
760         { TDBAH4, TDBAL4, TDLEN4, TDH4, TDT4, 4 },
761         { TDBAH5, TDBAL5, TDLEN5, TDH5, TDT5, 5 },
762         { TDBAH6, TDBAL6, TDLEN6, TDH6, TDT6, 6 },
763         { TDBAH7, TDBAL7, TDLEN7, TDH7, TDT7, 7 },
764         { TDBAH8, TDBAL8, TDLEN8, TDH8, TDT8, 8 },
765         { TDBAH9, TDBAL9, TDLEN9, TDH9, TDT9, 9 },
766         { TDBAH10, TDBAL10, TDLEN10, TDH10, TDT10, 10 },
767         { TDBAH11, TDBAL11, TDLEN11, TDH11, TDT11, 11 },
768         { TDBAH12, TDBAL12, TDLEN12, TDH12, TDT12, 12 },
769         { TDBAH13, TDBAL13, TDLEN13, TDH13, TDT13, 13 },
770         { TDBAH14, TDBAL14, TDLEN14, TDH14, TDT14, 14 },
771         { TDBAH15, TDBAL15, TDLEN15, TDH15, TDT15, 15 }
772     };
773 
774     assert(idx < ARRAY_SIZE(i));
775 
776     txr->i     = &i[idx];
777     txr->tx    = &core->tx[idx];
778 }
779 
780 typedef struct E1000E_RxRing_st {
781     const E1000E_RingInfo *i;
782 } E1000E_RxRing;
783 
784 static inline void
785 igb_rx_ring_init(IGBCore *core, E1000E_RxRing *rxr, int idx)
786 {
787     static const E1000E_RingInfo i[IGB_NUM_QUEUES] = {
788         { RDBAH0, RDBAL0, RDLEN0, RDH0, RDT0, 0 },
789         { RDBAH1, RDBAL1, RDLEN1, RDH1, RDT1, 1 },
790         { RDBAH2, RDBAL2, RDLEN2, RDH2, RDT2, 2 },
791         { RDBAH3, RDBAL3, RDLEN3, RDH3, RDT3, 3 },
792         { RDBAH4, RDBAL4, RDLEN4, RDH4, RDT4, 4 },
793         { RDBAH5, RDBAL5, RDLEN5, RDH5, RDT5, 5 },
794         { RDBAH6, RDBAL6, RDLEN6, RDH6, RDT6, 6 },
795         { RDBAH7, RDBAL7, RDLEN7, RDH7, RDT7, 7 },
796         { RDBAH8, RDBAL8, RDLEN8, RDH8, RDT8, 8 },
797         { RDBAH9, RDBAL9, RDLEN9, RDH9, RDT9, 9 },
798         { RDBAH10, RDBAL10, RDLEN10, RDH10, RDT10, 10 },
799         { RDBAH11, RDBAL11, RDLEN11, RDH11, RDT11, 11 },
800         { RDBAH12, RDBAL12, RDLEN12, RDH12, RDT12, 12 },
801         { RDBAH13, RDBAL13, RDLEN13, RDH13, RDT13, 13 },
802         { RDBAH14, RDBAL14, RDLEN14, RDH14, RDT14, 14 },
803         { RDBAH15, RDBAL15, RDLEN15, RDH15, RDT15, 15 }
804     };
805 
806     assert(idx < ARRAY_SIZE(i));
807 
808     rxr->i      = &i[idx];
809 }
810 
811 static uint32_t
812 igb_txdesc_writeback(IGBCore *core, dma_addr_t base,
813                      union e1000_adv_tx_desc *tx_desc,
814                      const E1000E_RingInfo *txi)
815 {
816     PCIDevice *d;
817     uint32_t cmd_type_len = le32_to_cpu(tx_desc->read.cmd_type_len);
818     uint64_t tdwba;
819 
820     tdwba = core->mac[E1000_TDWBAL(txi->idx) >> 2];
821     tdwba |= (uint64_t)core->mac[E1000_TDWBAH(txi->idx) >> 2] << 32;
822 
823     if (!(cmd_type_len & E1000_TXD_CMD_RS)) {
824         return 0;
825     }
826 
827     d = pcie_sriov_get_vf_at_index(core->owner, txi->idx % 8);
828     if (!d) {
829         d = core->owner;
830     }
831 
832     if (tdwba & 1) {
833         uint32_t buffer = cpu_to_le32(core->mac[txi->dh]);
834         pci_dma_write(d, tdwba & ~3, &buffer, sizeof(buffer));
835     } else {
836         uint32_t status = le32_to_cpu(tx_desc->wb.status) | E1000_TXD_STAT_DD;
837 
838         tx_desc->wb.status = cpu_to_le32(status);
839         pci_dma_write(d, base + offsetof(union e1000_adv_tx_desc, wb),
840             &tx_desc->wb, sizeof(tx_desc->wb));
841     }
842 
843     return igb_tx_wb_eic(core, txi->idx);
844 }
845 
846 static inline bool
847 igb_tx_enabled(IGBCore *core, const E1000E_RingInfo *txi)
848 {
849     bool vmdq = core->mac[MRQC] & 1;
850     uint16_t qn = txi->idx;
851     uint16_t pool = qn % IGB_NUM_VM_POOLS;
852 
853     return (core->mac[TCTL] & E1000_TCTL_EN) &&
854         (!vmdq || core->mac[VFTE] & BIT(pool)) &&
855         (core->mac[TXDCTL0 + (qn * 16)] & E1000_TXDCTL_QUEUE_ENABLE);
856 }
857 
858 static void
859 igb_start_xmit(IGBCore *core, const IGB_TxRing *txr)
860 {
861     PCIDevice *d;
862     dma_addr_t base;
863     union e1000_adv_tx_desc desc;
864     const E1000E_RingInfo *txi = txr->i;
865     uint32_t eic = 0;
866 
867     if (!igb_tx_enabled(core, txi)) {
868         trace_e1000e_tx_disabled();
869         return;
870     }
871 
872     d = pcie_sriov_get_vf_at_index(core->owner, txi->idx % 8);
873     if (!d) {
874         d = core->owner;
875     }
876 
877     while (!igb_ring_empty(core, txi)) {
878         base = igb_ring_head_descr(core, txi);
879 
880         pci_dma_read(d, base, &desc, sizeof(desc));
881 
882         trace_e1000e_tx_descr((void *)(intptr_t)desc.read.buffer_addr,
883                               desc.read.cmd_type_len, desc.wb.status);
884 
885         igb_process_tx_desc(core, d, txr->tx, &desc, txi->idx);
886         igb_ring_advance(core, txi, 1);
887         eic |= igb_txdesc_writeback(core, base, &desc, txi);
888     }
889 
890     if (eic) {
891         core->mac[EICR] |= eic;
892         igb_set_interrupt_cause(core, E1000_ICR_TXDW);
893     }
894 
895     net_tx_pkt_reset(txr->tx->tx_pkt, net_tx_pkt_unmap_frag_pci, d);
896 }
897 
898 static uint32_t
899 igb_rxbufsize(IGBCore *core, const E1000E_RingInfo *r)
900 {
901     uint32_t srrctl = core->mac[E1000_SRRCTL(r->idx) >> 2];
902     uint32_t bsizepkt = srrctl & E1000_SRRCTL_BSIZEPKT_MASK;
903     if (bsizepkt) {
904         return bsizepkt << E1000_SRRCTL_BSIZEPKT_SHIFT;
905     }
906 
907     return e1000x_rxbufsize(core->mac[RCTL]);
908 }
909 
910 static bool
911 igb_has_rxbufs(IGBCore *core, const E1000E_RingInfo *r, size_t total_size)
912 {
913     uint32_t bufs = igb_ring_free_descr_num(core, r);
914     uint32_t bufsize = igb_rxbufsize(core, r);
915 
916     trace_e1000e_rx_has_buffers(r->idx, bufs, total_size, bufsize);
917 
918     return total_size <= bufs / (core->rx_desc_len / E1000_MIN_RX_DESC_LEN) *
919                          bufsize;
920 }
921 
922 void
923 igb_start_recv(IGBCore *core)
924 {
925     int i;
926 
927     trace_e1000e_rx_start_recv();
928 
929     for (i = 0; i <= core->max_queue_num; i++) {
930         qemu_flush_queued_packets(qemu_get_subqueue(core->owner_nic, i));
931     }
932 }
933 
934 bool
935 igb_can_receive(IGBCore *core)
936 {
937     int i;
938 
939     if (!e1000x_rx_ready(core->owner, core->mac)) {
940         return false;
941     }
942 
943     for (i = 0; i < IGB_NUM_QUEUES; i++) {
944         E1000E_RxRing rxr;
945         if (!(core->mac[RXDCTL0 + (i * 16)] & E1000_RXDCTL_QUEUE_ENABLE)) {
946             continue;
947         }
948 
949         igb_rx_ring_init(core, &rxr, i);
950         if (igb_ring_enabled(core, rxr.i) && igb_has_rxbufs(core, rxr.i, 1)) {
951             trace_e1000e_rx_can_recv();
952             return true;
953         }
954     }
955 
956     trace_e1000e_rx_can_recv_rings_full();
957     return false;
958 }
959 
960 ssize_t
961 igb_receive(IGBCore *core, const uint8_t *buf, size_t size)
962 {
963     const struct iovec iov = {
964         .iov_base = (uint8_t *)buf,
965         .iov_len = size
966     };
967 
968     return igb_receive_iov(core, &iov, 1);
969 }
970 
971 static inline bool
972 igb_rx_l3_cso_enabled(IGBCore *core)
973 {
974     return !!(core->mac[RXCSUM] & E1000_RXCSUM_IPOFLD);
975 }
976 
977 static inline bool
978 igb_rx_l4_cso_enabled(IGBCore *core)
979 {
980     return !!(core->mac[RXCSUM] & E1000_RXCSUM_TUOFLD);
981 }
982 
983 static bool
984 igb_rx_is_oversized(IGBCore *core, uint16_t qn, size_t size)
985 {
986     uint16_t pool = qn % IGB_NUM_VM_POOLS;
987     bool lpe = !!(core->mac[VMOLR0 + pool] & E1000_VMOLR_LPE);
988     int max_ethernet_lpe_size =
989         core->mac[VMOLR0 + pool] & E1000_VMOLR_RLPML_MASK;
990     int max_ethernet_vlan_size = 1522;
991 
992     return size > (lpe ? max_ethernet_lpe_size : max_ethernet_vlan_size);
993 }
994 
995 static uint16_t igb_receive_assign(IGBCore *core, const L2Header *l2_header,
996                                    size_t size, E1000E_RSSInfo *rss_info,
997                                    bool *external_tx)
998 {
999     static const int ta_shift[] = { 4, 3, 2, 0 };
1000     const struct eth_header *ehdr = &l2_header->eth;
1001     uint32_t f, ra[2], *macp, rctl = core->mac[RCTL];
1002     uint16_t queues = 0;
1003     uint16_t oversized = 0;
1004     size_t vlan_num = 0;
1005     int i;
1006 
1007     memset(rss_info, 0, sizeof(E1000E_RSSInfo));
1008 
1009     if (external_tx) {
1010         *external_tx = true;
1011     }
1012 
1013     if (core->mac[CTRL_EXT] & BIT(26)) {
1014         if (be16_to_cpu(ehdr->h_proto) == core->mac[VET] >> 16 &&
1015             be16_to_cpu(l2_header->vlan[0].h_proto) == (core->mac[VET] & 0xffff)) {
1016             vlan_num = 2;
1017         }
1018     } else {
1019         if (be16_to_cpu(ehdr->h_proto) == (core->mac[VET] & 0xffff)) {
1020             vlan_num = 1;
1021         }
1022     }
1023 
1024     if (vlan_num &&
1025         !e1000x_rx_vlan_filter(core->mac, l2_header->vlan + vlan_num - 1)) {
1026         return queues;
1027     }
1028 
1029     if (core->mac[MRQC] & 1) {
1030         if (is_broadcast_ether_addr(ehdr->h_dest)) {
1031             for (i = 0; i < IGB_NUM_VM_POOLS; i++) {
1032                 if (core->mac[VMOLR0 + i] & E1000_VMOLR_BAM) {
1033                     queues |= BIT(i);
1034                 }
1035             }
1036         } else {
1037             for (macp = core->mac + RA; macp < core->mac + RA + 32; macp += 2) {
1038                 if (!(macp[1] & E1000_RAH_AV)) {
1039                     continue;
1040                 }
1041                 ra[0] = cpu_to_le32(macp[0]);
1042                 ra[1] = cpu_to_le32(macp[1]);
1043                 if (!memcmp(ehdr->h_dest, (uint8_t *)ra, ETH_ALEN)) {
1044                     queues |= (macp[1] & E1000_RAH_POOL_MASK) / E1000_RAH_POOL_1;
1045                 }
1046             }
1047 
1048             for (macp = core->mac + RA2; macp < core->mac + RA2 + 16; macp += 2) {
1049                 if (!(macp[1] & E1000_RAH_AV)) {
1050                     continue;
1051                 }
1052                 ra[0] = cpu_to_le32(macp[0]);
1053                 ra[1] = cpu_to_le32(macp[1]);
1054                 if (!memcmp(ehdr->h_dest, (uint8_t *)ra, ETH_ALEN)) {
1055                     queues |= (macp[1] & E1000_RAH_POOL_MASK) / E1000_RAH_POOL_1;
1056                 }
1057             }
1058 
1059             if (!queues) {
1060                 macp = core->mac + (is_multicast_ether_addr(ehdr->h_dest) ? MTA : UTA);
1061 
1062                 f = ta_shift[(rctl >> E1000_RCTL_MO_SHIFT) & 3];
1063                 f = (((ehdr->h_dest[5] << 8) | ehdr->h_dest[4]) >> f) & 0xfff;
1064                 if (macp[f >> 5] & (1 << (f & 0x1f))) {
1065                     for (i = 0; i < IGB_NUM_VM_POOLS; i++) {
1066                         if (core->mac[VMOLR0 + i] & E1000_VMOLR_ROMPE) {
1067                             queues |= BIT(i);
1068                         }
1069                     }
1070                 }
1071             } else if (is_unicast_ether_addr(ehdr->h_dest) && external_tx) {
1072                 *external_tx = false;
1073             }
1074         }
1075 
1076         if (e1000x_vlan_rx_filter_enabled(core->mac)) {
1077             uint16_t mask = 0;
1078 
1079             if (vlan_num) {
1080                 uint16_t vid = be16_to_cpu(l2_header->vlan[vlan_num - 1].h_tci) & VLAN_VID_MASK;
1081 
1082                 for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
1083                     if ((core->mac[VLVF0 + i] & E1000_VLVF_VLANID_MASK) == vid &&
1084                         (core->mac[VLVF0 + i] & E1000_VLVF_VLANID_ENABLE)) {
1085                         uint32_t poolsel = core->mac[VLVF0 + i] & E1000_VLVF_POOLSEL_MASK;
1086                         mask |= poolsel >> E1000_VLVF_POOLSEL_SHIFT;
1087                     }
1088                 }
1089             } else {
1090                 for (i = 0; i < IGB_NUM_VM_POOLS; i++) {
1091                     if (core->mac[VMOLR0 + i] & E1000_VMOLR_AUPE) {
1092                         mask |= BIT(i);
1093                     }
1094                 }
1095             }
1096 
1097             queues &= mask;
1098         }
1099 
1100         if (is_unicast_ether_addr(ehdr->h_dest) && !queues && !external_tx &&
1101             !(core->mac[VT_CTL] & E1000_VT_CTL_DISABLE_DEF_POOL)) {
1102             uint32_t def_pl = core->mac[VT_CTL] & E1000_VT_CTL_DEFAULT_POOL_MASK;
1103             queues = BIT(def_pl >> E1000_VT_CTL_DEFAULT_POOL_SHIFT);
1104         }
1105 
1106         queues &= core->mac[VFRE];
1107         if (queues) {
1108             for (i = 0; i < IGB_NUM_VM_POOLS; i++) {
1109                 if ((queues & BIT(i)) && igb_rx_is_oversized(core, i, size)) {
1110                     oversized |= BIT(i);
1111                 }
1112             }
1113             /* 8.19.37 increment ROC if packet is oversized for all queues */
1114             if (oversized == queues) {
1115                 trace_e1000x_rx_oversized(size);
1116                 e1000x_inc_reg_if_not_full(core->mac, ROC);
1117             }
1118             queues &= ~oversized;
1119         }
1120 
1121         if (queues) {
1122             igb_rss_parse_packet(core, core->rx_pkt,
1123                                  external_tx != NULL, rss_info);
1124             /* Sec 8.26.1: PQn = VFn + VQn*8 */
1125             if (rss_info->queue & 1) {
1126                 for (i = 0; i < IGB_NUM_VM_POOLS; i++) {
1127                     if ((queues & BIT(i)) &&
1128                         (core->mac[VMOLR0 + i] & E1000_VMOLR_RSSE)) {
1129                         queues |= BIT(i + IGB_NUM_VM_POOLS);
1130                         queues &= ~BIT(i);
1131                     }
1132                 }
1133             }
1134         }
1135     } else {
1136         bool accepted = e1000x_rx_group_filter(core->mac, ehdr);
1137         if (!accepted) {
1138             for (macp = core->mac + RA2; macp < core->mac + RA2 + 16; macp += 2) {
1139                 if (!(macp[1] & E1000_RAH_AV)) {
1140                     continue;
1141                 }
1142                 ra[0] = cpu_to_le32(macp[0]);
1143                 ra[1] = cpu_to_le32(macp[1]);
1144                 if (!memcmp(ehdr->h_dest, (uint8_t *)ra, ETH_ALEN)) {
1145                     trace_e1000x_rx_flt_ucast_match((int)(macp - core->mac - RA2) / 2,
1146                                                     MAC_ARG(ehdr->h_dest));
1147 
1148                     accepted = true;
1149                     break;
1150                 }
1151             }
1152         }
1153 
1154         if (accepted) {
1155             igb_rss_parse_packet(core, core->rx_pkt, false, rss_info);
1156             queues = BIT(rss_info->queue);
1157         }
1158     }
1159 
1160     return queues;
1161 }
1162 
1163 static inline void
1164 igb_read_lgcy_rx_descr(IGBCore *core, struct e1000_rx_desc *desc,
1165                        hwaddr *buff_addr)
1166 {
1167     *buff_addr = le64_to_cpu(desc->buffer_addr);
1168 }
1169 
1170 static inline void
1171 igb_read_adv_rx_descr(IGBCore *core, union e1000_adv_rx_desc *desc,
1172                       hwaddr *buff_addr)
1173 {
1174     *buff_addr = le64_to_cpu(desc->read.pkt_addr);
1175 }
1176 
1177 static inline void
1178 igb_read_rx_descr(IGBCore *core, union e1000_rx_desc_union *desc,
1179                   hwaddr *buff_addr)
1180 {
1181     if (igb_rx_use_legacy_descriptor(core)) {
1182         igb_read_lgcy_rx_descr(core, &desc->legacy, buff_addr);
1183     } else {
1184         igb_read_adv_rx_descr(core, &desc->adv, buff_addr);
1185     }
1186 }
1187 
1188 static void
1189 igb_verify_csum_in_sw(IGBCore *core,
1190                       struct NetRxPkt *pkt,
1191                       uint32_t *status_flags,
1192                       EthL4HdrProto l4hdr_proto)
1193 {
1194     bool csum_valid;
1195     uint32_t csum_error;
1196 
1197     if (igb_rx_l3_cso_enabled(core)) {
1198         if (!net_rx_pkt_validate_l3_csum(pkt, &csum_valid)) {
1199             trace_e1000e_rx_metadata_l3_csum_validation_failed();
1200         } else {
1201             csum_error = csum_valid ? 0 : E1000_RXDEXT_STATERR_IPE;
1202             *status_flags |= E1000_RXD_STAT_IPCS | csum_error;
1203         }
1204     } else {
1205         trace_e1000e_rx_metadata_l3_cso_disabled();
1206     }
1207 
1208     if (!igb_rx_l4_cso_enabled(core)) {
1209         trace_e1000e_rx_metadata_l4_cso_disabled();
1210         return;
1211     }
1212 
1213     if (!net_rx_pkt_validate_l4_csum(pkt, &csum_valid)) {
1214         trace_e1000e_rx_metadata_l4_csum_validation_failed();
1215         return;
1216     }
1217 
1218     csum_error = csum_valid ? 0 : E1000_RXDEXT_STATERR_TCPE;
1219     *status_flags |= E1000_RXD_STAT_TCPCS | csum_error;
1220 
1221     if (l4hdr_proto == ETH_L4_HDR_PROTO_UDP) {
1222         *status_flags |= E1000_RXD_STAT_UDPCS;
1223     }
1224 }
1225 
1226 static void
1227 igb_build_rx_metadata(IGBCore *core,
1228                       struct NetRxPkt *pkt,
1229                       bool is_eop,
1230                       const E1000E_RSSInfo *rss_info,
1231                       uint16_t *pkt_info, uint16_t *hdr_info,
1232                       uint32_t *rss,
1233                       uint32_t *status_flags,
1234                       uint16_t *ip_id,
1235                       uint16_t *vlan_tag)
1236 {
1237     struct virtio_net_hdr *vhdr;
1238     bool hasip4, hasip6, csum_valid;
1239     EthL4HdrProto l4hdr_proto;
1240 
1241     *status_flags = E1000_RXD_STAT_DD;
1242 
1243     /* No additional metadata needed for non-EOP descriptors */
1244     /* TODO: EOP apply only to status so don't skip whole function. */
1245     if (!is_eop) {
1246         goto func_exit;
1247     }
1248 
1249     *status_flags |= E1000_RXD_STAT_EOP;
1250 
1251     net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto);
1252     trace_e1000e_rx_metadata_protocols(hasip4, hasip6, l4hdr_proto);
1253 
1254     /* VLAN state */
1255     if (net_rx_pkt_is_vlan_stripped(pkt)) {
1256         *status_flags |= E1000_RXD_STAT_VP;
1257         *vlan_tag = cpu_to_le16(net_rx_pkt_get_vlan_tag(pkt));
1258         trace_e1000e_rx_metadata_vlan(*vlan_tag);
1259     }
1260 
1261     /* Packet parsing results */
1262     if ((core->mac[RXCSUM] & E1000_RXCSUM_PCSD) != 0) {
1263         if (rss_info->enabled) {
1264             *rss = cpu_to_le32(rss_info->hash);
1265             trace_igb_rx_metadata_rss(*rss);
1266         }
1267     } else if (hasip4) {
1268             *status_flags |= E1000_RXD_STAT_IPIDV;
1269             *ip_id = cpu_to_le16(net_rx_pkt_get_ip_id(pkt));
1270             trace_e1000e_rx_metadata_ip_id(*ip_id);
1271     }
1272 
1273     if (l4hdr_proto == ETH_L4_HDR_PROTO_TCP && net_rx_pkt_is_tcp_ack(pkt)) {
1274         *status_flags |= E1000_RXD_STAT_ACK;
1275         trace_e1000e_rx_metadata_ack();
1276     }
1277 
1278     if (pkt_info) {
1279         *pkt_info = rss_info->enabled ? rss_info->type : 0;
1280 
1281         if (hasip4) {
1282             *pkt_info |= E1000_ADVRXD_PKT_IP4;
1283         }
1284 
1285         if (hasip6) {
1286             *pkt_info |= E1000_ADVRXD_PKT_IP6;
1287         }
1288 
1289         switch (l4hdr_proto) {
1290         case ETH_L4_HDR_PROTO_TCP:
1291             *pkt_info |= E1000_ADVRXD_PKT_TCP;
1292             break;
1293 
1294         case ETH_L4_HDR_PROTO_UDP:
1295             *pkt_info |= E1000_ADVRXD_PKT_UDP;
1296             break;
1297 
1298         case ETH_L4_HDR_PROTO_SCTP:
1299             *pkt_info |= E1000_ADVRXD_PKT_SCTP;
1300             break;
1301 
1302         default:
1303             break;
1304         }
1305     }
1306 
1307     if (hdr_info) {
1308         *hdr_info = 0;
1309     }
1310 
1311     /* RX CSO information */
1312     if (hasip6 && (core->mac[RFCTL] & E1000_RFCTL_IPV6_XSUM_DIS)) {
1313         trace_e1000e_rx_metadata_ipv6_sum_disabled();
1314         goto func_exit;
1315     }
1316 
1317     vhdr = net_rx_pkt_get_vhdr(pkt);
1318 
1319     if (!(vhdr->flags & VIRTIO_NET_HDR_F_DATA_VALID) &&
1320         !(vhdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)) {
1321         trace_e1000e_rx_metadata_virthdr_no_csum_info();
1322         igb_verify_csum_in_sw(core, pkt, status_flags, l4hdr_proto);
1323         goto func_exit;
1324     }
1325 
1326     if (igb_rx_l3_cso_enabled(core)) {
1327         *status_flags |= hasip4 ? E1000_RXD_STAT_IPCS : 0;
1328     } else {
1329         trace_e1000e_rx_metadata_l3_cso_disabled();
1330     }
1331 
1332     if (igb_rx_l4_cso_enabled(core)) {
1333         switch (l4hdr_proto) {
1334         case ETH_L4_HDR_PROTO_SCTP:
1335             if (!net_rx_pkt_validate_l4_csum(pkt, &csum_valid)) {
1336                 trace_e1000e_rx_metadata_l4_csum_validation_failed();
1337                 goto func_exit;
1338             }
1339             if (!csum_valid) {
1340                 *status_flags |= E1000_RXDEXT_STATERR_TCPE;
1341             }
1342             /* fall through */
1343         case ETH_L4_HDR_PROTO_TCP:
1344             *status_flags |= E1000_RXD_STAT_TCPCS;
1345             break;
1346 
1347         case ETH_L4_HDR_PROTO_UDP:
1348             *status_flags |= E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS;
1349             break;
1350 
1351         default:
1352             break;
1353         }
1354     } else {
1355         trace_e1000e_rx_metadata_l4_cso_disabled();
1356     }
1357 
1358 func_exit:
1359     trace_e1000e_rx_metadata_status_flags(*status_flags);
1360     *status_flags = cpu_to_le32(*status_flags);
1361 }
1362 
1363 static inline void
1364 igb_write_lgcy_rx_descr(IGBCore *core, struct e1000_rx_desc *desc,
1365                         struct NetRxPkt *pkt,
1366                         const E1000E_RSSInfo *rss_info,
1367                         uint16_t length)
1368 {
1369     uint32_t status_flags, rss;
1370     uint16_t ip_id;
1371 
1372     assert(!rss_info->enabled);
1373     desc->length = cpu_to_le16(length);
1374     desc->csum = 0;
1375 
1376     igb_build_rx_metadata(core, pkt, pkt != NULL,
1377                           rss_info,
1378                           NULL, NULL, &rss,
1379                           &status_flags, &ip_id,
1380                           &desc->special);
1381     desc->errors = (uint8_t) (le32_to_cpu(status_flags) >> 24);
1382     desc->status = (uint8_t) le32_to_cpu(status_flags);
1383 }
1384 
1385 static inline void
1386 igb_write_adv_rx_descr(IGBCore *core, union e1000_adv_rx_desc *desc,
1387                        struct NetRxPkt *pkt,
1388                        const E1000E_RSSInfo *rss_info,
1389                        uint16_t length)
1390 {
1391     memset(&desc->wb, 0, sizeof(desc->wb));
1392 
1393     desc->wb.upper.length = cpu_to_le16(length);
1394 
1395     igb_build_rx_metadata(core, pkt, pkt != NULL,
1396                           rss_info,
1397                           &desc->wb.lower.lo_dword.pkt_info,
1398                           &desc->wb.lower.lo_dword.hdr_info,
1399                           &desc->wb.lower.hi_dword.rss,
1400                           &desc->wb.upper.status_error,
1401                           &desc->wb.lower.hi_dword.csum_ip.ip_id,
1402                           &desc->wb.upper.vlan);
1403 }
1404 
1405 static inline void
1406 igb_write_rx_descr(IGBCore *core, union e1000_rx_desc_union *desc,
1407 struct NetRxPkt *pkt, const E1000E_RSSInfo *rss_info, uint16_t length)
1408 {
1409     if (igb_rx_use_legacy_descriptor(core)) {
1410         igb_write_lgcy_rx_descr(core, &desc->legacy, pkt, rss_info, length);
1411     } else {
1412         igb_write_adv_rx_descr(core, &desc->adv, pkt, rss_info, length);
1413     }
1414 }
1415 
1416 static inline void
1417 igb_pci_dma_write_rx_desc(IGBCore *core, PCIDevice *dev, dma_addr_t addr,
1418                           union e1000_rx_desc_union *desc, dma_addr_t len)
1419 {
1420     if (igb_rx_use_legacy_descriptor(core)) {
1421         struct e1000_rx_desc *d = &desc->legacy;
1422         size_t offset = offsetof(struct e1000_rx_desc, status);
1423         uint8_t status = d->status;
1424 
1425         d->status &= ~E1000_RXD_STAT_DD;
1426         pci_dma_write(dev, addr, desc, len);
1427 
1428         if (status & E1000_RXD_STAT_DD) {
1429             d->status = status;
1430             pci_dma_write(dev, addr + offset, &status, sizeof(status));
1431         }
1432     } else {
1433         union e1000_adv_rx_desc *d = &desc->adv;
1434         size_t offset =
1435             offsetof(union e1000_adv_rx_desc, wb.upper.status_error);
1436         uint32_t status = d->wb.upper.status_error;
1437 
1438         d->wb.upper.status_error &= ~E1000_RXD_STAT_DD;
1439         pci_dma_write(dev, addr, desc, len);
1440 
1441         if (status & E1000_RXD_STAT_DD) {
1442             d->wb.upper.status_error = status;
1443             pci_dma_write(dev, addr + offset, &status, sizeof(status));
1444         }
1445     }
1446 }
1447 
1448 static void
1449 igb_write_to_rx_buffers(IGBCore *core,
1450                         PCIDevice *d,
1451                         hwaddr ba,
1452                         uint16_t *written,
1453                         const char *data,
1454                         dma_addr_t data_len)
1455 {
1456     trace_igb_rx_desc_buff_write(ba, *written, data, data_len);
1457     pci_dma_write(d, ba + *written, data, data_len);
1458     *written += data_len;
1459 }
1460 
1461 static void
1462 igb_update_rx_stats(IGBCore *core, const E1000E_RingInfo *rxi,
1463                     size_t pkt_size, size_t pkt_fcs_size)
1464 {
1465     eth_pkt_types_e pkt_type = net_rx_pkt_get_packet_type(core->rx_pkt);
1466     e1000x_update_rx_total_stats(core->mac, pkt_type, pkt_size, pkt_fcs_size);
1467 
1468     if (core->mac[MRQC] & 1) {
1469         uint16_t pool = rxi->idx % IGB_NUM_VM_POOLS;
1470 
1471         core->mac[PVFGORC0 + (pool * 64)] += pkt_size + 4;
1472         core->mac[PVFGPRC0 + (pool * 64)]++;
1473         if (pkt_type == ETH_PKT_MCAST) {
1474             core->mac[PVFMPRC0 + (pool * 64)]++;
1475         }
1476     }
1477 }
1478 
1479 static inline bool
1480 igb_rx_descr_threshold_hit(IGBCore *core, const E1000E_RingInfo *rxi)
1481 {
1482     return igb_ring_free_descr_num(core, rxi) ==
1483            ((core->mac[E1000_SRRCTL(rxi->idx) >> 2] >> 20) & 31) * 16;
1484 }
1485 
1486 static void
1487 igb_write_packet_to_guest(IGBCore *core, struct NetRxPkt *pkt,
1488                           const E1000E_RxRing *rxr,
1489                           const E1000E_RSSInfo *rss_info)
1490 {
1491     PCIDevice *d;
1492     dma_addr_t base;
1493     union e1000_rx_desc_union desc;
1494     size_t desc_size;
1495     size_t desc_offset = 0;
1496     size_t iov_ofs = 0;
1497 
1498     struct iovec *iov = net_rx_pkt_get_iovec(pkt);
1499     size_t size = net_rx_pkt_get_total_len(pkt);
1500     size_t total_size = size + e1000x_fcs_len(core->mac);
1501     const E1000E_RingInfo *rxi = rxr->i;
1502     size_t bufsize = igb_rxbufsize(core, rxi);
1503 
1504     d = pcie_sriov_get_vf_at_index(core->owner, rxi->idx % 8);
1505     if (!d) {
1506         d = core->owner;
1507     }
1508 
1509     do {
1510         hwaddr ba;
1511         uint16_t written = 0;
1512         bool is_last = false;
1513 
1514         desc_size = total_size - desc_offset;
1515 
1516         if (desc_size > bufsize) {
1517             desc_size = bufsize;
1518         }
1519 
1520         if (igb_ring_empty(core, rxi)) {
1521             return;
1522         }
1523 
1524         base = igb_ring_head_descr(core, rxi);
1525 
1526         pci_dma_read(d, base, &desc, core->rx_desc_len);
1527 
1528         trace_e1000e_rx_descr(rxi->idx, base, core->rx_desc_len);
1529 
1530         igb_read_rx_descr(core, &desc, &ba);
1531 
1532         if (ba) {
1533             if (desc_offset < size) {
1534                 static const uint32_t fcs_pad;
1535                 size_t iov_copy;
1536                 size_t copy_size = size - desc_offset;
1537                 if (copy_size > bufsize) {
1538                     copy_size = bufsize;
1539                 }
1540 
1541                 /* Copy packet payload */
1542                 while (copy_size) {
1543                     iov_copy = MIN(copy_size, iov->iov_len - iov_ofs);
1544 
1545                     igb_write_to_rx_buffers(core, d, ba, &written,
1546                                             iov->iov_base + iov_ofs, iov_copy);
1547 
1548                     copy_size -= iov_copy;
1549                     iov_ofs += iov_copy;
1550                     if (iov_ofs == iov->iov_len) {
1551                         iov++;
1552                         iov_ofs = 0;
1553                     }
1554                 }
1555 
1556                 if (desc_offset + desc_size >= total_size) {
1557                     /* Simulate FCS checksum presence in the last descriptor */
1558                     igb_write_to_rx_buffers(core, d, ba, &written,
1559                           (const char *) &fcs_pad, e1000x_fcs_len(core->mac));
1560                 }
1561             }
1562         } else { /* as per intel docs; skip descriptors with null buf addr */
1563             trace_e1000e_rx_null_descriptor();
1564         }
1565         desc_offset += desc_size;
1566         if (desc_offset >= total_size) {
1567             is_last = true;
1568         }
1569 
1570         igb_write_rx_descr(core, &desc, is_last ? core->rx_pkt : NULL,
1571                            rss_info, written);
1572         igb_pci_dma_write_rx_desc(core, d, base, &desc, core->rx_desc_len);
1573 
1574         igb_ring_advance(core, rxi, core->rx_desc_len / E1000_MIN_RX_DESC_LEN);
1575 
1576     } while (desc_offset < total_size);
1577 
1578     igb_update_rx_stats(core, rxi, size, total_size);
1579 }
1580 
1581 static bool
1582 igb_rx_strip_vlan(IGBCore *core, const E1000E_RingInfo *rxi)
1583 {
1584     if (core->mac[MRQC] & 1) {
1585         uint16_t pool = rxi->idx % IGB_NUM_VM_POOLS;
1586         /* Sec 7.10.3.8: CTRL.VME is ignored, only VMOLR/RPLOLR is used */
1587         return (net_rx_pkt_get_packet_type(core->rx_pkt) == ETH_PKT_MCAST) ?
1588                 core->mac[RPLOLR] & E1000_RPLOLR_STRVLAN :
1589                 core->mac[VMOLR0 + pool] & E1000_VMOLR_STRVLAN;
1590     }
1591 
1592     return e1000x_vlan_enabled(core->mac);
1593 }
1594 
1595 static inline void
1596 igb_rx_fix_l4_csum(IGBCore *core, struct NetRxPkt *pkt)
1597 {
1598     struct virtio_net_hdr *vhdr = net_rx_pkt_get_vhdr(pkt);
1599 
1600     if (vhdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1601         net_rx_pkt_fix_l4_csum(pkt);
1602     }
1603 }
1604 
1605 ssize_t
1606 igb_receive_iov(IGBCore *core, const struct iovec *iov, int iovcnt)
1607 {
1608     return igb_receive_internal(core, iov, iovcnt, core->has_vnet, NULL);
1609 }
1610 
1611 static ssize_t
1612 igb_receive_internal(IGBCore *core, const struct iovec *iov, int iovcnt,
1613                      bool has_vnet, bool *external_tx)
1614 {
1615     uint16_t queues = 0;
1616     uint32_t causes = 0;
1617     union {
1618         L2Header l2_header;
1619         uint8_t octets[ETH_ZLEN];
1620     } buf;
1621     struct iovec min_iov;
1622     size_t size, orig_size;
1623     size_t iov_ofs = 0;
1624     E1000E_RxRing rxr;
1625     E1000E_RSSInfo rss_info;
1626     size_t total_size;
1627     int strip_vlan_index;
1628     int i;
1629 
1630     trace_e1000e_rx_receive_iov(iovcnt);
1631 
1632     if (external_tx) {
1633         *external_tx = true;
1634     }
1635 
1636     if (!e1000x_hw_rx_enabled(core->mac)) {
1637         return -1;
1638     }
1639 
1640     /* Pull virtio header in */
1641     if (has_vnet) {
1642         net_rx_pkt_set_vhdr_iovec(core->rx_pkt, iov, iovcnt);
1643         iov_ofs = sizeof(struct virtio_net_hdr);
1644     } else {
1645         net_rx_pkt_unset_vhdr(core->rx_pkt);
1646     }
1647 
1648     orig_size = iov_size(iov, iovcnt);
1649     size = orig_size - iov_ofs;
1650 
1651     /* Pad to minimum Ethernet frame length */
1652     if (size < sizeof(buf)) {
1653         iov_to_buf(iov, iovcnt, iov_ofs, &buf, size);
1654         memset(&buf.octets[size], 0, sizeof(buf) - size);
1655         e1000x_inc_reg_if_not_full(core->mac, RUC);
1656         min_iov.iov_base = &buf;
1657         min_iov.iov_len = size = sizeof(buf);
1658         iovcnt = 1;
1659         iov = &min_iov;
1660         iov_ofs = 0;
1661     } else {
1662         iov_to_buf(iov, iovcnt, iov_ofs, &buf, sizeof(buf.l2_header));
1663     }
1664 
1665     /* Discard oversized packets if !LPE and !SBP. */
1666     if (e1000x_is_oversized(core->mac, size)) {
1667         return orig_size;
1668     }
1669 
1670     net_rx_pkt_set_packet_type(core->rx_pkt,
1671                                get_eth_packet_type(&buf.l2_header.eth));
1672     net_rx_pkt_set_protocols(core->rx_pkt, iov, iovcnt, iov_ofs);
1673 
1674     queues = igb_receive_assign(core, &buf.l2_header, size,
1675                                 &rss_info, external_tx);
1676     if (!queues) {
1677         trace_e1000e_rx_flt_dropped();
1678         return orig_size;
1679     }
1680 
1681     for (i = 0; i < IGB_NUM_QUEUES; i++) {
1682         if (!(queues & BIT(i)) ||
1683             !(core->mac[RXDCTL0 + (i * 16)] & E1000_RXDCTL_QUEUE_ENABLE)) {
1684             continue;
1685         }
1686 
1687         igb_rx_ring_init(core, &rxr, i);
1688 
1689         if (!igb_rx_strip_vlan(core, rxr.i)) {
1690             strip_vlan_index = -1;
1691         } else if (core->mac[CTRL_EXT] & BIT(26)) {
1692             strip_vlan_index = 1;
1693         } else {
1694             strip_vlan_index = 0;
1695         }
1696 
1697         net_rx_pkt_attach_iovec_ex(core->rx_pkt, iov, iovcnt, iov_ofs,
1698                                    strip_vlan_index,
1699                                    core->mac[VET] & 0xffff,
1700                                    core->mac[VET] >> 16);
1701 
1702         total_size = net_rx_pkt_get_total_len(core->rx_pkt) +
1703             e1000x_fcs_len(core->mac);
1704 
1705         if (!igb_has_rxbufs(core, rxr.i, total_size)) {
1706             causes |= E1000_ICS_RXO;
1707             trace_e1000e_rx_not_written_to_guest(rxr.i->idx);
1708             continue;
1709         }
1710 
1711         causes |= E1000_ICR_RXDW;
1712 
1713         igb_rx_fix_l4_csum(core, core->rx_pkt);
1714         igb_write_packet_to_guest(core, core->rx_pkt, &rxr, &rss_info);
1715 
1716         /* Check if receive descriptor minimum threshold hit */
1717         if (igb_rx_descr_threshold_hit(core, rxr.i)) {
1718             causes |= E1000_ICS_RXDMT0;
1719         }
1720 
1721         core->mac[EICR] |= igb_rx_wb_eic(core, rxr.i->idx);
1722 
1723         trace_e1000e_rx_written_to_guest(rxr.i->idx);
1724     }
1725 
1726     trace_e1000e_rx_interrupt_set(causes);
1727     igb_set_interrupt_cause(core, causes);
1728 
1729     return orig_size;
1730 }
1731 
1732 static inline bool
1733 igb_have_autoneg(IGBCore *core)
1734 {
1735     return core->phy[MII_BMCR] & MII_BMCR_AUTOEN;
1736 }
1737 
1738 static void igb_update_flowctl_status(IGBCore *core)
1739 {
1740     if (igb_have_autoneg(core) && core->phy[MII_BMSR] & MII_BMSR_AN_COMP) {
1741         trace_e1000e_link_autoneg_flowctl(true);
1742         core->mac[CTRL] |= E1000_CTRL_TFCE | E1000_CTRL_RFCE;
1743     } else {
1744         trace_e1000e_link_autoneg_flowctl(false);
1745     }
1746 }
1747 
1748 static inline void
1749 igb_link_down(IGBCore *core)
1750 {
1751     e1000x_update_regs_on_link_down(core->mac, core->phy);
1752     igb_update_flowctl_status(core);
1753 }
1754 
1755 static inline void
1756 igb_set_phy_ctrl(IGBCore *core, uint16_t val)
1757 {
1758     /* bits 0-5 reserved; MII_BMCR_[ANRESTART,RESET] are self clearing */
1759     core->phy[MII_BMCR] = val & ~(0x3f | MII_BMCR_RESET | MII_BMCR_ANRESTART);
1760 
1761     if ((val & MII_BMCR_ANRESTART) && igb_have_autoneg(core)) {
1762         e1000x_restart_autoneg(core->mac, core->phy, core->autoneg_timer);
1763     }
1764 }
1765 
1766 void igb_core_set_link_status(IGBCore *core)
1767 {
1768     NetClientState *nc = qemu_get_queue(core->owner_nic);
1769     uint32_t old_status = core->mac[STATUS];
1770 
1771     trace_e1000e_link_status_changed(nc->link_down ? false : true);
1772 
1773     if (nc->link_down) {
1774         e1000x_update_regs_on_link_down(core->mac, core->phy);
1775     } else {
1776         if (igb_have_autoneg(core) &&
1777             !(core->phy[MII_BMSR] & MII_BMSR_AN_COMP)) {
1778             e1000x_restart_autoneg(core->mac, core->phy,
1779                                    core->autoneg_timer);
1780         } else {
1781             e1000x_update_regs_on_link_up(core->mac, core->phy);
1782             igb_start_recv(core);
1783         }
1784     }
1785 
1786     if (core->mac[STATUS] != old_status) {
1787         igb_set_interrupt_cause(core, E1000_ICR_LSC);
1788     }
1789 }
1790 
1791 static void
1792 igb_set_ctrl(IGBCore *core, int index, uint32_t val)
1793 {
1794     trace_e1000e_core_ctrl_write(index, val);
1795 
1796     /* RST is self clearing */
1797     core->mac[CTRL] = val & ~E1000_CTRL_RST;
1798     core->mac[CTRL_DUP] = core->mac[CTRL];
1799 
1800     trace_e1000e_link_set_params(
1801         !!(val & E1000_CTRL_ASDE),
1802         (val & E1000_CTRL_SPD_SEL) >> E1000_CTRL_SPD_SHIFT,
1803         !!(val & E1000_CTRL_FRCSPD),
1804         !!(val & E1000_CTRL_FRCDPX),
1805         !!(val & E1000_CTRL_RFCE),
1806         !!(val & E1000_CTRL_TFCE));
1807 
1808     if (val & E1000_CTRL_RST) {
1809         trace_e1000e_core_ctrl_sw_reset();
1810         igb_reset(core, true);
1811     }
1812 
1813     if (val & E1000_CTRL_PHY_RST) {
1814         trace_e1000e_core_ctrl_phy_reset();
1815         core->mac[STATUS] |= E1000_STATUS_PHYRA;
1816     }
1817 }
1818 
1819 static void
1820 igb_set_rfctl(IGBCore *core, int index, uint32_t val)
1821 {
1822     trace_e1000e_rx_set_rfctl(val);
1823 
1824     if (!(val & E1000_RFCTL_ISCSI_DIS)) {
1825         trace_e1000e_wrn_iscsi_filtering_not_supported();
1826     }
1827 
1828     if (!(val & E1000_RFCTL_NFSW_DIS)) {
1829         trace_e1000e_wrn_nfsw_filtering_not_supported();
1830     }
1831 
1832     if (!(val & E1000_RFCTL_NFSR_DIS)) {
1833         trace_e1000e_wrn_nfsr_filtering_not_supported();
1834     }
1835 
1836     core->mac[RFCTL] = val;
1837 }
1838 
1839 static void
1840 igb_calc_rxdesclen(IGBCore *core)
1841 {
1842     if (igb_rx_use_legacy_descriptor(core)) {
1843         core->rx_desc_len = sizeof(struct e1000_rx_desc);
1844     } else {
1845         core->rx_desc_len = sizeof(union e1000_adv_rx_desc);
1846     }
1847     trace_e1000e_rx_desc_len(core->rx_desc_len);
1848 }
1849 
1850 static void
1851 igb_set_rx_control(IGBCore *core, int index, uint32_t val)
1852 {
1853     core->mac[RCTL] = val;
1854     trace_e1000e_rx_set_rctl(core->mac[RCTL]);
1855 
1856     if (val & E1000_RCTL_DTYP_MASK) {
1857         qemu_log_mask(LOG_GUEST_ERROR,
1858                       "igb: RCTL.DTYP must be zero for compatibility");
1859     }
1860 
1861     if (val & E1000_RCTL_EN) {
1862         igb_calc_rxdesclen(core);
1863         igb_start_recv(core);
1864     }
1865 }
1866 
1867 static inline void
1868 igb_clear_ims_bits(IGBCore *core, uint32_t bits)
1869 {
1870     trace_e1000e_irq_clear_ims(bits, core->mac[IMS], core->mac[IMS] & ~bits);
1871     core->mac[IMS] &= ~bits;
1872 }
1873 
1874 static inline bool
1875 igb_postpone_interrupt(IGBIntrDelayTimer *timer)
1876 {
1877     if (timer->running) {
1878         trace_e1000e_irq_postponed_by_xitr(timer->delay_reg << 2);
1879 
1880         return true;
1881     }
1882 
1883     if (timer->core->mac[timer->delay_reg] != 0) {
1884         igb_intrmgr_rearm_timer(timer);
1885     }
1886 
1887     return false;
1888 }
1889 
1890 static inline bool
1891 igb_eitr_should_postpone(IGBCore *core, int idx)
1892 {
1893     return igb_postpone_interrupt(&core->eitr[idx]);
1894 }
1895 
1896 static void igb_send_msix(IGBCore *core)
1897 {
1898     uint32_t causes = core->mac[EICR] & core->mac[EIMS];
1899     int vector;
1900 
1901     for (vector = 0; vector < IGB_INTR_NUM; ++vector) {
1902         if ((causes & BIT(vector)) && !igb_eitr_should_postpone(core, vector)) {
1903 
1904             trace_e1000e_irq_msix_notify_vec(vector);
1905             igb_msix_notify(core, vector);
1906         }
1907     }
1908 }
1909 
1910 static inline void
1911 igb_fix_icr_asserted(IGBCore *core)
1912 {
1913     core->mac[ICR] &= ~E1000_ICR_ASSERTED;
1914     if (core->mac[ICR]) {
1915         core->mac[ICR] |= E1000_ICR_ASSERTED;
1916     }
1917 
1918     trace_e1000e_irq_fix_icr_asserted(core->mac[ICR]);
1919 }
1920 
1921 static void
1922 igb_update_interrupt_state(IGBCore *core)
1923 {
1924     uint32_t icr;
1925     uint32_t causes;
1926     uint32_t int_alloc;
1927 
1928     icr = core->mac[ICR] & core->mac[IMS];
1929 
1930     if (core->mac[GPIE] & E1000_GPIE_MSIX_MODE) {
1931         if (icr) {
1932             causes = 0;
1933             if (icr & E1000_ICR_DRSTA) {
1934                 int_alloc = core->mac[IVAR_MISC] & 0xff;
1935                 if (int_alloc & E1000_IVAR_VALID) {
1936                     causes |= BIT(int_alloc & 0x1f);
1937                 }
1938             }
1939             /* Check if other bits (excluding the TCP Timer) are enabled. */
1940             if (icr & ~E1000_ICR_DRSTA) {
1941                 int_alloc = (core->mac[IVAR_MISC] >> 8) & 0xff;
1942                 if (int_alloc & E1000_IVAR_VALID) {
1943                     causes |= BIT(int_alloc & 0x1f);
1944                 }
1945                 trace_e1000e_irq_add_msi_other(core->mac[EICR]);
1946             }
1947             core->mac[EICR] |= causes;
1948         }
1949 
1950         if ((core->mac[EICR] & core->mac[EIMS])) {
1951             igb_send_msix(core);
1952         }
1953     } else {
1954         igb_fix_icr_asserted(core);
1955 
1956         if (icr) {
1957             core->mac[EICR] |= (icr & E1000_ICR_DRSTA) | E1000_EICR_OTHER;
1958         } else {
1959             core->mac[EICR] &= ~E1000_EICR_OTHER;
1960         }
1961 
1962         trace_e1000e_irq_pending_interrupts(core->mac[ICR] & core->mac[IMS],
1963                                             core->mac[ICR], core->mac[IMS]);
1964 
1965         if (msix_enabled(core->owner)) {
1966             if (icr) {
1967                 trace_e1000e_irq_msix_notify_vec(0);
1968                 msix_notify(core->owner, 0);
1969             }
1970         } else if (msi_enabled(core->owner)) {
1971             if (icr) {
1972                 msi_notify(core->owner, 0);
1973             }
1974         } else {
1975             if (icr) {
1976                 igb_raise_legacy_irq(core);
1977             } else {
1978                 igb_lower_legacy_irq(core);
1979             }
1980         }
1981     }
1982 }
1983 
1984 static void
1985 igb_set_interrupt_cause(IGBCore *core, uint32_t val)
1986 {
1987     trace_e1000e_irq_set_cause_entry(val, core->mac[ICR]);
1988 
1989     core->mac[ICR] |= val;
1990 
1991     trace_e1000e_irq_set_cause_exit(val, core->mac[ICR]);
1992 
1993     igb_update_interrupt_state(core);
1994 }
1995 
1996 static void igb_set_eics(IGBCore *core, int index, uint32_t val)
1997 {
1998     bool msix = !!(core->mac[GPIE] & E1000_GPIE_MSIX_MODE);
1999 
2000     trace_igb_irq_write_eics(val, msix);
2001 
2002     core->mac[EICS] |=
2003         val & (msix ? E1000_EICR_MSIX_MASK : E1000_EICR_LEGACY_MASK);
2004 
2005     /*
2006      * TODO: Move to igb_update_interrupt_state if EICS is modified in other
2007      * places.
2008      */
2009     core->mac[EICR] = core->mac[EICS];
2010 
2011     igb_update_interrupt_state(core);
2012 }
2013 
2014 static void igb_set_eims(IGBCore *core, int index, uint32_t val)
2015 {
2016     bool msix = !!(core->mac[GPIE] & E1000_GPIE_MSIX_MODE);
2017 
2018     trace_igb_irq_write_eims(val, msix);
2019 
2020     core->mac[EIMS] |=
2021         val & (msix ? E1000_EICR_MSIX_MASK : E1000_EICR_LEGACY_MASK);
2022 
2023     igb_update_interrupt_state(core);
2024 }
2025 
2026 static void mailbox_interrupt_to_vf(IGBCore *core, uint16_t vfn)
2027 {
2028     uint32_t ent = core->mac[VTIVAR_MISC + vfn];
2029 
2030     if ((ent & E1000_IVAR_VALID)) {
2031         core->mac[EICR] |= (ent & 0x3) << (22 - vfn * IGBVF_MSIX_VEC_NUM);
2032         igb_update_interrupt_state(core);
2033     }
2034 }
2035 
2036 static void mailbox_interrupt_to_pf(IGBCore *core)
2037 {
2038     igb_set_interrupt_cause(core, E1000_ICR_VMMB);
2039 }
2040 
2041 static void igb_set_pfmailbox(IGBCore *core, int index, uint32_t val)
2042 {
2043     uint16_t vfn = index - P2VMAILBOX0;
2044 
2045     trace_igb_set_pfmailbox(vfn, val);
2046 
2047     if (val & E1000_P2VMAILBOX_STS) {
2048         core->mac[V2PMAILBOX0 + vfn] |= E1000_V2PMAILBOX_PFSTS;
2049         mailbox_interrupt_to_vf(core, vfn);
2050     }
2051 
2052     if (val & E1000_P2VMAILBOX_ACK) {
2053         core->mac[V2PMAILBOX0 + vfn] |= E1000_V2PMAILBOX_PFACK;
2054         mailbox_interrupt_to_vf(core, vfn);
2055     }
2056 
2057     /* Buffer Taken by PF (can be set only if the VFU is cleared). */
2058     if (val & E1000_P2VMAILBOX_PFU) {
2059         if (!(core->mac[index] & E1000_P2VMAILBOX_VFU)) {
2060             core->mac[index] |= E1000_P2VMAILBOX_PFU;
2061             core->mac[V2PMAILBOX0 + vfn] |= E1000_V2PMAILBOX_PFU;
2062         }
2063     } else {
2064         core->mac[index] &= ~E1000_P2VMAILBOX_PFU;
2065         core->mac[V2PMAILBOX0 + vfn] &= ~E1000_V2PMAILBOX_PFU;
2066     }
2067 
2068     if (val & E1000_P2VMAILBOX_RVFU) {
2069         core->mac[V2PMAILBOX0 + vfn] &= ~E1000_V2PMAILBOX_VFU;
2070         core->mac[MBVFICR] &= ~((E1000_MBVFICR_VFACK_VF1 << vfn) |
2071                                 (E1000_MBVFICR_VFREQ_VF1 << vfn));
2072     }
2073 }
2074 
2075 static void igb_set_vfmailbox(IGBCore *core, int index, uint32_t val)
2076 {
2077     uint16_t vfn = index - V2PMAILBOX0;
2078 
2079     trace_igb_set_vfmailbox(vfn, val);
2080 
2081     if (val & E1000_V2PMAILBOX_REQ) {
2082         core->mac[MBVFICR] |= E1000_MBVFICR_VFREQ_VF1 << vfn;
2083         mailbox_interrupt_to_pf(core);
2084     }
2085 
2086     if (val & E1000_V2PMAILBOX_ACK) {
2087         core->mac[MBVFICR] |= E1000_MBVFICR_VFACK_VF1 << vfn;
2088         mailbox_interrupt_to_pf(core);
2089     }
2090 
2091     /* Buffer Taken by VF (can be set only if the PFU is cleared). */
2092     if (val & E1000_V2PMAILBOX_VFU) {
2093         if (!(core->mac[index] & E1000_V2PMAILBOX_PFU)) {
2094             core->mac[index] |= E1000_V2PMAILBOX_VFU;
2095             core->mac[P2VMAILBOX0 + vfn] |= E1000_P2VMAILBOX_VFU;
2096         }
2097     } else {
2098         core->mac[index] &= ~E1000_V2PMAILBOX_VFU;
2099         core->mac[P2VMAILBOX0 + vfn] &= ~E1000_P2VMAILBOX_VFU;
2100     }
2101 }
2102 
2103 static void igb_vf_reset(IGBCore *core, uint16_t vfn)
2104 {
2105     uint16_t qn0 = vfn;
2106     uint16_t qn1 = vfn + IGB_NUM_VM_POOLS;
2107 
2108     /* disable Rx and Tx for the VF*/
2109     core->mac[RXDCTL0 + (qn0 * 16)] &= ~E1000_RXDCTL_QUEUE_ENABLE;
2110     core->mac[RXDCTL0 + (qn1 * 16)] &= ~E1000_RXDCTL_QUEUE_ENABLE;
2111     core->mac[TXDCTL0 + (qn0 * 16)] &= ~E1000_TXDCTL_QUEUE_ENABLE;
2112     core->mac[TXDCTL0 + (qn1 * 16)] &= ~E1000_TXDCTL_QUEUE_ENABLE;
2113     core->mac[VFRE] &= ~BIT(vfn);
2114     core->mac[VFTE] &= ~BIT(vfn);
2115     /* indicate VF reset to PF */
2116     core->mac[VFLRE] |= BIT(vfn);
2117     /* VFLRE and mailbox use the same interrupt cause */
2118     mailbox_interrupt_to_pf(core);
2119 }
2120 
2121 static void igb_w1c(IGBCore *core, int index, uint32_t val)
2122 {
2123     core->mac[index] &= ~val;
2124 }
2125 
2126 static void igb_set_eimc(IGBCore *core, int index, uint32_t val)
2127 {
2128     bool msix = !!(core->mac[GPIE] & E1000_GPIE_MSIX_MODE);
2129 
2130     /* Interrupts are disabled via a write to EIMC and reflected in EIMS. */
2131     core->mac[EIMS] &=
2132         ~(val & (msix ? E1000_EICR_MSIX_MASK : E1000_EICR_LEGACY_MASK));
2133 
2134     trace_igb_irq_write_eimc(val, core->mac[EIMS], msix);
2135     igb_update_interrupt_state(core);
2136 }
2137 
2138 static void igb_set_eiac(IGBCore *core, int index, uint32_t val)
2139 {
2140     bool msix = !!(core->mac[GPIE] & E1000_GPIE_MSIX_MODE);
2141 
2142     if (msix) {
2143         trace_igb_irq_write_eiac(val);
2144 
2145         /*
2146          * TODO: When using IOV, the bits that correspond to MSI-X vectors
2147          * that are assigned to a VF are read-only.
2148          */
2149         core->mac[EIAC] |= (val & E1000_EICR_MSIX_MASK);
2150     }
2151 }
2152 
2153 static void igb_set_eiam(IGBCore *core, int index, uint32_t val)
2154 {
2155     bool msix = !!(core->mac[GPIE] & E1000_GPIE_MSIX_MODE);
2156 
2157     /*
2158      * TODO: When using IOV, the bits that correspond to MSI-X vectors that
2159      * are assigned to a VF are read-only.
2160      */
2161     core->mac[EIAM] |=
2162         ~(val & (msix ? E1000_EICR_MSIX_MASK : E1000_EICR_LEGACY_MASK));
2163 
2164     trace_igb_irq_write_eiam(val, msix);
2165 }
2166 
2167 static void igb_set_eicr(IGBCore *core, int index, uint32_t val)
2168 {
2169     bool msix = !!(core->mac[GPIE] & E1000_GPIE_MSIX_MODE);
2170 
2171     /*
2172      * TODO: In IOV mode, only bit zero of this vector is available for the PF
2173      * function.
2174      */
2175     core->mac[EICR] &=
2176         ~(val & (msix ? E1000_EICR_MSIX_MASK : E1000_EICR_LEGACY_MASK));
2177 
2178     trace_igb_irq_write_eicr(val, msix);
2179     igb_update_interrupt_state(core);
2180 }
2181 
2182 static void igb_set_vtctrl(IGBCore *core, int index, uint32_t val)
2183 {
2184     uint16_t vfn;
2185 
2186     if (val & E1000_CTRL_RST) {
2187         vfn = (index - PVTCTRL0) / 0x40;
2188         igb_vf_reset(core, vfn);
2189     }
2190 }
2191 
2192 static void igb_set_vteics(IGBCore *core, int index, uint32_t val)
2193 {
2194     uint16_t vfn = (index - PVTEICS0) / 0x40;
2195 
2196     core->mac[index] = val;
2197     igb_set_eics(core, EICS, (val & 0x7) << (22 - vfn * IGBVF_MSIX_VEC_NUM));
2198 }
2199 
2200 static void igb_set_vteims(IGBCore *core, int index, uint32_t val)
2201 {
2202     uint16_t vfn = (index - PVTEIMS0) / 0x40;
2203 
2204     core->mac[index] = val;
2205     igb_set_eims(core, EIMS, (val & 0x7) << (22 - vfn * IGBVF_MSIX_VEC_NUM));
2206 }
2207 
2208 static void igb_set_vteimc(IGBCore *core, int index, uint32_t val)
2209 {
2210     uint16_t vfn = (index - PVTEIMC0) / 0x40;
2211 
2212     core->mac[index] = val;
2213     igb_set_eimc(core, EIMC, (val & 0x7) << (22 - vfn * IGBVF_MSIX_VEC_NUM));
2214 }
2215 
2216 static void igb_set_vteiac(IGBCore *core, int index, uint32_t val)
2217 {
2218     uint16_t vfn = (index - PVTEIAC0) / 0x40;
2219 
2220     core->mac[index] = val;
2221     igb_set_eiac(core, EIAC, (val & 0x7) << (22 - vfn * IGBVF_MSIX_VEC_NUM));
2222 }
2223 
2224 static void igb_set_vteiam(IGBCore *core, int index, uint32_t val)
2225 {
2226     uint16_t vfn = (index - PVTEIAM0) / 0x40;
2227 
2228     core->mac[index] = val;
2229     igb_set_eiam(core, EIAM, (val & 0x7) << (22 - vfn * IGBVF_MSIX_VEC_NUM));
2230 }
2231 
2232 static void igb_set_vteicr(IGBCore *core, int index, uint32_t val)
2233 {
2234     uint16_t vfn = (index - PVTEICR0) / 0x40;
2235 
2236     core->mac[index] = val;
2237     igb_set_eicr(core, EICR, (val & 0x7) << (22 - vfn * IGBVF_MSIX_VEC_NUM));
2238 }
2239 
2240 static void igb_set_vtivar(IGBCore *core, int index, uint32_t val)
2241 {
2242     uint16_t vfn = (index - VTIVAR);
2243     uint16_t qn = vfn;
2244     uint8_t ent;
2245     int n;
2246 
2247     core->mac[index] = val;
2248 
2249     /* Get assigned vector associated with queue Rx#0. */
2250     if ((val & E1000_IVAR_VALID)) {
2251         n = igb_ivar_entry_rx(qn);
2252         ent = E1000_IVAR_VALID | (24 - vfn * IGBVF_MSIX_VEC_NUM - (2 - (val & 0x7)));
2253         core->mac[IVAR0 + n / 4] |= ent << 8 * (n % 4);
2254     }
2255 
2256     /* Get assigned vector associated with queue Tx#0 */
2257     ent = val >> 8;
2258     if ((ent & E1000_IVAR_VALID)) {
2259         n = igb_ivar_entry_tx(qn);
2260         ent = E1000_IVAR_VALID | (24 - vfn * IGBVF_MSIX_VEC_NUM - (2 - (ent & 0x7)));
2261         core->mac[IVAR0 + n / 4] |= ent << 8 * (n % 4);
2262     }
2263 
2264     /*
2265      * Ignoring assigned vectors associated with queues Rx#1 and Tx#1 for now.
2266      */
2267 }
2268 
2269 static inline void
2270 igb_autoneg_timer(void *opaque)
2271 {
2272     IGBCore *core = opaque;
2273     if (!qemu_get_queue(core->owner_nic)->link_down) {
2274         e1000x_update_regs_on_autoneg_done(core->mac, core->phy);
2275         igb_start_recv(core);
2276 
2277         igb_update_flowctl_status(core);
2278         /* signal link status change to the guest */
2279         igb_set_interrupt_cause(core, E1000_ICR_LSC);
2280     }
2281 }
2282 
2283 static inline uint16_t
2284 igb_get_reg_index_with_offset(const uint16_t *mac_reg_access, hwaddr addr)
2285 {
2286     uint16_t index = (addr & 0x1ffff) >> 2;
2287     return index + (mac_reg_access[index] & 0xfffe);
2288 }
2289 
2290 static const char igb_phy_regcap[MAX_PHY_REG_ADDRESS + 1] = {
2291     [MII_BMCR]                   = PHY_RW,
2292     [MII_BMSR]                   = PHY_R,
2293     [MII_PHYID1]                 = PHY_R,
2294     [MII_PHYID2]                 = PHY_R,
2295     [MII_ANAR]                   = PHY_RW,
2296     [MII_ANLPAR]                 = PHY_R,
2297     [MII_ANER]                   = PHY_R,
2298     [MII_ANNP]                   = PHY_RW,
2299     [MII_ANLPRNP]                = PHY_R,
2300     [MII_CTRL1000]               = PHY_RW,
2301     [MII_STAT1000]               = PHY_R,
2302     [MII_EXTSTAT]                = PHY_R,
2303 
2304     [IGP01E1000_PHY_PORT_CONFIG] = PHY_RW,
2305     [IGP01E1000_PHY_PORT_STATUS] = PHY_R,
2306     [IGP01E1000_PHY_PORT_CTRL]   = PHY_RW,
2307     [IGP01E1000_PHY_LINK_HEALTH] = PHY_R,
2308     [IGP02E1000_PHY_POWER_MGMT]  = PHY_RW,
2309     [IGP01E1000_PHY_PAGE_SELECT] = PHY_W
2310 };
2311 
2312 static void
2313 igb_phy_reg_write(IGBCore *core, uint32_t addr, uint16_t data)
2314 {
2315     assert(addr <= MAX_PHY_REG_ADDRESS);
2316 
2317     if (addr == MII_BMCR) {
2318         igb_set_phy_ctrl(core, data);
2319     } else {
2320         core->phy[addr] = data;
2321     }
2322 }
2323 
2324 static void
2325 igb_set_mdic(IGBCore *core, int index, uint32_t val)
2326 {
2327     uint32_t data = val & E1000_MDIC_DATA_MASK;
2328     uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT);
2329 
2330     if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) { /* phy # */
2331         val = core->mac[MDIC] | E1000_MDIC_ERROR;
2332     } else if (val & E1000_MDIC_OP_READ) {
2333         if (!(igb_phy_regcap[addr] & PHY_R)) {
2334             trace_igb_core_mdic_read_unhandled(addr);
2335             val |= E1000_MDIC_ERROR;
2336         } else {
2337             val = (val ^ data) | core->phy[addr];
2338             trace_igb_core_mdic_read(addr, val);
2339         }
2340     } else if (val & E1000_MDIC_OP_WRITE) {
2341         if (!(igb_phy_regcap[addr] & PHY_W)) {
2342             trace_igb_core_mdic_write_unhandled(addr);
2343             val |= E1000_MDIC_ERROR;
2344         } else {
2345             trace_igb_core_mdic_write(addr, data);
2346             igb_phy_reg_write(core, addr, data);
2347         }
2348     }
2349     core->mac[MDIC] = val | E1000_MDIC_READY;
2350 
2351     if (val & E1000_MDIC_INT_EN) {
2352         igb_set_interrupt_cause(core, E1000_ICR_MDAC);
2353     }
2354 }
2355 
2356 static void
2357 igb_set_rdt(IGBCore *core, int index, uint32_t val)
2358 {
2359     core->mac[index] = val & 0xffff;
2360     trace_e1000e_rx_set_rdt(igb_mq_queue_idx(RDT0, index), val);
2361     igb_start_recv(core);
2362 }
2363 
2364 static void
2365 igb_set_status(IGBCore *core, int index, uint32_t val)
2366 {
2367     if ((val & E1000_STATUS_PHYRA) == 0) {
2368         core->mac[index] &= ~E1000_STATUS_PHYRA;
2369     }
2370 }
2371 
2372 static void
2373 igb_set_ctrlext(IGBCore *core, int index, uint32_t val)
2374 {
2375     trace_igb_link_set_ext_params(!!(val & E1000_CTRL_EXT_ASDCHK),
2376                                   !!(val & E1000_CTRL_EXT_SPD_BYPS),
2377                                   !!(val & E1000_CTRL_EXT_PFRSTD));
2378 
2379     /* Zero self-clearing bits */
2380     val &= ~(E1000_CTRL_EXT_ASDCHK | E1000_CTRL_EXT_EE_RST);
2381     core->mac[CTRL_EXT] = val;
2382 
2383     if (core->mac[CTRL_EXT] & E1000_CTRL_EXT_PFRSTD) {
2384         for (int vfn = 0; vfn < IGB_MAX_VF_FUNCTIONS; vfn++) {
2385             core->mac[V2PMAILBOX0 + vfn] &= ~E1000_V2PMAILBOX_RSTI;
2386             core->mac[V2PMAILBOX0 + vfn] |= E1000_V2PMAILBOX_RSTD;
2387         }
2388     }
2389 }
2390 
2391 static void
2392 igb_set_pbaclr(IGBCore *core, int index, uint32_t val)
2393 {
2394     int i;
2395 
2396     core->mac[PBACLR] = val & E1000_PBACLR_VALID_MASK;
2397 
2398     if (!msix_enabled(core->owner)) {
2399         return;
2400     }
2401 
2402     for (i = 0; i < IGB_INTR_NUM; i++) {
2403         if (core->mac[PBACLR] & BIT(i)) {
2404             msix_clr_pending(core->owner, i);
2405         }
2406     }
2407 }
2408 
2409 static void
2410 igb_set_fcrth(IGBCore *core, int index, uint32_t val)
2411 {
2412     core->mac[FCRTH] = val & 0xFFF8;
2413 }
2414 
2415 static void
2416 igb_set_fcrtl(IGBCore *core, int index, uint32_t val)
2417 {
2418     core->mac[FCRTL] = val & 0x8000FFF8;
2419 }
2420 
2421 #define IGB_LOW_BITS_SET_FUNC(num)                             \
2422     static void                                                \
2423     igb_set_##num##bit(IGBCore *core, int index, uint32_t val) \
2424     {                                                          \
2425         core->mac[index] = val & (BIT(num) - 1);               \
2426     }
2427 
2428 IGB_LOW_BITS_SET_FUNC(4)
2429 IGB_LOW_BITS_SET_FUNC(13)
2430 IGB_LOW_BITS_SET_FUNC(16)
2431 
2432 static void
2433 igb_set_dlen(IGBCore *core, int index, uint32_t val)
2434 {
2435     core->mac[index] = val & 0xffff0;
2436 }
2437 
2438 static void
2439 igb_set_dbal(IGBCore *core, int index, uint32_t val)
2440 {
2441     core->mac[index] = val & E1000_XDBAL_MASK;
2442 }
2443 
2444 static void
2445 igb_set_tdt(IGBCore *core, int index, uint32_t val)
2446 {
2447     IGB_TxRing txr;
2448     int qn = igb_mq_queue_idx(TDT0, index);
2449 
2450     core->mac[index] = val & 0xffff;
2451 
2452     igb_tx_ring_init(core, &txr, qn);
2453     igb_start_xmit(core, &txr);
2454 }
2455 
2456 static void
2457 igb_set_ics(IGBCore *core, int index, uint32_t val)
2458 {
2459     trace_e1000e_irq_write_ics(val);
2460     igb_set_interrupt_cause(core, val);
2461 }
2462 
2463 static void
2464 igb_set_imc(IGBCore *core, int index, uint32_t val)
2465 {
2466     trace_e1000e_irq_ims_clear_set_imc(val);
2467     igb_clear_ims_bits(core, val);
2468     igb_update_interrupt_state(core);
2469 }
2470 
2471 static void
2472 igb_set_ims(IGBCore *core, int index, uint32_t val)
2473 {
2474     uint32_t valid_val = val & 0x77D4FBFD;
2475 
2476     trace_e1000e_irq_set_ims(val, core->mac[IMS], core->mac[IMS] | valid_val);
2477     core->mac[IMS] |= valid_val;
2478     igb_update_interrupt_state(core);
2479 }
2480 
2481 static void igb_commit_icr(IGBCore *core)
2482 {
2483     /*
2484      * If GPIE.NSICR = 0, then the clear of IMS will occur only if at
2485      * least one bit is set in the IMS and there is a true interrupt as
2486      * reflected in ICR.INTA.
2487      */
2488     if ((core->mac[GPIE] & E1000_GPIE_NSICR) ||
2489         (core->mac[IMS] && (core->mac[ICR] & E1000_ICR_INT_ASSERTED))) {
2490         igb_clear_ims_bits(core, core->mac[IAM]);
2491     }
2492 
2493     igb_update_interrupt_state(core);
2494 }
2495 
2496 static void igb_set_icr(IGBCore *core, int index, uint32_t val)
2497 {
2498     uint32_t icr = core->mac[ICR] & ~val;
2499 
2500     trace_igb_irq_icr_write(val, core->mac[ICR], icr);
2501     core->mac[ICR] = icr;
2502     igb_commit_icr(core);
2503 }
2504 
2505 static uint32_t
2506 igb_mac_readreg(IGBCore *core, int index)
2507 {
2508     return core->mac[index];
2509 }
2510 
2511 static uint32_t
2512 igb_mac_ics_read(IGBCore *core, int index)
2513 {
2514     trace_e1000e_irq_read_ics(core->mac[ICS]);
2515     return core->mac[ICS];
2516 }
2517 
2518 static uint32_t
2519 igb_mac_ims_read(IGBCore *core, int index)
2520 {
2521     trace_e1000e_irq_read_ims(core->mac[IMS]);
2522     return core->mac[IMS];
2523 }
2524 
2525 static uint32_t
2526 igb_mac_swsm_read(IGBCore *core, int index)
2527 {
2528     uint32_t val = core->mac[SWSM];
2529     core->mac[SWSM] = val | E1000_SWSM_SMBI;
2530     return val;
2531 }
2532 
2533 static uint32_t
2534 igb_mac_eitr_read(IGBCore *core, int index)
2535 {
2536     return core->eitr_guest_value[index - EITR0];
2537 }
2538 
2539 static uint32_t igb_mac_vfmailbox_read(IGBCore *core, int index)
2540 {
2541     uint32_t val = core->mac[index];
2542 
2543     core->mac[index] &= ~(E1000_V2PMAILBOX_PFSTS | E1000_V2PMAILBOX_PFACK |
2544                           E1000_V2PMAILBOX_RSTD);
2545 
2546     return val;
2547 }
2548 
2549 static uint32_t
2550 igb_mac_icr_read(IGBCore *core, int index)
2551 {
2552     uint32_t ret = core->mac[ICR];
2553     trace_e1000e_irq_icr_read_entry(ret);
2554 
2555     if (core->mac[GPIE] & E1000_GPIE_NSICR) {
2556         trace_igb_irq_icr_clear_gpie_nsicr();
2557         core->mac[ICR] = 0;
2558     } else if (core->mac[IMS] == 0) {
2559         trace_e1000e_irq_icr_clear_zero_ims();
2560         core->mac[ICR] = 0;
2561     } else if (!msix_enabled(core->owner)) {
2562         trace_e1000e_irq_icr_clear_nonmsix_icr_read();
2563         core->mac[ICR] = 0;
2564     }
2565 
2566     trace_e1000e_irq_icr_read_exit(core->mac[ICR]);
2567     igb_commit_icr(core);
2568     return ret;
2569 }
2570 
2571 static uint32_t
2572 igb_mac_read_clr4(IGBCore *core, int index)
2573 {
2574     uint32_t ret = core->mac[index];
2575 
2576     core->mac[index] = 0;
2577     return ret;
2578 }
2579 
2580 static uint32_t
2581 igb_mac_read_clr8(IGBCore *core, int index)
2582 {
2583     uint32_t ret = core->mac[index];
2584 
2585     core->mac[index] = 0;
2586     core->mac[index - 1] = 0;
2587     return ret;
2588 }
2589 
2590 static uint32_t
2591 igb_get_ctrl(IGBCore *core, int index)
2592 {
2593     uint32_t val = core->mac[CTRL];
2594 
2595     trace_e1000e_link_read_params(
2596         !!(val & E1000_CTRL_ASDE),
2597         (val & E1000_CTRL_SPD_SEL) >> E1000_CTRL_SPD_SHIFT,
2598         !!(val & E1000_CTRL_FRCSPD),
2599         !!(val & E1000_CTRL_FRCDPX),
2600         !!(val & E1000_CTRL_RFCE),
2601         !!(val & E1000_CTRL_TFCE));
2602 
2603     return val;
2604 }
2605 
2606 static uint32_t igb_get_status(IGBCore *core, int index)
2607 {
2608     uint32_t res = core->mac[STATUS];
2609     uint16_t num_vfs = pcie_sriov_num_vfs(core->owner);
2610 
2611     if (core->mac[CTRL] & E1000_CTRL_FRCDPX) {
2612         res |= (core->mac[CTRL] & E1000_CTRL_FD) ? E1000_STATUS_FD : 0;
2613     } else {
2614         res |= E1000_STATUS_FD;
2615     }
2616 
2617     if ((core->mac[CTRL] & E1000_CTRL_FRCSPD) ||
2618         (core->mac[CTRL_EXT] & E1000_CTRL_EXT_SPD_BYPS)) {
2619         switch (core->mac[CTRL] & E1000_CTRL_SPD_SEL) {
2620         case E1000_CTRL_SPD_10:
2621             res |= E1000_STATUS_SPEED_10;
2622             break;
2623         case E1000_CTRL_SPD_100:
2624             res |= E1000_STATUS_SPEED_100;
2625             break;
2626         case E1000_CTRL_SPD_1000:
2627         default:
2628             res |= E1000_STATUS_SPEED_1000;
2629             break;
2630         }
2631     } else {
2632         res |= E1000_STATUS_SPEED_1000;
2633     }
2634 
2635     if (num_vfs) {
2636         res |= num_vfs << E1000_STATUS_NUM_VFS_SHIFT;
2637         res |= E1000_STATUS_IOV_MODE;
2638     }
2639 
2640     /*
2641      * Windows driver 12.18.9.23 resets if E1000_STATUS_GIO_MASTER_ENABLE is
2642      * left set after E1000_CTRL_LRST is set.
2643      */
2644     if (!(core->mac[CTRL] & E1000_CTRL_GIO_MASTER_DISABLE) &&
2645         !(core->mac[CTRL] & E1000_CTRL_LRST)) {
2646         res |= E1000_STATUS_GIO_MASTER_ENABLE;
2647     }
2648 
2649     return res;
2650 }
2651 
2652 static void
2653 igb_mac_writereg(IGBCore *core, int index, uint32_t val)
2654 {
2655     core->mac[index] = val;
2656 }
2657 
2658 static void
2659 igb_mac_setmacaddr(IGBCore *core, int index, uint32_t val)
2660 {
2661     uint32_t macaddr[2];
2662 
2663     core->mac[index] = val;
2664 
2665     macaddr[0] = cpu_to_le32(core->mac[RA]);
2666     macaddr[1] = cpu_to_le32(core->mac[RA + 1]);
2667     qemu_format_nic_info_str(qemu_get_queue(core->owner_nic),
2668         (uint8_t *) macaddr);
2669 
2670     trace_e1000e_mac_set_sw(MAC_ARG(macaddr));
2671 }
2672 
2673 static void
2674 igb_set_eecd(IGBCore *core, int index, uint32_t val)
2675 {
2676     static const uint32_t ro_bits = E1000_EECD_PRES          |
2677                                     E1000_EECD_AUTO_RD       |
2678                                     E1000_EECD_SIZE_EX_MASK;
2679 
2680     core->mac[EECD] = (core->mac[EECD] & ro_bits) | (val & ~ro_bits);
2681 }
2682 
2683 static void
2684 igb_set_eerd(IGBCore *core, int index, uint32_t val)
2685 {
2686     uint32_t addr = (val >> E1000_EERW_ADDR_SHIFT) & E1000_EERW_ADDR_MASK;
2687     uint32_t flags = 0;
2688     uint32_t data = 0;
2689 
2690     if ((addr < IGB_EEPROM_SIZE) && (val & E1000_EERW_START)) {
2691         data = core->eeprom[addr];
2692         flags = E1000_EERW_DONE;
2693     }
2694 
2695     core->mac[EERD] = flags                           |
2696                       (addr << E1000_EERW_ADDR_SHIFT) |
2697                       (data << E1000_EERW_DATA_SHIFT);
2698 }
2699 
2700 static void
2701 igb_set_eitr(IGBCore *core, int index, uint32_t val)
2702 {
2703     uint32_t eitr_num = index - EITR0;
2704 
2705     trace_igb_irq_eitr_set(eitr_num, val);
2706 
2707     core->eitr_guest_value[eitr_num] = val & ~E1000_EITR_CNT_IGNR;
2708     core->mac[index] = val & 0x7FFE;
2709 }
2710 
2711 static void
2712 igb_update_rx_offloads(IGBCore *core)
2713 {
2714     int cso_state = igb_rx_l4_cso_enabled(core);
2715 
2716     trace_e1000e_rx_set_cso(cso_state);
2717 
2718     if (core->has_vnet) {
2719         qemu_set_offload(qemu_get_queue(core->owner_nic)->peer,
2720                          cso_state, 0, 0, 0, 0);
2721     }
2722 }
2723 
2724 static void
2725 igb_set_rxcsum(IGBCore *core, int index, uint32_t val)
2726 {
2727     core->mac[RXCSUM] = val;
2728     igb_update_rx_offloads(core);
2729 }
2730 
2731 static void
2732 igb_set_gcr(IGBCore *core, int index, uint32_t val)
2733 {
2734     uint32_t ro_bits = core->mac[GCR] & E1000_GCR_RO_BITS;
2735     core->mac[GCR] = (val & ~E1000_GCR_RO_BITS) | ro_bits;
2736 }
2737 
2738 static uint32_t igb_get_systiml(IGBCore *core, int index)
2739 {
2740     e1000x_timestamp(core->mac, core->timadj, SYSTIML, SYSTIMH);
2741     return core->mac[SYSTIML];
2742 }
2743 
2744 static uint32_t igb_get_rxsatrh(IGBCore *core, int index)
2745 {
2746     core->mac[TSYNCRXCTL] &= ~E1000_TSYNCRXCTL_VALID;
2747     return core->mac[RXSATRH];
2748 }
2749 
2750 static uint32_t igb_get_txstmph(IGBCore *core, int index)
2751 {
2752     core->mac[TSYNCTXCTL] &= ~E1000_TSYNCTXCTL_VALID;
2753     return core->mac[TXSTMPH];
2754 }
2755 
2756 static void igb_set_timinca(IGBCore *core, int index, uint32_t val)
2757 {
2758     e1000x_set_timinca(core->mac, &core->timadj, val);
2759 }
2760 
2761 static void igb_set_timadjh(IGBCore *core, int index, uint32_t val)
2762 {
2763     core->mac[TIMADJH] = val;
2764     core->timadj += core->mac[TIMADJL] | ((int64_t)core->mac[TIMADJH] << 32);
2765 }
2766 
2767 #define igb_getreg(x)    [x] = igb_mac_readreg
2768 typedef uint32_t (*readops)(IGBCore *, int);
2769 static const readops igb_macreg_readops[] = {
2770     igb_getreg(WUFC),
2771     igb_getreg(MANC),
2772     igb_getreg(TOTL),
2773     igb_getreg(RDT0),
2774     igb_getreg(RDT1),
2775     igb_getreg(RDT2),
2776     igb_getreg(RDT3),
2777     igb_getreg(RDT4),
2778     igb_getreg(RDT5),
2779     igb_getreg(RDT6),
2780     igb_getreg(RDT7),
2781     igb_getreg(RDT8),
2782     igb_getreg(RDT9),
2783     igb_getreg(RDT10),
2784     igb_getreg(RDT11),
2785     igb_getreg(RDT12),
2786     igb_getreg(RDT13),
2787     igb_getreg(RDT14),
2788     igb_getreg(RDT15),
2789     igb_getreg(RDBAH0),
2790     igb_getreg(RDBAH1),
2791     igb_getreg(RDBAH2),
2792     igb_getreg(RDBAH3),
2793     igb_getreg(RDBAH4),
2794     igb_getreg(RDBAH5),
2795     igb_getreg(RDBAH6),
2796     igb_getreg(RDBAH7),
2797     igb_getreg(RDBAH8),
2798     igb_getreg(RDBAH9),
2799     igb_getreg(RDBAH10),
2800     igb_getreg(RDBAH11),
2801     igb_getreg(RDBAH12),
2802     igb_getreg(RDBAH13),
2803     igb_getreg(RDBAH14),
2804     igb_getreg(RDBAH15),
2805     igb_getreg(TDBAL0),
2806     igb_getreg(TDBAL1),
2807     igb_getreg(TDBAL2),
2808     igb_getreg(TDBAL3),
2809     igb_getreg(TDBAL4),
2810     igb_getreg(TDBAL5),
2811     igb_getreg(TDBAL6),
2812     igb_getreg(TDBAL7),
2813     igb_getreg(TDBAL8),
2814     igb_getreg(TDBAL9),
2815     igb_getreg(TDBAL10),
2816     igb_getreg(TDBAL11),
2817     igb_getreg(TDBAL12),
2818     igb_getreg(TDBAL13),
2819     igb_getreg(TDBAL14),
2820     igb_getreg(TDBAL15),
2821     igb_getreg(RDLEN0),
2822     igb_getreg(RDLEN1),
2823     igb_getreg(RDLEN2),
2824     igb_getreg(RDLEN3),
2825     igb_getreg(RDLEN4),
2826     igb_getreg(RDLEN5),
2827     igb_getreg(RDLEN6),
2828     igb_getreg(RDLEN7),
2829     igb_getreg(RDLEN8),
2830     igb_getreg(RDLEN9),
2831     igb_getreg(RDLEN10),
2832     igb_getreg(RDLEN11),
2833     igb_getreg(RDLEN12),
2834     igb_getreg(RDLEN13),
2835     igb_getreg(RDLEN14),
2836     igb_getreg(RDLEN15),
2837     igb_getreg(SRRCTL0),
2838     igb_getreg(SRRCTL1),
2839     igb_getreg(SRRCTL2),
2840     igb_getreg(SRRCTL3),
2841     igb_getreg(SRRCTL4),
2842     igb_getreg(SRRCTL5),
2843     igb_getreg(SRRCTL6),
2844     igb_getreg(SRRCTL7),
2845     igb_getreg(SRRCTL8),
2846     igb_getreg(SRRCTL9),
2847     igb_getreg(SRRCTL10),
2848     igb_getreg(SRRCTL11),
2849     igb_getreg(SRRCTL12),
2850     igb_getreg(SRRCTL13),
2851     igb_getreg(SRRCTL14),
2852     igb_getreg(SRRCTL15),
2853     igb_getreg(LATECOL),
2854     igb_getreg(XONTXC),
2855     igb_getreg(TDFH),
2856     igb_getreg(TDFT),
2857     igb_getreg(TDFHS),
2858     igb_getreg(TDFTS),
2859     igb_getreg(TDFPC),
2860     igb_getreg(WUS),
2861     igb_getreg(RDFH),
2862     igb_getreg(RDFT),
2863     igb_getreg(RDFHS),
2864     igb_getreg(RDFTS),
2865     igb_getreg(RDFPC),
2866     igb_getreg(GORCL),
2867     igb_getreg(MGTPRC),
2868     igb_getreg(EERD),
2869     igb_getreg(EIAC),
2870     igb_getreg(MANC2H),
2871     igb_getreg(RXCSUM),
2872     igb_getreg(GSCL_3),
2873     igb_getreg(GSCN_2),
2874     igb_getreg(FCAH),
2875     igb_getreg(FCRTH),
2876     igb_getreg(FLOP),
2877     igb_getreg(RXSTMPH),
2878     igb_getreg(TXSTMPL),
2879     igb_getreg(TIMADJL),
2880     igb_getreg(RDH0),
2881     igb_getreg(RDH1),
2882     igb_getreg(RDH2),
2883     igb_getreg(RDH3),
2884     igb_getreg(RDH4),
2885     igb_getreg(RDH5),
2886     igb_getreg(RDH6),
2887     igb_getreg(RDH7),
2888     igb_getreg(RDH8),
2889     igb_getreg(RDH9),
2890     igb_getreg(RDH10),
2891     igb_getreg(RDH11),
2892     igb_getreg(RDH12),
2893     igb_getreg(RDH13),
2894     igb_getreg(RDH14),
2895     igb_getreg(RDH15),
2896     igb_getreg(TDT0),
2897     igb_getreg(TDT1),
2898     igb_getreg(TDT2),
2899     igb_getreg(TDT3),
2900     igb_getreg(TDT4),
2901     igb_getreg(TDT5),
2902     igb_getreg(TDT6),
2903     igb_getreg(TDT7),
2904     igb_getreg(TDT8),
2905     igb_getreg(TDT9),
2906     igb_getreg(TDT10),
2907     igb_getreg(TDT11),
2908     igb_getreg(TDT12),
2909     igb_getreg(TDT13),
2910     igb_getreg(TDT14),
2911     igb_getreg(TDT15),
2912     igb_getreg(TNCRS),
2913     igb_getreg(RJC),
2914     igb_getreg(IAM),
2915     igb_getreg(GSCL_2),
2916     igb_getreg(TIPG),
2917     igb_getreg(FLMNGCTL),
2918     igb_getreg(FLMNGCNT),
2919     igb_getreg(TSYNCTXCTL),
2920     igb_getreg(EEMNGDATA),
2921     igb_getreg(CTRL_EXT),
2922     igb_getreg(SYSTIMH),
2923     igb_getreg(EEMNGCTL),
2924     igb_getreg(FLMNGDATA),
2925     igb_getreg(TSYNCRXCTL),
2926     igb_getreg(LEDCTL),
2927     igb_getreg(TCTL),
2928     igb_getreg(TCTL_EXT),
2929     igb_getreg(DTXCTL),
2930     igb_getreg(RXPBS),
2931     igb_getreg(TDH0),
2932     igb_getreg(TDH1),
2933     igb_getreg(TDH2),
2934     igb_getreg(TDH3),
2935     igb_getreg(TDH4),
2936     igb_getreg(TDH5),
2937     igb_getreg(TDH6),
2938     igb_getreg(TDH7),
2939     igb_getreg(TDH8),
2940     igb_getreg(TDH9),
2941     igb_getreg(TDH10),
2942     igb_getreg(TDH11),
2943     igb_getreg(TDH12),
2944     igb_getreg(TDH13),
2945     igb_getreg(TDH14),
2946     igb_getreg(TDH15),
2947     igb_getreg(ECOL),
2948     igb_getreg(DC),
2949     igb_getreg(RLEC),
2950     igb_getreg(XOFFTXC),
2951     igb_getreg(RFC),
2952     igb_getreg(RNBC),
2953     igb_getreg(MGTPTC),
2954     igb_getreg(TIMINCA),
2955     igb_getreg(FACTPS),
2956     igb_getreg(GSCL_1),
2957     igb_getreg(GSCN_0),
2958     igb_getreg(PBACLR),
2959     igb_getreg(FCTTV),
2960     igb_getreg(RXSATRL),
2961     igb_getreg(TORL),
2962     igb_getreg(TDLEN0),
2963     igb_getreg(TDLEN1),
2964     igb_getreg(TDLEN2),
2965     igb_getreg(TDLEN3),
2966     igb_getreg(TDLEN4),
2967     igb_getreg(TDLEN5),
2968     igb_getreg(TDLEN6),
2969     igb_getreg(TDLEN7),
2970     igb_getreg(TDLEN8),
2971     igb_getreg(TDLEN9),
2972     igb_getreg(TDLEN10),
2973     igb_getreg(TDLEN11),
2974     igb_getreg(TDLEN12),
2975     igb_getreg(TDLEN13),
2976     igb_getreg(TDLEN14),
2977     igb_getreg(TDLEN15),
2978     igb_getreg(MCC),
2979     igb_getreg(WUC),
2980     igb_getreg(EECD),
2981     igb_getreg(FCRTV),
2982     igb_getreg(TXDCTL0),
2983     igb_getreg(TXDCTL1),
2984     igb_getreg(TXDCTL2),
2985     igb_getreg(TXDCTL3),
2986     igb_getreg(TXDCTL4),
2987     igb_getreg(TXDCTL5),
2988     igb_getreg(TXDCTL6),
2989     igb_getreg(TXDCTL7),
2990     igb_getreg(TXDCTL8),
2991     igb_getreg(TXDCTL9),
2992     igb_getreg(TXDCTL10),
2993     igb_getreg(TXDCTL11),
2994     igb_getreg(TXDCTL12),
2995     igb_getreg(TXDCTL13),
2996     igb_getreg(TXDCTL14),
2997     igb_getreg(TXDCTL15),
2998     igb_getreg(TXCTL0),
2999     igb_getreg(TXCTL1),
3000     igb_getreg(TXCTL2),
3001     igb_getreg(TXCTL3),
3002     igb_getreg(TXCTL4),
3003     igb_getreg(TXCTL5),
3004     igb_getreg(TXCTL6),
3005     igb_getreg(TXCTL7),
3006     igb_getreg(TXCTL8),
3007     igb_getreg(TXCTL9),
3008     igb_getreg(TXCTL10),
3009     igb_getreg(TXCTL11),
3010     igb_getreg(TXCTL12),
3011     igb_getreg(TXCTL13),
3012     igb_getreg(TXCTL14),
3013     igb_getreg(TXCTL15),
3014     igb_getreg(TDWBAL0),
3015     igb_getreg(TDWBAL1),
3016     igb_getreg(TDWBAL2),
3017     igb_getreg(TDWBAL3),
3018     igb_getreg(TDWBAL4),
3019     igb_getreg(TDWBAL5),
3020     igb_getreg(TDWBAL6),
3021     igb_getreg(TDWBAL7),
3022     igb_getreg(TDWBAL8),
3023     igb_getreg(TDWBAL9),
3024     igb_getreg(TDWBAL10),
3025     igb_getreg(TDWBAL11),
3026     igb_getreg(TDWBAL12),
3027     igb_getreg(TDWBAL13),
3028     igb_getreg(TDWBAL14),
3029     igb_getreg(TDWBAL15),
3030     igb_getreg(TDWBAH0),
3031     igb_getreg(TDWBAH1),
3032     igb_getreg(TDWBAH2),
3033     igb_getreg(TDWBAH3),
3034     igb_getreg(TDWBAH4),
3035     igb_getreg(TDWBAH5),
3036     igb_getreg(TDWBAH6),
3037     igb_getreg(TDWBAH7),
3038     igb_getreg(TDWBAH8),
3039     igb_getreg(TDWBAH9),
3040     igb_getreg(TDWBAH10),
3041     igb_getreg(TDWBAH11),
3042     igb_getreg(TDWBAH12),
3043     igb_getreg(TDWBAH13),
3044     igb_getreg(TDWBAH14),
3045     igb_getreg(TDWBAH15),
3046     igb_getreg(PVTCTRL0),
3047     igb_getreg(PVTCTRL1),
3048     igb_getreg(PVTCTRL2),
3049     igb_getreg(PVTCTRL3),
3050     igb_getreg(PVTCTRL4),
3051     igb_getreg(PVTCTRL5),
3052     igb_getreg(PVTCTRL6),
3053     igb_getreg(PVTCTRL7),
3054     igb_getreg(PVTEIMS0),
3055     igb_getreg(PVTEIMS1),
3056     igb_getreg(PVTEIMS2),
3057     igb_getreg(PVTEIMS3),
3058     igb_getreg(PVTEIMS4),
3059     igb_getreg(PVTEIMS5),
3060     igb_getreg(PVTEIMS6),
3061     igb_getreg(PVTEIMS7),
3062     igb_getreg(PVTEIAC0),
3063     igb_getreg(PVTEIAC1),
3064     igb_getreg(PVTEIAC2),
3065     igb_getreg(PVTEIAC3),
3066     igb_getreg(PVTEIAC4),
3067     igb_getreg(PVTEIAC5),
3068     igb_getreg(PVTEIAC6),
3069     igb_getreg(PVTEIAC7),
3070     igb_getreg(PVTEIAM0),
3071     igb_getreg(PVTEIAM1),
3072     igb_getreg(PVTEIAM2),
3073     igb_getreg(PVTEIAM3),
3074     igb_getreg(PVTEIAM4),
3075     igb_getreg(PVTEIAM5),
3076     igb_getreg(PVTEIAM6),
3077     igb_getreg(PVTEIAM7),
3078     igb_getreg(PVFGPRC0),
3079     igb_getreg(PVFGPRC1),
3080     igb_getreg(PVFGPRC2),
3081     igb_getreg(PVFGPRC3),
3082     igb_getreg(PVFGPRC4),
3083     igb_getreg(PVFGPRC5),
3084     igb_getreg(PVFGPRC6),
3085     igb_getreg(PVFGPRC7),
3086     igb_getreg(PVFGPTC0),
3087     igb_getreg(PVFGPTC1),
3088     igb_getreg(PVFGPTC2),
3089     igb_getreg(PVFGPTC3),
3090     igb_getreg(PVFGPTC4),
3091     igb_getreg(PVFGPTC5),
3092     igb_getreg(PVFGPTC6),
3093     igb_getreg(PVFGPTC7),
3094     igb_getreg(PVFGORC0),
3095     igb_getreg(PVFGORC1),
3096     igb_getreg(PVFGORC2),
3097     igb_getreg(PVFGORC3),
3098     igb_getreg(PVFGORC4),
3099     igb_getreg(PVFGORC5),
3100     igb_getreg(PVFGORC6),
3101     igb_getreg(PVFGORC7),
3102     igb_getreg(PVFGOTC0),
3103     igb_getreg(PVFGOTC1),
3104     igb_getreg(PVFGOTC2),
3105     igb_getreg(PVFGOTC3),
3106     igb_getreg(PVFGOTC4),
3107     igb_getreg(PVFGOTC5),
3108     igb_getreg(PVFGOTC6),
3109     igb_getreg(PVFGOTC7),
3110     igb_getreg(PVFMPRC0),
3111     igb_getreg(PVFMPRC1),
3112     igb_getreg(PVFMPRC2),
3113     igb_getreg(PVFMPRC3),
3114     igb_getreg(PVFMPRC4),
3115     igb_getreg(PVFMPRC5),
3116     igb_getreg(PVFMPRC6),
3117     igb_getreg(PVFMPRC7),
3118     igb_getreg(PVFGPRLBC0),
3119     igb_getreg(PVFGPRLBC1),
3120     igb_getreg(PVFGPRLBC2),
3121     igb_getreg(PVFGPRLBC3),
3122     igb_getreg(PVFGPRLBC4),
3123     igb_getreg(PVFGPRLBC5),
3124     igb_getreg(PVFGPRLBC6),
3125     igb_getreg(PVFGPRLBC7),
3126     igb_getreg(PVFGPTLBC0),
3127     igb_getreg(PVFGPTLBC1),
3128     igb_getreg(PVFGPTLBC2),
3129     igb_getreg(PVFGPTLBC3),
3130     igb_getreg(PVFGPTLBC4),
3131     igb_getreg(PVFGPTLBC5),
3132     igb_getreg(PVFGPTLBC6),
3133     igb_getreg(PVFGPTLBC7),
3134     igb_getreg(PVFGORLBC0),
3135     igb_getreg(PVFGORLBC1),
3136     igb_getreg(PVFGORLBC2),
3137     igb_getreg(PVFGORLBC3),
3138     igb_getreg(PVFGORLBC4),
3139     igb_getreg(PVFGORLBC5),
3140     igb_getreg(PVFGORLBC6),
3141     igb_getreg(PVFGORLBC7),
3142     igb_getreg(PVFGOTLBC0),
3143     igb_getreg(PVFGOTLBC1),
3144     igb_getreg(PVFGOTLBC2),
3145     igb_getreg(PVFGOTLBC3),
3146     igb_getreg(PVFGOTLBC4),
3147     igb_getreg(PVFGOTLBC5),
3148     igb_getreg(PVFGOTLBC6),
3149     igb_getreg(PVFGOTLBC7),
3150     igb_getreg(RCTL),
3151     igb_getreg(MDIC),
3152     igb_getreg(FCRUC),
3153     igb_getreg(VET),
3154     igb_getreg(RDBAL0),
3155     igb_getreg(RDBAL1),
3156     igb_getreg(RDBAL2),
3157     igb_getreg(RDBAL3),
3158     igb_getreg(RDBAL4),
3159     igb_getreg(RDBAL5),
3160     igb_getreg(RDBAL6),
3161     igb_getreg(RDBAL7),
3162     igb_getreg(RDBAL8),
3163     igb_getreg(RDBAL9),
3164     igb_getreg(RDBAL10),
3165     igb_getreg(RDBAL11),
3166     igb_getreg(RDBAL12),
3167     igb_getreg(RDBAL13),
3168     igb_getreg(RDBAL14),
3169     igb_getreg(RDBAL15),
3170     igb_getreg(TDBAH0),
3171     igb_getreg(TDBAH1),
3172     igb_getreg(TDBAH2),
3173     igb_getreg(TDBAH3),
3174     igb_getreg(TDBAH4),
3175     igb_getreg(TDBAH5),
3176     igb_getreg(TDBAH6),
3177     igb_getreg(TDBAH7),
3178     igb_getreg(TDBAH8),
3179     igb_getreg(TDBAH9),
3180     igb_getreg(TDBAH10),
3181     igb_getreg(TDBAH11),
3182     igb_getreg(TDBAH12),
3183     igb_getreg(TDBAH13),
3184     igb_getreg(TDBAH14),
3185     igb_getreg(TDBAH15),
3186     igb_getreg(SCC),
3187     igb_getreg(COLC),
3188     igb_getreg(XOFFRXC),
3189     igb_getreg(IPAV),
3190     igb_getreg(GOTCL),
3191     igb_getreg(MGTPDC),
3192     igb_getreg(GCR),
3193     igb_getreg(MFVAL),
3194     igb_getreg(FUNCTAG),
3195     igb_getreg(GSCL_4),
3196     igb_getreg(GSCN_3),
3197     igb_getreg(MRQC),
3198     igb_getreg(FCT),
3199     igb_getreg(FLA),
3200     igb_getreg(RXDCTL0),
3201     igb_getreg(RXDCTL1),
3202     igb_getreg(RXDCTL2),
3203     igb_getreg(RXDCTL3),
3204     igb_getreg(RXDCTL4),
3205     igb_getreg(RXDCTL5),
3206     igb_getreg(RXDCTL6),
3207     igb_getreg(RXDCTL7),
3208     igb_getreg(RXDCTL8),
3209     igb_getreg(RXDCTL9),
3210     igb_getreg(RXDCTL10),
3211     igb_getreg(RXDCTL11),
3212     igb_getreg(RXDCTL12),
3213     igb_getreg(RXDCTL13),
3214     igb_getreg(RXDCTL14),
3215     igb_getreg(RXDCTL15),
3216     igb_getreg(RXSTMPL),
3217     igb_getreg(TIMADJH),
3218     igb_getreg(FCRTL),
3219     igb_getreg(XONRXC),
3220     igb_getreg(RFCTL),
3221     igb_getreg(GSCN_1),
3222     igb_getreg(FCAL),
3223     igb_getreg(GPIE),
3224     igb_getreg(TXPBS),
3225     igb_getreg(RLPML),
3226 
3227     [TOTH]    = igb_mac_read_clr8,
3228     [GOTCH]   = igb_mac_read_clr8,
3229     [PRC64]   = igb_mac_read_clr4,
3230     [PRC255]  = igb_mac_read_clr4,
3231     [PRC1023] = igb_mac_read_clr4,
3232     [PTC64]   = igb_mac_read_clr4,
3233     [PTC255]  = igb_mac_read_clr4,
3234     [PTC1023] = igb_mac_read_clr4,
3235     [GPRC]    = igb_mac_read_clr4,
3236     [TPT]     = igb_mac_read_clr4,
3237     [RUC]     = igb_mac_read_clr4,
3238     [BPRC]    = igb_mac_read_clr4,
3239     [MPTC]    = igb_mac_read_clr4,
3240     [IAC]     = igb_mac_read_clr4,
3241     [ICR]     = igb_mac_icr_read,
3242     [STATUS]  = igb_get_status,
3243     [ICS]     = igb_mac_ics_read,
3244     /*
3245      * 8.8.10: Reading the IMC register returns the value of the IMS register.
3246      */
3247     [IMC]     = igb_mac_ims_read,
3248     [TORH]    = igb_mac_read_clr8,
3249     [GORCH]   = igb_mac_read_clr8,
3250     [PRC127]  = igb_mac_read_clr4,
3251     [PRC511]  = igb_mac_read_clr4,
3252     [PRC1522] = igb_mac_read_clr4,
3253     [PTC127]  = igb_mac_read_clr4,
3254     [PTC511]  = igb_mac_read_clr4,
3255     [PTC1522] = igb_mac_read_clr4,
3256     [GPTC]    = igb_mac_read_clr4,
3257     [TPR]     = igb_mac_read_clr4,
3258     [ROC]     = igb_mac_read_clr4,
3259     [MPRC]    = igb_mac_read_clr4,
3260     [BPTC]    = igb_mac_read_clr4,
3261     [TSCTC]   = igb_mac_read_clr4,
3262     [CTRL]    = igb_get_ctrl,
3263     [SWSM]    = igb_mac_swsm_read,
3264     [IMS]     = igb_mac_ims_read,
3265     [SYSTIML] = igb_get_systiml,
3266     [RXSATRH] = igb_get_rxsatrh,
3267     [TXSTMPH] = igb_get_txstmph,
3268 
3269     [CRCERRS ... MPC]      = igb_mac_readreg,
3270     [IP6AT ... IP6AT + 3]  = igb_mac_readreg,
3271     [IP4AT ... IP4AT + 6]  = igb_mac_readreg,
3272     [RA ... RA + 31]       = igb_mac_readreg,
3273     [RA2 ... RA2 + 31]     = igb_mac_readreg,
3274     [WUPM ... WUPM + 31]   = igb_mac_readreg,
3275     [MTA ... MTA + E1000_MC_TBL_SIZE - 1]    = igb_mac_readreg,
3276     [VFTA ... VFTA + E1000_VLAN_FILTER_TBL_SIZE - 1]  = igb_mac_readreg,
3277     [FFMT ... FFMT + 254]  = igb_mac_readreg,
3278     [MDEF ... MDEF + 7]    = igb_mac_readreg,
3279     [FTFT ... FTFT + 254]  = igb_mac_readreg,
3280     [RETA ... RETA + 31]   = igb_mac_readreg,
3281     [RSSRK ... RSSRK + 9]  = igb_mac_readreg,
3282     [MAVTV0 ... MAVTV3]    = igb_mac_readreg,
3283     [EITR0 ... EITR0 + IGB_INTR_NUM - 1] = igb_mac_eitr_read,
3284     [PVTEICR0] = igb_mac_read_clr4,
3285     [PVTEICR1] = igb_mac_read_clr4,
3286     [PVTEICR2] = igb_mac_read_clr4,
3287     [PVTEICR3] = igb_mac_read_clr4,
3288     [PVTEICR4] = igb_mac_read_clr4,
3289     [PVTEICR5] = igb_mac_read_clr4,
3290     [PVTEICR6] = igb_mac_read_clr4,
3291     [PVTEICR7] = igb_mac_read_clr4,
3292 
3293     /* IGB specific: */
3294     [FWSM]       = igb_mac_readreg,
3295     [SW_FW_SYNC] = igb_mac_readreg,
3296     [HTCBDPC]    = igb_mac_read_clr4,
3297     [EICR]       = igb_mac_read_clr4,
3298     [EIMS]       = igb_mac_readreg,
3299     [EIAM]       = igb_mac_readreg,
3300     [IVAR0 ... IVAR0 + 7] = igb_mac_readreg,
3301     igb_getreg(IVAR_MISC),
3302     igb_getreg(VT_CTL),
3303     [P2VMAILBOX0 ... P2VMAILBOX7] = igb_mac_readreg,
3304     [V2PMAILBOX0 ... V2PMAILBOX7] = igb_mac_vfmailbox_read,
3305     igb_getreg(MBVFICR),
3306     [VMBMEM0 ... VMBMEM0 + 127] = igb_mac_readreg,
3307     igb_getreg(MBVFIMR),
3308     igb_getreg(VFLRE),
3309     igb_getreg(VFRE),
3310     igb_getreg(VFTE),
3311     igb_getreg(QDE),
3312     igb_getreg(DTXSWC),
3313     igb_getreg(RPLOLR),
3314     [VLVF0 ... VLVF0 + E1000_VLVF_ARRAY_SIZE - 1] = igb_mac_readreg,
3315     [VMVIR0 ... VMVIR7] = igb_mac_readreg,
3316     [VMOLR0 ... VMOLR7] = igb_mac_readreg,
3317     [WVBR] = igb_mac_read_clr4,
3318     [RQDPC0] = igb_mac_read_clr4,
3319     [RQDPC1] = igb_mac_read_clr4,
3320     [RQDPC2] = igb_mac_read_clr4,
3321     [RQDPC3] = igb_mac_read_clr4,
3322     [RQDPC4] = igb_mac_read_clr4,
3323     [RQDPC5] = igb_mac_read_clr4,
3324     [RQDPC6] = igb_mac_read_clr4,
3325     [RQDPC7] = igb_mac_read_clr4,
3326     [RQDPC8] = igb_mac_read_clr4,
3327     [RQDPC9] = igb_mac_read_clr4,
3328     [RQDPC10] = igb_mac_read_clr4,
3329     [RQDPC11] = igb_mac_read_clr4,
3330     [RQDPC12] = igb_mac_read_clr4,
3331     [RQDPC13] = igb_mac_read_clr4,
3332     [RQDPC14] = igb_mac_read_clr4,
3333     [RQDPC15] = igb_mac_read_clr4,
3334     [VTIVAR ... VTIVAR + 7] = igb_mac_readreg,
3335     [VTIVAR_MISC ... VTIVAR_MISC + 7] = igb_mac_readreg,
3336 };
3337 enum { IGB_NREADOPS = ARRAY_SIZE(igb_macreg_readops) };
3338 
3339 #define igb_putreg(x)    [x] = igb_mac_writereg
3340 typedef void (*writeops)(IGBCore *, int, uint32_t);
3341 static const writeops igb_macreg_writeops[] = {
3342     igb_putreg(SWSM),
3343     igb_putreg(WUFC),
3344     igb_putreg(RDBAH0),
3345     igb_putreg(RDBAH1),
3346     igb_putreg(RDBAH2),
3347     igb_putreg(RDBAH3),
3348     igb_putreg(RDBAH4),
3349     igb_putreg(RDBAH5),
3350     igb_putreg(RDBAH6),
3351     igb_putreg(RDBAH7),
3352     igb_putreg(RDBAH8),
3353     igb_putreg(RDBAH9),
3354     igb_putreg(RDBAH10),
3355     igb_putreg(RDBAH11),
3356     igb_putreg(RDBAH12),
3357     igb_putreg(RDBAH13),
3358     igb_putreg(RDBAH14),
3359     igb_putreg(RDBAH15),
3360     igb_putreg(SRRCTL0),
3361     igb_putreg(SRRCTL1),
3362     igb_putreg(SRRCTL2),
3363     igb_putreg(SRRCTL3),
3364     igb_putreg(SRRCTL4),
3365     igb_putreg(SRRCTL5),
3366     igb_putreg(SRRCTL6),
3367     igb_putreg(SRRCTL7),
3368     igb_putreg(SRRCTL8),
3369     igb_putreg(SRRCTL9),
3370     igb_putreg(SRRCTL10),
3371     igb_putreg(SRRCTL11),
3372     igb_putreg(SRRCTL12),
3373     igb_putreg(SRRCTL13),
3374     igb_putreg(SRRCTL14),
3375     igb_putreg(SRRCTL15),
3376     igb_putreg(RXDCTL0),
3377     igb_putreg(RXDCTL1),
3378     igb_putreg(RXDCTL2),
3379     igb_putreg(RXDCTL3),
3380     igb_putreg(RXDCTL4),
3381     igb_putreg(RXDCTL5),
3382     igb_putreg(RXDCTL6),
3383     igb_putreg(RXDCTL7),
3384     igb_putreg(RXDCTL8),
3385     igb_putreg(RXDCTL9),
3386     igb_putreg(RXDCTL10),
3387     igb_putreg(RXDCTL11),
3388     igb_putreg(RXDCTL12),
3389     igb_putreg(RXDCTL13),
3390     igb_putreg(RXDCTL14),
3391     igb_putreg(RXDCTL15),
3392     igb_putreg(LEDCTL),
3393     igb_putreg(TCTL),
3394     igb_putreg(TCTL_EXT),
3395     igb_putreg(DTXCTL),
3396     igb_putreg(RXPBS),
3397     igb_putreg(RQDPC0),
3398     igb_putreg(FCAL),
3399     igb_putreg(FCRUC),
3400     igb_putreg(WUC),
3401     igb_putreg(WUS),
3402     igb_putreg(IPAV),
3403     igb_putreg(TDBAH0),
3404     igb_putreg(TDBAH1),
3405     igb_putreg(TDBAH2),
3406     igb_putreg(TDBAH3),
3407     igb_putreg(TDBAH4),
3408     igb_putreg(TDBAH5),
3409     igb_putreg(TDBAH6),
3410     igb_putreg(TDBAH7),
3411     igb_putreg(TDBAH8),
3412     igb_putreg(TDBAH9),
3413     igb_putreg(TDBAH10),
3414     igb_putreg(TDBAH11),
3415     igb_putreg(TDBAH12),
3416     igb_putreg(TDBAH13),
3417     igb_putreg(TDBAH14),
3418     igb_putreg(TDBAH15),
3419     igb_putreg(IAM),
3420     igb_putreg(MANC),
3421     igb_putreg(MANC2H),
3422     igb_putreg(MFVAL),
3423     igb_putreg(FACTPS),
3424     igb_putreg(FUNCTAG),
3425     igb_putreg(GSCL_1),
3426     igb_putreg(GSCL_2),
3427     igb_putreg(GSCL_3),
3428     igb_putreg(GSCL_4),
3429     igb_putreg(GSCN_0),
3430     igb_putreg(GSCN_1),
3431     igb_putreg(GSCN_2),
3432     igb_putreg(GSCN_3),
3433     igb_putreg(MRQC),
3434     igb_putreg(FLOP),
3435     igb_putreg(FLA),
3436     igb_putreg(TXDCTL0),
3437     igb_putreg(TXDCTL1),
3438     igb_putreg(TXDCTL2),
3439     igb_putreg(TXDCTL3),
3440     igb_putreg(TXDCTL4),
3441     igb_putreg(TXDCTL5),
3442     igb_putreg(TXDCTL6),
3443     igb_putreg(TXDCTL7),
3444     igb_putreg(TXDCTL8),
3445     igb_putreg(TXDCTL9),
3446     igb_putreg(TXDCTL10),
3447     igb_putreg(TXDCTL11),
3448     igb_putreg(TXDCTL12),
3449     igb_putreg(TXDCTL13),
3450     igb_putreg(TXDCTL14),
3451     igb_putreg(TXDCTL15),
3452     igb_putreg(TXCTL0),
3453     igb_putreg(TXCTL1),
3454     igb_putreg(TXCTL2),
3455     igb_putreg(TXCTL3),
3456     igb_putreg(TXCTL4),
3457     igb_putreg(TXCTL5),
3458     igb_putreg(TXCTL6),
3459     igb_putreg(TXCTL7),
3460     igb_putreg(TXCTL8),
3461     igb_putreg(TXCTL9),
3462     igb_putreg(TXCTL10),
3463     igb_putreg(TXCTL11),
3464     igb_putreg(TXCTL12),
3465     igb_putreg(TXCTL13),
3466     igb_putreg(TXCTL14),
3467     igb_putreg(TXCTL15),
3468     igb_putreg(TDWBAL0),
3469     igb_putreg(TDWBAL1),
3470     igb_putreg(TDWBAL2),
3471     igb_putreg(TDWBAL3),
3472     igb_putreg(TDWBAL4),
3473     igb_putreg(TDWBAL5),
3474     igb_putreg(TDWBAL6),
3475     igb_putreg(TDWBAL7),
3476     igb_putreg(TDWBAL8),
3477     igb_putreg(TDWBAL9),
3478     igb_putreg(TDWBAL10),
3479     igb_putreg(TDWBAL11),
3480     igb_putreg(TDWBAL12),
3481     igb_putreg(TDWBAL13),
3482     igb_putreg(TDWBAL14),
3483     igb_putreg(TDWBAL15),
3484     igb_putreg(TDWBAH0),
3485     igb_putreg(TDWBAH1),
3486     igb_putreg(TDWBAH2),
3487     igb_putreg(TDWBAH3),
3488     igb_putreg(TDWBAH4),
3489     igb_putreg(TDWBAH5),
3490     igb_putreg(TDWBAH6),
3491     igb_putreg(TDWBAH7),
3492     igb_putreg(TDWBAH8),
3493     igb_putreg(TDWBAH9),
3494     igb_putreg(TDWBAH10),
3495     igb_putreg(TDWBAH11),
3496     igb_putreg(TDWBAH12),
3497     igb_putreg(TDWBAH13),
3498     igb_putreg(TDWBAH14),
3499     igb_putreg(TDWBAH15),
3500     igb_putreg(TIPG),
3501     igb_putreg(RXSTMPH),
3502     igb_putreg(RXSTMPL),
3503     igb_putreg(RXSATRL),
3504     igb_putreg(RXSATRH),
3505     igb_putreg(TXSTMPL),
3506     igb_putreg(TXSTMPH),
3507     igb_putreg(SYSTIML),
3508     igb_putreg(SYSTIMH),
3509     igb_putreg(TIMADJL),
3510     igb_putreg(TSYNCRXCTL),
3511     igb_putreg(TSYNCTXCTL),
3512     igb_putreg(EEMNGCTL),
3513     igb_putreg(GPIE),
3514     igb_putreg(TXPBS),
3515     igb_putreg(RLPML),
3516     igb_putreg(VET),
3517 
3518     [TDH0]     = igb_set_16bit,
3519     [TDH1]     = igb_set_16bit,
3520     [TDH2]     = igb_set_16bit,
3521     [TDH3]     = igb_set_16bit,
3522     [TDH4]     = igb_set_16bit,
3523     [TDH5]     = igb_set_16bit,
3524     [TDH6]     = igb_set_16bit,
3525     [TDH7]     = igb_set_16bit,
3526     [TDH8]     = igb_set_16bit,
3527     [TDH9]     = igb_set_16bit,
3528     [TDH10]    = igb_set_16bit,
3529     [TDH11]    = igb_set_16bit,
3530     [TDH12]    = igb_set_16bit,
3531     [TDH13]    = igb_set_16bit,
3532     [TDH14]    = igb_set_16bit,
3533     [TDH15]    = igb_set_16bit,
3534     [TDT0]     = igb_set_tdt,
3535     [TDT1]     = igb_set_tdt,
3536     [TDT2]     = igb_set_tdt,
3537     [TDT3]     = igb_set_tdt,
3538     [TDT4]     = igb_set_tdt,
3539     [TDT5]     = igb_set_tdt,
3540     [TDT6]     = igb_set_tdt,
3541     [TDT7]     = igb_set_tdt,
3542     [TDT8]     = igb_set_tdt,
3543     [TDT9]     = igb_set_tdt,
3544     [TDT10]    = igb_set_tdt,
3545     [TDT11]    = igb_set_tdt,
3546     [TDT12]    = igb_set_tdt,
3547     [TDT13]    = igb_set_tdt,
3548     [TDT14]    = igb_set_tdt,
3549     [TDT15]    = igb_set_tdt,
3550     [MDIC]     = igb_set_mdic,
3551     [ICS]      = igb_set_ics,
3552     [RDH0]     = igb_set_16bit,
3553     [RDH1]     = igb_set_16bit,
3554     [RDH2]     = igb_set_16bit,
3555     [RDH3]     = igb_set_16bit,
3556     [RDH4]     = igb_set_16bit,
3557     [RDH5]     = igb_set_16bit,
3558     [RDH6]     = igb_set_16bit,
3559     [RDH7]     = igb_set_16bit,
3560     [RDH8]     = igb_set_16bit,
3561     [RDH9]     = igb_set_16bit,
3562     [RDH10]    = igb_set_16bit,
3563     [RDH11]    = igb_set_16bit,
3564     [RDH12]    = igb_set_16bit,
3565     [RDH13]    = igb_set_16bit,
3566     [RDH14]    = igb_set_16bit,
3567     [RDH15]    = igb_set_16bit,
3568     [RDT0]     = igb_set_rdt,
3569     [RDT1]     = igb_set_rdt,
3570     [RDT2]     = igb_set_rdt,
3571     [RDT3]     = igb_set_rdt,
3572     [RDT4]     = igb_set_rdt,
3573     [RDT5]     = igb_set_rdt,
3574     [RDT6]     = igb_set_rdt,
3575     [RDT7]     = igb_set_rdt,
3576     [RDT8]     = igb_set_rdt,
3577     [RDT9]     = igb_set_rdt,
3578     [RDT10]    = igb_set_rdt,
3579     [RDT11]    = igb_set_rdt,
3580     [RDT12]    = igb_set_rdt,
3581     [RDT13]    = igb_set_rdt,
3582     [RDT14]    = igb_set_rdt,
3583     [RDT15]    = igb_set_rdt,
3584     [IMC]      = igb_set_imc,
3585     [IMS]      = igb_set_ims,
3586     [ICR]      = igb_set_icr,
3587     [EECD]     = igb_set_eecd,
3588     [RCTL]     = igb_set_rx_control,
3589     [CTRL]     = igb_set_ctrl,
3590     [EERD]     = igb_set_eerd,
3591     [TDFH]     = igb_set_13bit,
3592     [TDFT]     = igb_set_13bit,
3593     [TDFHS]    = igb_set_13bit,
3594     [TDFTS]    = igb_set_13bit,
3595     [TDFPC]    = igb_set_13bit,
3596     [RDFH]     = igb_set_13bit,
3597     [RDFT]     = igb_set_13bit,
3598     [RDFHS]    = igb_set_13bit,
3599     [RDFTS]    = igb_set_13bit,
3600     [RDFPC]    = igb_set_13bit,
3601     [GCR]      = igb_set_gcr,
3602     [RXCSUM]   = igb_set_rxcsum,
3603     [TDLEN0]   = igb_set_dlen,
3604     [TDLEN1]   = igb_set_dlen,
3605     [TDLEN2]   = igb_set_dlen,
3606     [TDLEN3]   = igb_set_dlen,
3607     [TDLEN4]   = igb_set_dlen,
3608     [TDLEN5]   = igb_set_dlen,
3609     [TDLEN6]   = igb_set_dlen,
3610     [TDLEN7]   = igb_set_dlen,
3611     [TDLEN8]   = igb_set_dlen,
3612     [TDLEN9]   = igb_set_dlen,
3613     [TDLEN10]  = igb_set_dlen,
3614     [TDLEN11]  = igb_set_dlen,
3615     [TDLEN12]  = igb_set_dlen,
3616     [TDLEN13]  = igb_set_dlen,
3617     [TDLEN14]  = igb_set_dlen,
3618     [TDLEN15]  = igb_set_dlen,
3619     [RDLEN0]   = igb_set_dlen,
3620     [RDLEN1]   = igb_set_dlen,
3621     [RDLEN2]   = igb_set_dlen,
3622     [RDLEN3]   = igb_set_dlen,
3623     [RDLEN4]   = igb_set_dlen,
3624     [RDLEN5]   = igb_set_dlen,
3625     [RDLEN6]   = igb_set_dlen,
3626     [RDLEN7]   = igb_set_dlen,
3627     [RDLEN8]   = igb_set_dlen,
3628     [RDLEN9]   = igb_set_dlen,
3629     [RDLEN10]  = igb_set_dlen,
3630     [RDLEN11]  = igb_set_dlen,
3631     [RDLEN12]  = igb_set_dlen,
3632     [RDLEN13]  = igb_set_dlen,
3633     [RDLEN14]  = igb_set_dlen,
3634     [RDLEN15]  = igb_set_dlen,
3635     [TDBAL0]   = igb_set_dbal,
3636     [TDBAL1]   = igb_set_dbal,
3637     [TDBAL2]   = igb_set_dbal,
3638     [TDBAL3]   = igb_set_dbal,
3639     [TDBAL4]   = igb_set_dbal,
3640     [TDBAL5]   = igb_set_dbal,
3641     [TDBAL6]   = igb_set_dbal,
3642     [TDBAL7]   = igb_set_dbal,
3643     [TDBAL8]   = igb_set_dbal,
3644     [TDBAL9]   = igb_set_dbal,
3645     [TDBAL10]  = igb_set_dbal,
3646     [TDBAL11]  = igb_set_dbal,
3647     [TDBAL12]  = igb_set_dbal,
3648     [TDBAL13]  = igb_set_dbal,
3649     [TDBAL14]  = igb_set_dbal,
3650     [TDBAL15]  = igb_set_dbal,
3651     [RDBAL0]   = igb_set_dbal,
3652     [RDBAL1]   = igb_set_dbal,
3653     [RDBAL2]   = igb_set_dbal,
3654     [RDBAL3]   = igb_set_dbal,
3655     [RDBAL4]   = igb_set_dbal,
3656     [RDBAL5]   = igb_set_dbal,
3657     [RDBAL6]   = igb_set_dbal,
3658     [RDBAL7]   = igb_set_dbal,
3659     [RDBAL8]   = igb_set_dbal,
3660     [RDBAL9]   = igb_set_dbal,
3661     [RDBAL10]  = igb_set_dbal,
3662     [RDBAL11]  = igb_set_dbal,
3663     [RDBAL12]  = igb_set_dbal,
3664     [RDBAL13]  = igb_set_dbal,
3665     [RDBAL14]  = igb_set_dbal,
3666     [RDBAL15]  = igb_set_dbal,
3667     [STATUS]   = igb_set_status,
3668     [PBACLR]   = igb_set_pbaclr,
3669     [CTRL_EXT] = igb_set_ctrlext,
3670     [FCAH]     = igb_set_16bit,
3671     [FCT]      = igb_set_16bit,
3672     [FCTTV]    = igb_set_16bit,
3673     [FCRTV]    = igb_set_16bit,
3674     [FCRTH]    = igb_set_fcrth,
3675     [FCRTL]    = igb_set_fcrtl,
3676     [CTRL_DUP] = igb_set_ctrl,
3677     [RFCTL]    = igb_set_rfctl,
3678     [TIMINCA]  = igb_set_timinca,
3679     [TIMADJH]  = igb_set_timadjh,
3680 
3681     [IP6AT ... IP6AT + 3]    = igb_mac_writereg,
3682     [IP4AT ... IP4AT + 6]    = igb_mac_writereg,
3683     [RA]                     = igb_mac_writereg,
3684     [RA + 1]                 = igb_mac_setmacaddr,
3685     [RA + 2 ... RA + 31]     = igb_mac_writereg,
3686     [RA2 ... RA2 + 31]       = igb_mac_writereg,
3687     [WUPM ... WUPM + 31]     = igb_mac_writereg,
3688     [MTA ... MTA + E1000_MC_TBL_SIZE - 1] = igb_mac_writereg,
3689     [VFTA ... VFTA + E1000_VLAN_FILTER_TBL_SIZE - 1] = igb_mac_writereg,
3690     [FFMT ... FFMT + 254]    = igb_set_4bit,
3691     [MDEF ... MDEF + 7]      = igb_mac_writereg,
3692     [FTFT ... FTFT + 254]    = igb_mac_writereg,
3693     [RETA ... RETA + 31]     = igb_mac_writereg,
3694     [RSSRK ... RSSRK + 9]    = igb_mac_writereg,
3695     [MAVTV0 ... MAVTV3]      = igb_mac_writereg,
3696     [EITR0 ... EITR0 + IGB_INTR_NUM - 1] = igb_set_eitr,
3697 
3698     /* IGB specific: */
3699     [FWSM]     = igb_mac_writereg,
3700     [SW_FW_SYNC] = igb_mac_writereg,
3701     [EICR] = igb_set_eicr,
3702     [EICS] = igb_set_eics,
3703     [EIAC] = igb_set_eiac,
3704     [EIAM] = igb_set_eiam,
3705     [EIMC] = igb_set_eimc,
3706     [EIMS] = igb_set_eims,
3707     [IVAR0 ... IVAR0 + 7] = igb_mac_writereg,
3708     igb_putreg(IVAR_MISC),
3709     igb_putreg(VT_CTL),
3710     [P2VMAILBOX0 ... P2VMAILBOX7] = igb_set_pfmailbox,
3711     [V2PMAILBOX0 ... V2PMAILBOX7] = igb_set_vfmailbox,
3712     [MBVFICR] = igb_w1c,
3713     [VMBMEM0 ... VMBMEM0 + 127] = igb_mac_writereg,
3714     igb_putreg(MBVFIMR),
3715     [VFLRE] = igb_w1c,
3716     igb_putreg(VFRE),
3717     igb_putreg(VFTE),
3718     igb_putreg(QDE),
3719     igb_putreg(DTXSWC),
3720     igb_putreg(RPLOLR),
3721     [VLVF0 ... VLVF0 + E1000_VLVF_ARRAY_SIZE - 1] = igb_mac_writereg,
3722     [VMVIR0 ... VMVIR7] = igb_mac_writereg,
3723     [VMOLR0 ... VMOLR7] = igb_mac_writereg,
3724     [UTA ... UTA + E1000_MC_TBL_SIZE - 1] = igb_mac_writereg,
3725     [PVTCTRL0] = igb_set_vtctrl,
3726     [PVTCTRL1] = igb_set_vtctrl,
3727     [PVTCTRL2] = igb_set_vtctrl,
3728     [PVTCTRL3] = igb_set_vtctrl,
3729     [PVTCTRL4] = igb_set_vtctrl,
3730     [PVTCTRL5] = igb_set_vtctrl,
3731     [PVTCTRL6] = igb_set_vtctrl,
3732     [PVTCTRL7] = igb_set_vtctrl,
3733     [PVTEICS0] = igb_set_vteics,
3734     [PVTEICS1] = igb_set_vteics,
3735     [PVTEICS2] = igb_set_vteics,
3736     [PVTEICS3] = igb_set_vteics,
3737     [PVTEICS4] = igb_set_vteics,
3738     [PVTEICS5] = igb_set_vteics,
3739     [PVTEICS6] = igb_set_vteics,
3740     [PVTEICS7] = igb_set_vteics,
3741     [PVTEIMS0] = igb_set_vteims,
3742     [PVTEIMS1] = igb_set_vteims,
3743     [PVTEIMS2] = igb_set_vteims,
3744     [PVTEIMS3] = igb_set_vteims,
3745     [PVTEIMS4] = igb_set_vteims,
3746     [PVTEIMS5] = igb_set_vteims,
3747     [PVTEIMS6] = igb_set_vteims,
3748     [PVTEIMS7] = igb_set_vteims,
3749     [PVTEIMC0] = igb_set_vteimc,
3750     [PVTEIMC1] = igb_set_vteimc,
3751     [PVTEIMC2] = igb_set_vteimc,
3752     [PVTEIMC3] = igb_set_vteimc,
3753     [PVTEIMC4] = igb_set_vteimc,
3754     [PVTEIMC5] = igb_set_vteimc,
3755     [PVTEIMC6] = igb_set_vteimc,
3756     [PVTEIMC7] = igb_set_vteimc,
3757     [PVTEIAC0] = igb_set_vteiac,
3758     [PVTEIAC1] = igb_set_vteiac,
3759     [PVTEIAC2] = igb_set_vteiac,
3760     [PVTEIAC3] = igb_set_vteiac,
3761     [PVTEIAC4] = igb_set_vteiac,
3762     [PVTEIAC5] = igb_set_vteiac,
3763     [PVTEIAC6] = igb_set_vteiac,
3764     [PVTEIAC7] = igb_set_vteiac,
3765     [PVTEIAM0] = igb_set_vteiam,
3766     [PVTEIAM1] = igb_set_vteiam,
3767     [PVTEIAM2] = igb_set_vteiam,
3768     [PVTEIAM3] = igb_set_vteiam,
3769     [PVTEIAM4] = igb_set_vteiam,
3770     [PVTEIAM5] = igb_set_vteiam,
3771     [PVTEIAM6] = igb_set_vteiam,
3772     [PVTEIAM7] = igb_set_vteiam,
3773     [PVTEICR0] = igb_set_vteicr,
3774     [PVTEICR1] = igb_set_vteicr,
3775     [PVTEICR2] = igb_set_vteicr,
3776     [PVTEICR3] = igb_set_vteicr,
3777     [PVTEICR4] = igb_set_vteicr,
3778     [PVTEICR5] = igb_set_vteicr,
3779     [PVTEICR6] = igb_set_vteicr,
3780     [PVTEICR7] = igb_set_vteicr,
3781     [VTIVAR ... VTIVAR + 7] = igb_set_vtivar,
3782     [VTIVAR_MISC ... VTIVAR_MISC + 7] = igb_mac_writereg
3783 };
3784 enum { IGB_NWRITEOPS = ARRAY_SIZE(igb_macreg_writeops) };
3785 
3786 enum { MAC_ACCESS_PARTIAL = 1 };
3787 
3788 /*
3789  * The array below combines alias offsets of the index values for the
3790  * MAC registers that have aliases, with the indication of not fully
3791  * implemented registers (lowest bit). This combination is possible
3792  * because all of the offsets are even.
3793  */
3794 static const uint16_t mac_reg_access[E1000E_MAC_SIZE] = {
3795     /* Alias index offsets */
3796     [FCRTL_A] = 0x07fe,
3797     [RDFH_A]  = 0xe904, [RDFT_A]  = 0xe904,
3798     [TDFH_A]  = 0xed00, [TDFT_A]  = 0xed00,
3799     [RA_A ... RA_A + 31]      = 0x14f0,
3800     [VFTA_A ... VFTA_A + E1000_VLAN_FILTER_TBL_SIZE - 1] = 0x1400,
3801 
3802     [RDBAL0_A] = 0x2600,
3803     [RDBAH0_A] = 0x2600,
3804     [RDLEN0_A] = 0x2600,
3805     [SRRCTL0_A] = 0x2600,
3806     [RDH0_A] = 0x2600,
3807     [RDT0_A] = 0x2600,
3808     [RXDCTL0_A] = 0x2600,
3809     [RXCTL0_A] = 0x2600,
3810     [RQDPC0_A] = 0x2600,
3811     [RDBAL1_A] = 0x25D0,
3812     [RDBAL2_A] = 0x25A0,
3813     [RDBAL3_A] = 0x2570,
3814     [RDBAH1_A] = 0x25D0,
3815     [RDBAH2_A] = 0x25A0,
3816     [RDBAH3_A] = 0x2570,
3817     [RDLEN1_A] = 0x25D0,
3818     [RDLEN2_A] = 0x25A0,
3819     [RDLEN3_A] = 0x2570,
3820     [SRRCTL1_A] = 0x25D0,
3821     [SRRCTL2_A] = 0x25A0,
3822     [SRRCTL3_A] = 0x2570,
3823     [RDH1_A] = 0x25D0,
3824     [RDH2_A] = 0x25A0,
3825     [RDH3_A] = 0x2570,
3826     [RDT1_A] = 0x25D0,
3827     [RDT2_A] = 0x25A0,
3828     [RDT3_A] = 0x2570,
3829     [RXDCTL1_A] = 0x25D0,
3830     [RXDCTL2_A] = 0x25A0,
3831     [RXDCTL3_A] = 0x2570,
3832     [RXCTL1_A] = 0x25D0,
3833     [RXCTL2_A] = 0x25A0,
3834     [RXCTL3_A] = 0x2570,
3835     [RQDPC1_A] = 0x25D0,
3836     [RQDPC2_A] = 0x25A0,
3837     [RQDPC3_A] = 0x2570,
3838     [TDBAL0_A] = 0x2A00,
3839     [TDBAH0_A] = 0x2A00,
3840     [TDLEN0_A] = 0x2A00,
3841     [TDH0_A] = 0x2A00,
3842     [TDT0_A] = 0x2A00,
3843     [TXCTL0_A] = 0x2A00,
3844     [TDWBAL0_A] = 0x2A00,
3845     [TDWBAH0_A] = 0x2A00,
3846     [TDBAL1_A] = 0x29D0,
3847     [TDBAL2_A] = 0x29A0,
3848     [TDBAL3_A] = 0x2970,
3849     [TDBAH1_A] = 0x29D0,
3850     [TDBAH2_A] = 0x29A0,
3851     [TDBAH3_A] = 0x2970,
3852     [TDLEN1_A] = 0x29D0,
3853     [TDLEN2_A] = 0x29A0,
3854     [TDLEN3_A] = 0x2970,
3855     [TDH1_A] = 0x29D0,
3856     [TDH2_A] = 0x29A0,
3857     [TDH3_A] = 0x2970,
3858     [TDT1_A] = 0x29D0,
3859     [TDT2_A] = 0x29A0,
3860     [TDT3_A] = 0x2970,
3861     [TXDCTL0_A] = 0x2A00,
3862     [TXDCTL1_A] = 0x29D0,
3863     [TXDCTL2_A] = 0x29A0,
3864     [TXDCTL3_A] = 0x2970,
3865     [TXCTL1_A] = 0x29D0,
3866     [TXCTL2_A] = 0x29A0,
3867     [TXCTL3_A] = 0x29D0,
3868     [TDWBAL1_A] = 0x29D0,
3869     [TDWBAL2_A] = 0x29A0,
3870     [TDWBAL3_A] = 0x2970,
3871     [TDWBAH1_A] = 0x29D0,
3872     [TDWBAH2_A] = 0x29A0,
3873     [TDWBAH3_A] = 0x2970,
3874 
3875     /* Access options */
3876     [RDFH]  = MAC_ACCESS_PARTIAL,    [RDFT]  = MAC_ACCESS_PARTIAL,
3877     [RDFHS] = MAC_ACCESS_PARTIAL,    [RDFTS] = MAC_ACCESS_PARTIAL,
3878     [RDFPC] = MAC_ACCESS_PARTIAL,
3879     [TDFH]  = MAC_ACCESS_PARTIAL,    [TDFT]  = MAC_ACCESS_PARTIAL,
3880     [TDFHS] = MAC_ACCESS_PARTIAL,    [TDFTS] = MAC_ACCESS_PARTIAL,
3881     [TDFPC] = MAC_ACCESS_PARTIAL,    [EECD]  = MAC_ACCESS_PARTIAL,
3882     [FLA]   = MAC_ACCESS_PARTIAL,
3883     [FCAL]  = MAC_ACCESS_PARTIAL,    [FCAH]  = MAC_ACCESS_PARTIAL,
3884     [FCT]   = MAC_ACCESS_PARTIAL,    [FCTTV] = MAC_ACCESS_PARTIAL,
3885     [FCRTV] = MAC_ACCESS_PARTIAL,    [FCRTL] = MAC_ACCESS_PARTIAL,
3886     [FCRTH] = MAC_ACCESS_PARTIAL,
3887     [MAVTV0 ... MAVTV3] = MAC_ACCESS_PARTIAL
3888 };
3889 
3890 void
3891 igb_core_write(IGBCore *core, hwaddr addr, uint64_t val, unsigned size)
3892 {
3893     uint16_t index = igb_get_reg_index_with_offset(mac_reg_access, addr);
3894 
3895     if (index < IGB_NWRITEOPS && igb_macreg_writeops[index]) {
3896         if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
3897             trace_e1000e_wrn_regs_write_trivial(index << 2);
3898         }
3899         trace_e1000e_core_write(index << 2, size, val);
3900         igb_macreg_writeops[index](core, index, val);
3901     } else if (index < IGB_NREADOPS && igb_macreg_readops[index]) {
3902         trace_e1000e_wrn_regs_write_ro(index << 2, size, val);
3903     } else {
3904         trace_e1000e_wrn_regs_write_unknown(index << 2, size, val);
3905     }
3906 }
3907 
3908 uint64_t
3909 igb_core_read(IGBCore *core, hwaddr addr, unsigned size)
3910 {
3911     uint64_t val;
3912     uint16_t index = igb_get_reg_index_with_offset(mac_reg_access, addr);
3913 
3914     if (index < IGB_NREADOPS && igb_macreg_readops[index]) {
3915         if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
3916             trace_e1000e_wrn_regs_read_trivial(index << 2);
3917         }
3918         val = igb_macreg_readops[index](core, index);
3919         trace_e1000e_core_read(index << 2, size, val);
3920         return val;
3921     } else {
3922         trace_e1000e_wrn_regs_read_unknown(index << 2, size);
3923     }
3924     return 0;
3925 }
3926 
3927 static inline void
3928 igb_autoneg_pause(IGBCore *core)
3929 {
3930     timer_del(core->autoneg_timer);
3931 }
3932 
3933 static void
3934 igb_autoneg_resume(IGBCore *core)
3935 {
3936     if (igb_have_autoneg(core) &&
3937         !(core->phy[MII_BMSR] & MII_BMSR_AN_COMP)) {
3938         qemu_get_queue(core->owner_nic)->link_down = false;
3939         timer_mod(core->autoneg_timer,
3940                   qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
3941     }
3942 }
3943 
3944 static void
3945 igb_vm_state_change(void *opaque, bool running, RunState state)
3946 {
3947     IGBCore *core = opaque;
3948 
3949     if (running) {
3950         trace_e1000e_vm_state_running();
3951         igb_intrmgr_resume(core);
3952         igb_autoneg_resume(core);
3953     } else {
3954         trace_e1000e_vm_state_stopped();
3955         igb_autoneg_pause(core);
3956         igb_intrmgr_pause(core);
3957     }
3958 }
3959 
3960 void
3961 igb_core_pci_realize(IGBCore        *core,
3962                      const uint16_t *eeprom_templ,
3963                      uint32_t        eeprom_size,
3964                      const uint8_t  *macaddr)
3965 {
3966     int i;
3967 
3968     core->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
3969                                        igb_autoneg_timer, core);
3970     igb_intrmgr_pci_realize(core);
3971 
3972     core->vmstate = qemu_add_vm_change_state_handler(igb_vm_state_change, core);
3973 
3974     for (i = 0; i < IGB_NUM_QUEUES; i++) {
3975         net_tx_pkt_init(&core->tx[i].tx_pkt, E1000E_MAX_TX_FRAGS);
3976     }
3977 
3978     net_rx_pkt_init(&core->rx_pkt);
3979 
3980     e1000x_core_prepare_eeprom(core->eeprom,
3981                                eeprom_templ,
3982                                eeprom_size,
3983                                PCI_DEVICE_GET_CLASS(core->owner)->device_id,
3984                                macaddr);
3985     igb_update_rx_offloads(core);
3986 }
3987 
3988 void
3989 igb_core_pci_uninit(IGBCore *core)
3990 {
3991     int i;
3992 
3993     timer_free(core->autoneg_timer);
3994 
3995     igb_intrmgr_pci_unint(core);
3996 
3997     qemu_del_vm_change_state_handler(core->vmstate);
3998 
3999     for (i = 0; i < IGB_NUM_QUEUES; i++) {
4000         net_tx_pkt_uninit(core->tx[i].tx_pkt);
4001     }
4002 
4003     net_rx_pkt_uninit(core->rx_pkt);
4004 }
4005 
4006 static const uint16_t
4007 igb_phy_reg_init[] = {
4008     [MII_BMCR] = MII_BMCR_SPEED1000 |
4009                  MII_BMCR_FD        |
4010                  MII_BMCR_AUTOEN,
4011 
4012     [MII_BMSR] = MII_BMSR_EXTCAP    |
4013                  MII_BMSR_LINK_ST   |
4014                  MII_BMSR_AUTONEG   |
4015                  MII_BMSR_MFPS      |
4016                  MII_BMSR_EXTSTAT   |
4017                  MII_BMSR_10T_HD    |
4018                  MII_BMSR_10T_FD    |
4019                  MII_BMSR_100TX_HD  |
4020                  MII_BMSR_100TX_FD,
4021 
4022     [MII_PHYID1]            = IGP03E1000_E_PHY_ID >> 16,
4023     [MII_PHYID2]            = (IGP03E1000_E_PHY_ID & 0xfff0) | 1,
4024     [MII_ANAR]              = MII_ANAR_CSMACD | MII_ANAR_10 |
4025                               MII_ANAR_10FD | MII_ANAR_TX |
4026                               MII_ANAR_TXFD | MII_ANAR_PAUSE |
4027                               MII_ANAR_PAUSE_ASYM,
4028     [MII_ANLPAR]            = MII_ANLPAR_10 | MII_ANLPAR_10FD |
4029                               MII_ANLPAR_TX | MII_ANLPAR_TXFD |
4030                               MII_ANLPAR_T4 | MII_ANLPAR_PAUSE,
4031     [MII_ANER]              = MII_ANER_NP | MII_ANER_NWAY,
4032     [MII_ANNP]              = 0x1 | MII_ANNP_MP,
4033     [MII_CTRL1000]          = MII_CTRL1000_HALF | MII_CTRL1000_FULL |
4034                               MII_CTRL1000_PORT | MII_CTRL1000_MASTER,
4035     [MII_STAT1000]          = MII_STAT1000_HALF | MII_STAT1000_FULL |
4036                               MII_STAT1000_ROK | MII_STAT1000_LOK,
4037     [MII_EXTSTAT]           = MII_EXTSTAT_1000T_HD | MII_EXTSTAT_1000T_FD,
4038 
4039     [IGP01E1000_PHY_PORT_CONFIG] = BIT(5) | BIT(8),
4040     [IGP01E1000_PHY_PORT_STATUS] = IGP01E1000_PSSR_SPEED_1000MBPS,
4041     [IGP02E1000_PHY_POWER_MGMT]  = BIT(0) | BIT(3) | IGP02E1000_PM_D3_LPLU |
4042                                    IGP01E1000_PSCFR_SMART_SPEED
4043 };
4044 
4045 static const uint32_t igb_mac_reg_init[] = {
4046     [LEDCTL]        = 2 | (3 << 8) | BIT(15) | (6 << 16) | (7 << 24),
4047     [EEMNGCTL]      = BIT(31),
4048     [TXDCTL0]       = E1000_TXDCTL_QUEUE_ENABLE,
4049     [RXDCTL0]       = E1000_RXDCTL_QUEUE_ENABLE | (1 << 16),
4050     [RXDCTL1]       = 1 << 16,
4051     [RXDCTL2]       = 1 << 16,
4052     [RXDCTL3]       = 1 << 16,
4053     [RXDCTL4]       = 1 << 16,
4054     [RXDCTL5]       = 1 << 16,
4055     [RXDCTL6]       = 1 << 16,
4056     [RXDCTL7]       = 1 << 16,
4057     [RXDCTL8]       = 1 << 16,
4058     [RXDCTL9]       = 1 << 16,
4059     [RXDCTL10]      = 1 << 16,
4060     [RXDCTL11]      = 1 << 16,
4061     [RXDCTL12]      = 1 << 16,
4062     [RXDCTL13]      = 1 << 16,
4063     [RXDCTL14]      = 1 << 16,
4064     [RXDCTL15]      = 1 << 16,
4065     [TIPG]          = 0x08 | (0x04 << 10) | (0x06 << 20),
4066     [CTRL]          = E1000_CTRL_FD | E1000_CTRL_LRST | E1000_CTRL_SPD_1000 |
4067                       E1000_CTRL_ADVD3WUC,
4068     [STATUS]        = E1000_STATUS_PHYRA | BIT(31),
4069     [EECD]          = E1000_EECD_FWE_DIS | E1000_EECD_PRES |
4070                       (2 << E1000_EECD_SIZE_EX_SHIFT),
4071     [GCR]           = E1000_L0S_ADJUST |
4072                       E1000_GCR_CMPL_TMOUT_RESEND |
4073                       E1000_GCR_CAP_VER2 |
4074                       E1000_L1_ENTRY_LATENCY_MSB |
4075                       E1000_L1_ENTRY_LATENCY_LSB,
4076     [RXCSUM]        = E1000_RXCSUM_IPOFLD | E1000_RXCSUM_TUOFLD,
4077     [TXPBS]         = 0x28,
4078     [RXPBS]         = 0x40,
4079     [TCTL]          = E1000_TCTL_PSP | (0xF << E1000_CT_SHIFT) |
4080                       (0x40 << E1000_COLD_SHIFT) | (0x1 << 26) | (0xA << 28),
4081     [TCTL_EXT]      = 0x40 | (0x42 << 10),
4082     [DTXCTL]        = E1000_DTXCTL_8023LL | E1000_DTXCTL_SPOOF_INT,
4083     [VET]           = ETH_P_VLAN | (ETH_P_VLAN << 16),
4084 
4085     [V2PMAILBOX0 ... V2PMAILBOX0 + IGB_MAX_VF_FUNCTIONS - 1] = E1000_V2PMAILBOX_RSTI,
4086     [MBVFIMR]       = 0xFF,
4087     [VFRE]          = 0xFF,
4088     [VFTE]          = 0xFF,
4089     [VMOLR0 ... VMOLR0 + 7] = 0x2600 | E1000_VMOLR_STRCRC,
4090     [RPLOLR]        = E1000_RPLOLR_STRCRC,
4091     [RLPML]         = 0x2600,
4092     [TXCTL0]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4093                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4094                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4095     [TXCTL1]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4096                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4097                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4098     [TXCTL2]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4099                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4100                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4101     [TXCTL3]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4102                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4103                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4104     [TXCTL4]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4105                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4106                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4107     [TXCTL5]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4108                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4109                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4110     [TXCTL6]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4111                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4112                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4113     [TXCTL7]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4114                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4115                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4116     [TXCTL8]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4117                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4118                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4119     [TXCTL9]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4120                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4121                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4122     [TXCTL10]       = E1000_DCA_TXCTRL_DATA_RRO_EN |
4123                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4124                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4125     [TXCTL11]       = E1000_DCA_TXCTRL_DATA_RRO_EN |
4126                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4127                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4128     [TXCTL12]       = E1000_DCA_TXCTRL_DATA_RRO_EN |
4129                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4130                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4131     [TXCTL13]       = E1000_DCA_TXCTRL_DATA_RRO_EN |
4132                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4133                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4134     [TXCTL14]       = E1000_DCA_TXCTRL_DATA_RRO_EN |
4135                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4136                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4137     [TXCTL15]       = E1000_DCA_TXCTRL_DATA_RRO_EN |
4138                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4139                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4140 };
4141 
4142 static void igb_reset(IGBCore *core, bool sw)
4143 {
4144     struct igb_tx *tx;
4145     int i;
4146 
4147     timer_del(core->autoneg_timer);
4148 
4149     igb_intrmgr_reset(core);
4150 
4151     memset(core->phy, 0, sizeof core->phy);
4152     memcpy(core->phy, igb_phy_reg_init, sizeof igb_phy_reg_init);
4153 
4154     for (i = 0; i < E1000E_MAC_SIZE; i++) {
4155         if (sw &&
4156             (i == RXPBS || i == TXPBS ||
4157              (i >= EITR0 && i < EITR0 + IGB_INTR_NUM))) {
4158             continue;
4159         }
4160 
4161         core->mac[i] = i < ARRAY_SIZE(igb_mac_reg_init) ?
4162                        igb_mac_reg_init[i] : 0;
4163     }
4164 
4165     if (qemu_get_queue(core->owner_nic)->link_down) {
4166         igb_link_down(core);
4167     }
4168 
4169     e1000x_reset_mac_addr(core->owner_nic, core->mac, core->permanent_mac);
4170 
4171     for (int vfn = 0; vfn < IGB_MAX_VF_FUNCTIONS; vfn++) {
4172         /* Set RSTI, so VF can identify a PF reset is in progress */
4173         core->mac[V2PMAILBOX0 + vfn] |= E1000_V2PMAILBOX_RSTI;
4174     }
4175 
4176     for (i = 0; i < ARRAY_SIZE(core->tx); i++) {
4177         tx = &core->tx[i];
4178         memset(tx->ctx, 0, sizeof(tx->ctx));
4179         tx->first = true;
4180         tx->skip_cp = false;
4181     }
4182 }
4183 
4184 void
4185 igb_core_reset(IGBCore *core)
4186 {
4187     igb_reset(core, false);
4188 }
4189 
4190 void igb_core_pre_save(IGBCore *core)
4191 {
4192     int i;
4193     NetClientState *nc = qemu_get_queue(core->owner_nic);
4194 
4195     /*
4196      * If link is down and auto-negotiation is supported and ongoing,
4197      * complete auto-negotiation immediately. This allows us to look
4198      * at MII_BMSR_AN_COMP to infer link status on load.
4199      */
4200     if (nc->link_down && igb_have_autoneg(core)) {
4201         core->phy[MII_BMSR] |= MII_BMSR_AN_COMP;
4202         igb_update_flowctl_status(core);
4203     }
4204 
4205     for (i = 0; i < ARRAY_SIZE(core->tx); i++) {
4206         if (net_tx_pkt_has_fragments(core->tx[i].tx_pkt)) {
4207             core->tx[i].skip_cp = true;
4208         }
4209     }
4210 }
4211 
4212 int
4213 igb_core_post_load(IGBCore *core)
4214 {
4215     NetClientState *nc = qemu_get_queue(core->owner_nic);
4216 
4217     /*
4218      * nc.link_down can't be migrated, so infer link_down according
4219      * to link status bit in core.mac[STATUS].
4220      */
4221     nc->link_down = (core->mac[STATUS] & E1000_STATUS_LU) == 0;
4222 
4223     return 0;
4224 }
4225