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