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