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