xref: /openbmc/qemu/hw/net/igb_core.c (revision ec82ad7c4d61061d78907436ceb181859ab4a8d5)
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 E1000ERingInfo {
698     int dbah;
699     int dbal;
700     int dlen;
701     int dh;
702     int dt;
703     int idx;
704 } E1000ERingInfo;
705 
706 static inline bool
707 igb_ring_empty(IGBCore *core, const E1000ERingInfo *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 E1000ERingInfo *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 E1000ERingInfo *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 E1000ERingInfo *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 E1000ERingInfo *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 E1000ERingInfo *r)
759 {
760     return core->mac[r->dlen] > 0;
761 }
762 
763 typedef struct IGB_TxRing_st {
764     const E1000ERingInfo *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 E1000ERingInfo 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 E1000ERingInfo *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 E1000ERingInfo 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 E1000ERingInfo *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 E1000ERingInfo *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 E1000ERingInfo *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 E1000ERingInfo *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 E1000ERingInfo *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_common(IGBCore *core,
1285                              struct NetRxPkt *pkt,
1286                              bool is_eop,
1287                              uint32_t *status_flags,
1288                              uint16_t *vlan_tag)
1289 {
1290     struct virtio_net_hdr *vhdr;
1291     bool hasip4, hasip6, csum_valid;
1292     EthL4HdrProto l4hdr_proto;
1293 
1294     *status_flags = E1000_RXD_STAT_DD;
1295 
1296     /* No additional metadata needed for non-EOP descriptors */
1297     if (!is_eop) {
1298         goto func_exit;
1299     }
1300 
1301     *status_flags |= E1000_RXD_STAT_EOP;
1302 
1303     net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto);
1304     trace_e1000e_rx_metadata_protocols(hasip4, hasip6, l4hdr_proto);
1305 
1306     /* VLAN state */
1307     if (net_rx_pkt_is_vlan_stripped(pkt)) {
1308         *status_flags |= E1000_RXD_STAT_VP;
1309         *vlan_tag = cpu_to_le16(net_rx_pkt_get_vlan_tag(pkt));
1310         trace_e1000e_rx_metadata_vlan(*vlan_tag);
1311     }
1312 
1313     /* RX CSO information */
1314     if (hasip6 && (core->mac[RFCTL] & E1000_RFCTL_IPV6_XSUM_DIS)) {
1315         trace_e1000e_rx_metadata_ipv6_sum_disabled();
1316         goto func_exit;
1317     }
1318 
1319     vhdr = net_rx_pkt_get_vhdr(pkt);
1320 
1321     if (!(vhdr->flags & VIRTIO_NET_HDR_F_DATA_VALID) &&
1322         !(vhdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)) {
1323         trace_e1000e_rx_metadata_virthdr_no_csum_info();
1324         igb_verify_csum_in_sw(core, pkt, status_flags, l4hdr_proto);
1325         goto func_exit;
1326     }
1327 
1328     if (igb_rx_l3_cso_enabled(core)) {
1329         *status_flags |= hasip4 ? E1000_RXD_STAT_IPCS : 0;
1330     } else {
1331         trace_e1000e_rx_metadata_l3_cso_disabled();
1332     }
1333 
1334     if (igb_rx_l4_cso_enabled(core)) {
1335         switch (l4hdr_proto) {
1336         case ETH_L4_HDR_PROTO_SCTP:
1337             if (!net_rx_pkt_validate_l4_csum(pkt, &csum_valid)) {
1338                 trace_e1000e_rx_metadata_l4_csum_validation_failed();
1339                 goto func_exit;
1340             }
1341             if (!csum_valid) {
1342                 *status_flags |= E1000_RXDEXT_STATERR_TCPE;
1343             }
1344             /* fall through */
1345         case ETH_L4_HDR_PROTO_TCP:
1346             *status_flags |= E1000_RXD_STAT_TCPCS;
1347             break;
1348 
1349         case ETH_L4_HDR_PROTO_UDP:
1350             *status_flags |= E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS;
1351             break;
1352 
1353         default:
1354             break;
1355         }
1356     } else {
1357         trace_e1000e_rx_metadata_l4_cso_disabled();
1358     }
1359 
1360 func_exit:
1361     trace_e1000e_rx_metadata_status_flags(*status_flags);
1362     *status_flags = cpu_to_le32(*status_flags);
1363 }
1364 
1365 static inline void
1366 igb_write_lgcy_rx_descr(IGBCore *core, struct e1000_rx_desc *desc,
1367                         struct NetRxPkt *pkt,
1368                         const E1000E_RSSInfo *rss_info,
1369                         uint16_t length)
1370 {
1371     uint32_t status_flags;
1372 
1373     assert(!rss_info->enabled);
1374 
1375     memset(desc, 0, sizeof(*desc));
1376     desc->length = cpu_to_le16(length);
1377     igb_build_rx_metadata_common(core, pkt, pkt != NULL,
1378                                  &status_flags,
1379                                  &desc->special);
1380 
1381     desc->errors = (uint8_t) (le32_to_cpu(status_flags) >> 24);
1382     desc->status = (uint8_t) le32_to_cpu(status_flags);
1383 }
1384 
1385 static uint16_t
1386 igb_rx_desc_get_packet_type(IGBCore *core, struct NetRxPkt *pkt, uint16_t etqf)
1387 {
1388     uint16_t pkt_type;
1389     bool hasip4, hasip6;
1390     EthL4HdrProto l4hdr_proto;
1391 
1392     if (etqf < 8) {
1393         pkt_type = BIT(11) | etqf;
1394         return pkt_type;
1395     }
1396 
1397     net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto);
1398 
1399     if (hasip6 && !(core->mac[RFCTL] & E1000_RFCTL_IPV6_DIS)) {
1400         pkt_type = E1000_ADVRXD_PKT_IP6;
1401     } else if (hasip4) {
1402         pkt_type = E1000_ADVRXD_PKT_IP4;
1403     } else {
1404         pkt_type = 0;
1405     }
1406 
1407     switch (l4hdr_proto) {
1408     case ETH_L4_HDR_PROTO_TCP:
1409         pkt_type |= E1000_ADVRXD_PKT_TCP;
1410         break;
1411     case ETH_L4_HDR_PROTO_UDP:
1412         pkt_type |= E1000_ADVRXD_PKT_UDP;
1413         break;
1414     case ETH_L4_HDR_PROTO_SCTP:
1415         pkt_type |= E1000_ADVRXD_PKT_SCTP;
1416         break;
1417     default:
1418         break;
1419     }
1420 
1421     return pkt_type;
1422 }
1423 
1424 static inline void
1425 igb_write_adv_rx_descr(IGBCore *core, union e1000_adv_rx_desc *desc,
1426                        struct NetRxPkt *pkt,
1427                        const E1000E_RSSInfo *rss_info, uint16_t etqf, bool ts,
1428                        uint16_t length)
1429 {
1430     bool hasip4, hasip6;
1431     EthL4HdrProto l4hdr_proto;
1432     uint16_t rss_type = 0, pkt_type;
1433     bool eop = (pkt != NULL);
1434     uint32_t adv_desc_status_error = 0;
1435     memset(&desc->wb, 0, sizeof(desc->wb));
1436 
1437     desc->wb.upper.length = cpu_to_le16(length);
1438     igb_build_rx_metadata_common(core, pkt, eop,
1439                                  &desc->wb.upper.status_error,
1440                                  &desc->wb.upper.vlan);
1441 
1442     if (!eop) {
1443         return;
1444     }
1445 
1446     net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto);
1447 
1448     if ((core->mac[RXCSUM] & E1000_RXCSUM_PCSD) != 0) {
1449         if (rss_info->enabled) {
1450             desc->wb.lower.hi_dword.rss = cpu_to_le32(rss_info->hash);
1451             rss_type = rss_info->type;
1452             trace_igb_rx_metadata_rss(desc->wb.lower.hi_dword.rss, rss_type);
1453         }
1454     } else if (hasip4) {
1455             adv_desc_status_error |= E1000_RXD_STAT_IPIDV;
1456             desc->wb.lower.hi_dword.csum_ip.ip_id =
1457                 cpu_to_le16(net_rx_pkt_get_ip_id(pkt));
1458             trace_e1000e_rx_metadata_ip_id(
1459                 desc->wb.lower.hi_dword.csum_ip.ip_id);
1460     }
1461 
1462     if (ts) {
1463         adv_desc_status_error |= BIT(16);
1464     }
1465 
1466     pkt_type = igb_rx_desc_get_packet_type(core, pkt, etqf);
1467     trace_e1000e_rx_metadata_pkt_type(pkt_type);
1468     desc->wb.lower.lo_dword.pkt_info = cpu_to_le16(rss_type | (pkt_type << 4));
1469     desc->wb.upper.status_error |= cpu_to_le32(adv_desc_status_error);
1470 }
1471 
1472 static inline void
1473 igb_write_rx_descr(IGBCore *core, union e1000_rx_desc_union *desc,
1474                    struct NetRxPkt *pkt, const E1000E_RSSInfo *rss_info,
1475                    uint16_t etqf, bool ts, uint16_t length)
1476 {
1477     if (igb_rx_use_legacy_descriptor(core)) {
1478         igb_write_lgcy_rx_descr(core, &desc->legacy, pkt, rss_info, length);
1479     } else {
1480         igb_write_adv_rx_descr(core, &desc->adv, pkt, rss_info,
1481                                etqf, ts, length);
1482     }
1483 }
1484 
1485 static inline void
1486 igb_pci_dma_write_rx_desc(IGBCore *core, PCIDevice *dev, dma_addr_t addr,
1487                           union e1000_rx_desc_union *desc, dma_addr_t len)
1488 {
1489     if (igb_rx_use_legacy_descriptor(core)) {
1490         struct e1000_rx_desc *d = &desc->legacy;
1491         size_t offset = offsetof(struct e1000_rx_desc, status);
1492         uint8_t status = d->status;
1493 
1494         d->status &= ~E1000_RXD_STAT_DD;
1495         pci_dma_write(dev, addr, desc, len);
1496 
1497         if (status & E1000_RXD_STAT_DD) {
1498             d->status = status;
1499             pci_dma_write(dev, addr + offset, &status, sizeof(status));
1500         }
1501     } else {
1502         union e1000_adv_rx_desc *d = &desc->adv;
1503         size_t offset =
1504             offsetof(union e1000_adv_rx_desc, wb.upper.status_error);
1505         uint32_t status = d->wb.upper.status_error;
1506 
1507         d->wb.upper.status_error &= ~E1000_RXD_STAT_DD;
1508         pci_dma_write(dev, addr, desc, len);
1509 
1510         if (status & E1000_RXD_STAT_DD) {
1511             d->wb.upper.status_error = status;
1512             pci_dma_write(dev, addr + offset, &status, sizeof(status));
1513         }
1514     }
1515 }
1516 
1517 static void
1518 igb_write_to_rx_buffers(IGBCore *core,
1519                         PCIDevice *d,
1520                         hwaddr ba,
1521                         uint16_t *written,
1522                         const char *data,
1523                         dma_addr_t data_len)
1524 {
1525     trace_igb_rx_desc_buff_write(ba, *written, data, data_len);
1526     pci_dma_write(d, ba + *written, data, data_len);
1527     *written += data_len;
1528 }
1529 
1530 static void
1531 igb_update_rx_stats(IGBCore *core, const E1000ERingInfo *rxi,
1532                     size_t pkt_size, size_t pkt_fcs_size)
1533 {
1534     eth_pkt_types_e pkt_type = net_rx_pkt_get_packet_type(core->rx_pkt);
1535     e1000x_update_rx_total_stats(core->mac, pkt_type, pkt_size, pkt_fcs_size);
1536 
1537     if (core->mac[MRQC] & 1) {
1538         uint16_t pool = rxi->idx % IGB_NUM_VM_POOLS;
1539 
1540         core->mac[PVFGORC0 + (pool * 64)] += pkt_size + 4;
1541         core->mac[PVFGPRC0 + (pool * 64)]++;
1542         if (pkt_type == ETH_PKT_MCAST) {
1543             core->mac[PVFMPRC0 + (pool * 64)]++;
1544         }
1545     }
1546 }
1547 
1548 static inline bool
1549 igb_rx_descr_threshold_hit(IGBCore *core, const E1000ERingInfo *rxi)
1550 {
1551     return igb_ring_free_descr_num(core, rxi) ==
1552            ((core->mac[E1000_SRRCTL(rxi->idx) >> 2] >> 20) & 31) * 16;
1553 }
1554 
1555 static void
1556 igb_write_packet_to_guest(IGBCore *core, struct NetRxPkt *pkt,
1557                           const E1000E_RxRing *rxr,
1558                           const E1000E_RSSInfo *rss_info,
1559                           uint16_t etqf, bool ts)
1560 {
1561     PCIDevice *d;
1562     dma_addr_t base;
1563     union e1000_rx_desc_union desc;
1564     size_t desc_size;
1565     size_t desc_offset = 0;
1566     size_t iov_ofs = 0;
1567 
1568     struct iovec *iov = net_rx_pkt_get_iovec(pkt);
1569     size_t size = net_rx_pkt_get_total_len(pkt);
1570     size_t total_size = size + e1000x_fcs_len(core->mac);
1571     const E1000ERingInfo *rxi = rxr->i;
1572     size_t bufsize = igb_rxbufsize(core, rxi);
1573 
1574     d = pcie_sriov_get_vf_at_index(core->owner, rxi->idx % 8);
1575     if (!d) {
1576         d = core->owner;
1577     }
1578 
1579     do {
1580         hwaddr ba;
1581         uint16_t written = 0;
1582         bool is_last = false;
1583 
1584         desc_size = total_size - desc_offset;
1585 
1586         if (desc_size > bufsize) {
1587             desc_size = bufsize;
1588         }
1589 
1590         if (igb_ring_empty(core, rxi)) {
1591             return;
1592         }
1593 
1594         base = igb_ring_head_descr(core, rxi);
1595 
1596         pci_dma_read(d, base, &desc, core->rx_desc_len);
1597 
1598         trace_e1000e_rx_descr(rxi->idx, base, core->rx_desc_len);
1599 
1600         igb_read_rx_descr(core, &desc, &ba);
1601 
1602         if (ba) {
1603             if (desc_offset < size) {
1604                 static const uint32_t fcs_pad;
1605                 size_t iov_copy;
1606                 size_t copy_size = size - desc_offset;
1607                 if (copy_size > bufsize) {
1608                     copy_size = bufsize;
1609                 }
1610 
1611                 /* Copy packet payload */
1612                 while (copy_size) {
1613                     iov_copy = MIN(copy_size, iov->iov_len - iov_ofs);
1614 
1615                     igb_write_to_rx_buffers(core, d, ba, &written,
1616                                             iov->iov_base + iov_ofs, iov_copy);
1617 
1618                     copy_size -= iov_copy;
1619                     iov_ofs += iov_copy;
1620                     if (iov_ofs == iov->iov_len) {
1621                         iov++;
1622                         iov_ofs = 0;
1623                     }
1624                 }
1625 
1626                 if (desc_offset + desc_size >= total_size) {
1627                     /* Simulate FCS checksum presence in the last descriptor */
1628                     igb_write_to_rx_buffers(core, d, ba, &written,
1629                           (const char *) &fcs_pad, e1000x_fcs_len(core->mac));
1630                 }
1631             }
1632         } else { /* as per intel docs; skip descriptors with null buf addr */
1633             trace_e1000e_rx_null_descriptor();
1634         }
1635         desc_offset += desc_size;
1636         if (desc_offset >= total_size) {
1637             is_last = true;
1638         }
1639 
1640         igb_write_rx_descr(core, &desc, is_last ? core->rx_pkt : NULL,
1641                            rss_info, etqf, ts, written);
1642         igb_pci_dma_write_rx_desc(core, d, base, &desc, core->rx_desc_len);
1643 
1644         igb_ring_advance(core, rxi, core->rx_desc_len / E1000_MIN_RX_DESC_LEN);
1645 
1646     } while (desc_offset < total_size);
1647 
1648     igb_update_rx_stats(core, rxi, size, total_size);
1649 }
1650 
1651 static bool
1652 igb_rx_strip_vlan(IGBCore *core, const E1000ERingInfo *rxi)
1653 {
1654     if (core->mac[MRQC] & 1) {
1655         uint16_t pool = rxi->idx % IGB_NUM_VM_POOLS;
1656         /* Sec 7.10.3.8: CTRL.VME is ignored, only VMOLR/RPLOLR is used */
1657         return (net_rx_pkt_get_packet_type(core->rx_pkt) == ETH_PKT_MCAST) ?
1658                 core->mac[RPLOLR] & E1000_RPLOLR_STRVLAN :
1659                 core->mac[VMOLR0 + pool] & E1000_VMOLR_STRVLAN;
1660     }
1661 
1662     return e1000x_vlan_enabled(core->mac);
1663 }
1664 
1665 static inline void
1666 igb_rx_fix_l4_csum(IGBCore *core, struct NetRxPkt *pkt)
1667 {
1668     struct virtio_net_hdr *vhdr = net_rx_pkt_get_vhdr(pkt);
1669 
1670     if (vhdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1671         net_rx_pkt_fix_l4_csum(pkt);
1672     }
1673 }
1674 
1675 ssize_t
1676 igb_receive_iov(IGBCore *core, const struct iovec *iov, int iovcnt)
1677 {
1678     return igb_receive_internal(core, iov, iovcnt, core->has_vnet, NULL);
1679 }
1680 
1681 static ssize_t
1682 igb_receive_internal(IGBCore *core, const struct iovec *iov, int iovcnt,
1683                      bool has_vnet, bool *external_tx)
1684 {
1685     uint16_t queues = 0;
1686     uint32_t causes = 0;
1687     uint32_t ecauses = 0;
1688     union {
1689         L2Header l2_header;
1690         uint8_t octets[ETH_ZLEN];
1691     } buf;
1692     struct iovec min_iov;
1693     size_t size, orig_size;
1694     size_t iov_ofs = 0;
1695     E1000E_RxRing rxr;
1696     E1000E_RSSInfo rss_info;
1697     uint16_t etqf;
1698     bool ts;
1699     size_t total_size;
1700     int strip_vlan_index;
1701     int i;
1702 
1703     trace_e1000e_rx_receive_iov(iovcnt);
1704 
1705     if (external_tx) {
1706         *external_tx = true;
1707     }
1708 
1709     if (!e1000x_hw_rx_enabled(core->mac)) {
1710         return -1;
1711     }
1712 
1713     /* Pull virtio header in */
1714     if (has_vnet) {
1715         net_rx_pkt_set_vhdr_iovec(core->rx_pkt, iov, iovcnt);
1716         iov_ofs = sizeof(struct virtio_net_hdr);
1717     } else {
1718         net_rx_pkt_unset_vhdr(core->rx_pkt);
1719     }
1720 
1721     orig_size = iov_size(iov, iovcnt);
1722     size = orig_size - iov_ofs;
1723 
1724     /* Pad to minimum Ethernet frame length */
1725     if (size < sizeof(buf)) {
1726         iov_to_buf(iov, iovcnt, iov_ofs, &buf, size);
1727         memset(&buf.octets[size], 0, sizeof(buf) - size);
1728         e1000x_inc_reg_if_not_full(core->mac, RUC);
1729         min_iov.iov_base = &buf;
1730         min_iov.iov_len = size = sizeof(buf);
1731         iovcnt = 1;
1732         iov = &min_iov;
1733         iov_ofs = 0;
1734     } else {
1735         iov_to_buf(iov, iovcnt, iov_ofs, &buf, sizeof(buf.l2_header));
1736     }
1737 
1738     net_rx_pkt_set_packet_type(core->rx_pkt,
1739                                get_eth_packet_type(&buf.l2_header.eth));
1740     net_rx_pkt_set_protocols(core->rx_pkt, iov, iovcnt, iov_ofs);
1741 
1742     queues = igb_receive_assign(core, iov, iovcnt, iov_ofs,
1743                                 &buf.l2_header, size,
1744                                 &rss_info, &etqf, &ts, external_tx);
1745     if (!queues) {
1746         trace_e1000e_rx_flt_dropped();
1747         return orig_size;
1748     }
1749 
1750     for (i = 0; i < IGB_NUM_QUEUES; i++) {
1751         if (!(queues & BIT(i)) ||
1752             !(core->mac[RXDCTL0 + (i * 16)] & E1000_RXDCTL_QUEUE_ENABLE)) {
1753             continue;
1754         }
1755 
1756         igb_rx_ring_init(core, &rxr, i);
1757 
1758         if (!igb_rx_strip_vlan(core, rxr.i)) {
1759             strip_vlan_index = -1;
1760         } else if (core->mac[CTRL_EXT] & BIT(26)) {
1761             strip_vlan_index = 1;
1762         } else {
1763             strip_vlan_index = 0;
1764         }
1765 
1766         net_rx_pkt_attach_iovec_ex(core->rx_pkt, iov, iovcnt, iov_ofs,
1767                                    strip_vlan_index,
1768                                    core->mac[VET] & 0xffff,
1769                                    core->mac[VET] >> 16);
1770 
1771         total_size = net_rx_pkt_get_total_len(core->rx_pkt) +
1772             e1000x_fcs_len(core->mac);
1773 
1774         if (!igb_has_rxbufs(core, rxr.i, total_size)) {
1775             causes |= E1000_ICS_RXO;
1776             trace_e1000e_rx_not_written_to_guest(rxr.i->idx);
1777             continue;
1778         }
1779 
1780         causes |= E1000_ICR_RXDW;
1781 
1782         igb_rx_fix_l4_csum(core, core->rx_pkt);
1783         igb_write_packet_to_guest(core, core->rx_pkt, &rxr, &rss_info, etqf, ts);
1784 
1785         /* Check if receive descriptor minimum threshold hit */
1786         if (igb_rx_descr_threshold_hit(core, rxr.i)) {
1787             causes |= E1000_ICS_RXDMT0;
1788         }
1789 
1790         ecauses |= igb_rx_wb_eic(core, rxr.i->idx);
1791 
1792         trace_e1000e_rx_written_to_guest(rxr.i->idx);
1793     }
1794 
1795     trace_e1000e_rx_interrupt_set(causes);
1796     igb_raise_interrupts(core, EICR, ecauses);
1797     igb_raise_interrupts(core, ICR, causes);
1798 
1799     return orig_size;
1800 }
1801 
1802 static inline bool
1803 igb_have_autoneg(IGBCore *core)
1804 {
1805     return core->phy[MII_BMCR] & MII_BMCR_AUTOEN;
1806 }
1807 
1808 static void igb_update_flowctl_status(IGBCore *core)
1809 {
1810     if (igb_have_autoneg(core) && core->phy[MII_BMSR] & MII_BMSR_AN_COMP) {
1811         trace_e1000e_link_autoneg_flowctl(true);
1812         core->mac[CTRL] |= E1000_CTRL_TFCE | E1000_CTRL_RFCE;
1813     } else {
1814         trace_e1000e_link_autoneg_flowctl(false);
1815     }
1816 }
1817 
1818 static inline void
1819 igb_link_down(IGBCore *core)
1820 {
1821     e1000x_update_regs_on_link_down(core->mac, core->phy);
1822     igb_update_flowctl_status(core);
1823 }
1824 
1825 static inline void
1826 igb_set_phy_ctrl(IGBCore *core, uint16_t val)
1827 {
1828     /* bits 0-5 reserved; MII_BMCR_[ANRESTART,RESET] are self clearing */
1829     core->phy[MII_BMCR] = val & ~(0x3f | MII_BMCR_RESET | MII_BMCR_ANRESTART);
1830 
1831     if ((val & MII_BMCR_ANRESTART) && igb_have_autoneg(core)) {
1832         e1000x_restart_autoneg(core->mac, core->phy, core->autoneg_timer);
1833     }
1834 }
1835 
1836 void igb_core_set_link_status(IGBCore *core)
1837 {
1838     NetClientState *nc = qemu_get_queue(core->owner_nic);
1839     uint32_t old_status = core->mac[STATUS];
1840 
1841     trace_e1000e_link_status_changed(nc->link_down ? false : true);
1842 
1843     if (nc->link_down) {
1844         e1000x_update_regs_on_link_down(core->mac, core->phy);
1845     } else {
1846         if (igb_have_autoneg(core) &&
1847             !(core->phy[MII_BMSR] & MII_BMSR_AN_COMP)) {
1848             e1000x_restart_autoneg(core->mac, core->phy,
1849                                    core->autoneg_timer);
1850         } else {
1851             e1000x_update_regs_on_link_up(core->mac, core->phy);
1852             igb_start_recv(core);
1853         }
1854     }
1855 
1856     if (core->mac[STATUS] != old_status) {
1857         igb_raise_interrupts(core, ICR, E1000_ICR_LSC);
1858     }
1859 }
1860 
1861 static void
1862 igb_set_ctrl(IGBCore *core, int index, uint32_t val)
1863 {
1864     trace_e1000e_core_ctrl_write(index, val);
1865 
1866     /* RST is self clearing */
1867     core->mac[CTRL] = val & ~E1000_CTRL_RST;
1868     core->mac[CTRL_DUP] = core->mac[CTRL];
1869 
1870     trace_e1000e_link_set_params(
1871         !!(val & E1000_CTRL_ASDE),
1872         (val & E1000_CTRL_SPD_SEL) >> E1000_CTRL_SPD_SHIFT,
1873         !!(val & E1000_CTRL_FRCSPD),
1874         !!(val & E1000_CTRL_FRCDPX),
1875         !!(val & E1000_CTRL_RFCE),
1876         !!(val & E1000_CTRL_TFCE));
1877 
1878     if (val & E1000_CTRL_RST) {
1879         trace_e1000e_core_ctrl_sw_reset();
1880         igb_reset(core, true);
1881     }
1882 
1883     if (val & E1000_CTRL_PHY_RST) {
1884         trace_e1000e_core_ctrl_phy_reset();
1885         core->mac[STATUS] |= E1000_STATUS_PHYRA;
1886     }
1887 }
1888 
1889 static void
1890 igb_set_rfctl(IGBCore *core, int index, uint32_t val)
1891 {
1892     trace_e1000e_rx_set_rfctl(val);
1893 
1894     if (!(val & E1000_RFCTL_ISCSI_DIS)) {
1895         trace_e1000e_wrn_iscsi_filtering_not_supported();
1896     }
1897 
1898     if (!(val & E1000_RFCTL_NFSW_DIS)) {
1899         trace_e1000e_wrn_nfsw_filtering_not_supported();
1900     }
1901 
1902     if (!(val & E1000_RFCTL_NFSR_DIS)) {
1903         trace_e1000e_wrn_nfsr_filtering_not_supported();
1904     }
1905 
1906     core->mac[RFCTL] = val;
1907 }
1908 
1909 static void
1910 igb_calc_rxdesclen(IGBCore *core)
1911 {
1912     if (igb_rx_use_legacy_descriptor(core)) {
1913         core->rx_desc_len = sizeof(struct e1000_rx_desc);
1914     } else {
1915         core->rx_desc_len = sizeof(union e1000_adv_rx_desc);
1916     }
1917     trace_e1000e_rx_desc_len(core->rx_desc_len);
1918 }
1919 
1920 static void
1921 igb_set_rx_control(IGBCore *core, int index, uint32_t val)
1922 {
1923     core->mac[RCTL] = val;
1924     trace_e1000e_rx_set_rctl(core->mac[RCTL]);
1925 
1926     if (val & E1000_RCTL_DTYP_MASK) {
1927         qemu_log_mask(LOG_GUEST_ERROR,
1928                       "igb: RCTL.DTYP must be zero for compatibility");
1929     }
1930 
1931     if (val & E1000_RCTL_EN) {
1932         igb_calc_rxdesclen(core);
1933         igb_start_recv(core);
1934     }
1935 }
1936 
1937 static inline bool
1938 igb_postpone_interrupt(IGBIntrDelayTimer *timer)
1939 {
1940     if (timer->running) {
1941         trace_e1000e_irq_postponed_by_xitr(timer->delay_reg << 2);
1942 
1943         return true;
1944     }
1945 
1946     if (timer->core->mac[timer->delay_reg] != 0) {
1947         igb_intrmgr_rearm_timer(timer);
1948     }
1949 
1950     return false;
1951 }
1952 
1953 static inline bool
1954 igb_eitr_should_postpone(IGBCore *core, int idx)
1955 {
1956     return igb_postpone_interrupt(&core->eitr[idx]);
1957 }
1958 
1959 static void igb_send_msix(IGBCore *core, uint32_t causes)
1960 {
1961     int vector;
1962 
1963     for (vector = 0; vector < IGB_INTR_NUM; ++vector) {
1964         if ((causes & BIT(vector)) && !igb_eitr_should_postpone(core, vector)) {
1965 
1966             trace_e1000e_irq_msix_notify_vec(vector);
1967             igb_msix_notify(core, vector);
1968         }
1969     }
1970 }
1971 
1972 static inline void
1973 igb_fix_icr_asserted(IGBCore *core)
1974 {
1975     core->mac[ICR] &= ~E1000_ICR_ASSERTED;
1976     if (core->mac[ICR]) {
1977         core->mac[ICR] |= E1000_ICR_ASSERTED;
1978     }
1979 
1980     trace_e1000e_irq_fix_icr_asserted(core->mac[ICR]);
1981 }
1982 
1983 static void igb_raise_interrupts(IGBCore *core, size_t index, uint32_t causes)
1984 {
1985     uint32_t old_causes = core->mac[ICR] & core->mac[IMS];
1986     uint32_t old_ecauses = core->mac[EICR] & core->mac[EIMS];
1987     uint32_t raised_causes;
1988     uint32_t raised_ecauses;
1989     uint32_t int_alloc;
1990 
1991     trace_e1000e_irq_set(index << 2,
1992                          core->mac[index], core->mac[index] | causes);
1993 
1994     core->mac[index] |= causes;
1995 
1996     if (core->mac[GPIE] & E1000_GPIE_MSIX_MODE) {
1997         raised_causes = core->mac[ICR] & core->mac[IMS] & ~old_causes;
1998 
1999         if (raised_causes & E1000_ICR_DRSTA) {
2000             int_alloc = core->mac[IVAR_MISC] & 0xff;
2001             if (int_alloc & E1000_IVAR_VALID) {
2002                 core->mac[EICR] |= BIT(int_alloc & 0x1f);
2003             }
2004         }
2005         /* Check if other bits (excluding the TCP Timer) are enabled. */
2006         if (raised_causes & ~E1000_ICR_DRSTA) {
2007             int_alloc = (core->mac[IVAR_MISC] >> 8) & 0xff;
2008             if (int_alloc & E1000_IVAR_VALID) {
2009                 core->mac[EICR] |= BIT(int_alloc & 0x1f);
2010             }
2011         }
2012 
2013         raised_ecauses = core->mac[EICR] & core->mac[EIMS] & ~old_ecauses;
2014         if (!raised_ecauses) {
2015             return;
2016         }
2017 
2018         igb_send_msix(core, raised_ecauses);
2019     } else {
2020         igb_fix_icr_asserted(core);
2021 
2022         raised_causes = core->mac[ICR] & core->mac[IMS] & ~old_causes;
2023         if (!raised_causes) {
2024             return;
2025         }
2026 
2027         core->mac[EICR] |= (raised_causes & E1000_ICR_DRSTA) | E1000_EICR_OTHER;
2028 
2029         if (msix_enabled(core->owner)) {
2030             trace_e1000e_irq_msix_notify_vec(0);
2031             msix_notify(core->owner, 0);
2032         } else if (msi_enabled(core->owner)) {
2033             trace_e1000e_irq_msi_notify(raised_causes);
2034             msi_notify(core->owner, 0);
2035         } else {
2036             igb_raise_legacy_irq(core);
2037         }
2038     }
2039 }
2040 
2041 static void igb_lower_interrupts(IGBCore *core, size_t index, uint32_t causes)
2042 {
2043     trace_e1000e_irq_clear(index << 2,
2044                            core->mac[index], core->mac[index] & ~causes);
2045 
2046     core->mac[index] &= ~causes;
2047 
2048     trace_e1000e_irq_pending_interrupts(core->mac[ICR] & core->mac[IMS],
2049                                         core->mac[ICR], core->mac[IMS]);
2050 
2051     if (!(core->mac[ICR] & core->mac[IMS]) &&
2052         !(core->mac[GPIE] & E1000_GPIE_MSIX_MODE)) {
2053         core->mac[EICR] &= ~E1000_EICR_OTHER;
2054 
2055         if (!msix_enabled(core->owner) && !msi_enabled(core->owner)) {
2056             igb_lower_legacy_irq(core);
2057         }
2058     }
2059 }
2060 
2061 static void igb_set_eics(IGBCore *core, int index, uint32_t val)
2062 {
2063     bool msix = !!(core->mac[GPIE] & E1000_GPIE_MSIX_MODE);
2064     uint32_t mask = msix ? E1000_EICR_MSIX_MASK : E1000_EICR_LEGACY_MASK;
2065 
2066     trace_igb_irq_write_eics(val, msix);
2067     igb_raise_interrupts(core, EICR, val & mask);
2068 }
2069 
2070 static void igb_set_eims(IGBCore *core, int index, uint32_t val)
2071 {
2072     bool msix = !!(core->mac[GPIE] & E1000_GPIE_MSIX_MODE);
2073     uint32_t mask = msix ? E1000_EICR_MSIX_MASK : E1000_EICR_LEGACY_MASK;
2074 
2075     trace_igb_irq_write_eims(val, msix);
2076     igb_raise_interrupts(core, EIMS, val & mask);
2077 }
2078 
2079 static void mailbox_interrupt_to_vf(IGBCore *core, uint16_t vfn)
2080 {
2081     uint32_t ent = core->mac[VTIVAR_MISC + vfn];
2082     uint32_t causes;
2083 
2084     if ((ent & E1000_IVAR_VALID)) {
2085         causes = (ent & 0x3) << (22 - vfn * IGBVF_MSIX_VEC_NUM);
2086         igb_raise_interrupts(core, EICR, causes);
2087     }
2088 }
2089 
2090 static void mailbox_interrupt_to_pf(IGBCore *core)
2091 {
2092     igb_raise_interrupts(core, ICR, E1000_ICR_VMMB);
2093 }
2094 
2095 static void igb_set_pfmailbox(IGBCore *core, int index, uint32_t val)
2096 {
2097     uint16_t vfn = index - P2VMAILBOX0;
2098 
2099     trace_igb_set_pfmailbox(vfn, val);
2100 
2101     if (val & E1000_P2VMAILBOX_STS) {
2102         core->mac[V2PMAILBOX0 + vfn] |= E1000_V2PMAILBOX_PFSTS;
2103         mailbox_interrupt_to_vf(core, vfn);
2104     }
2105 
2106     if (val & E1000_P2VMAILBOX_ACK) {
2107         core->mac[V2PMAILBOX0 + vfn] |= E1000_V2PMAILBOX_PFACK;
2108         mailbox_interrupt_to_vf(core, vfn);
2109     }
2110 
2111     /* Buffer Taken by PF (can be set only if the VFU is cleared). */
2112     if (val & E1000_P2VMAILBOX_PFU) {
2113         if (!(core->mac[index] & E1000_P2VMAILBOX_VFU)) {
2114             core->mac[index] |= E1000_P2VMAILBOX_PFU;
2115             core->mac[V2PMAILBOX0 + vfn] |= E1000_V2PMAILBOX_PFU;
2116         }
2117     } else {
2118         core->mac[index] &= ~E1000_P2VMAILBOX_PFU;
2119         core->mac[V2PMAILBOX0 + vfn] &= ~E1000_V2PMAILBOX_PFU;
2120     }
2121 
2122     if (val & E1000_P2VMAILBOX_RVFU) {
2123         core->mac[V2PMAILBOX0 + vfn] &= ~E1000_V2PMAILBOX_VFU;
2124         core->mac[MBVFICR] &= ~((E1000_MBVFICR_VFACK_VF1 << vfn) |
2125                                 (E1000_MBVFICR_VFREQ_VF1 << vfn));
2126     }
2127 }
2128 
2129 static void igb_set_vfmailbox(IGBCore *core, int index, uint32_t val)
2130 {
2131     uint16_t vfn = index - V2PMAILBOX0;
2132 
2133     trace_igb_set_vfmailbox(vfn, val);
2134 
2135     if (val & E1000_V2PMAILBOX_REQ) {
2136         core->mac[MBVFICR] |= E1000_MBVFICR_VFREQ_VF1 << vfn;
2137         mailbox_interrupt_to_pf(core);
2138     }
2139 
2140     if (val & E1000_V2PMAILBOX_ACK) {
2141         core->mac[MBVFICR] |= E1000_MBVFICR_VFACK_VF1 << vfn;
2142         mailbox_interrupt_to_pf(core);
2143     }
2144 
2145     /* Buffer Taken by VF (can be set only if the PFU is cleared). */
2146     if (val & E1000_V2PMAILBOX_VFU) {
2147         if (!(core->mac[index] & E1000_V2PMAILBOX_PFU)) {
2148             core->mac[index] |= E1000_V2PMAILBOX_VFU;
2149             core->mac[P2VMAILBOX0 + vfn] |= E1000_P2VMAILBOX_VFU;
2150         }
2151     } else {
2152         core->mac[index] &= ~E1000_V2PMAILBOX_VFU;
2153         core->mac[P2VMAILBOX0 + vfn] &= ~E1000_P2VMAILBOX_VFU;
2154     }
2155 }
2156 
2157 static void igb_vf_reset(IGBCore *core, uint16_t vfn)
2158 {
2159     uint16_t qn0 = vfn;
2160     uint16_t qn1 = vfn + IGB_NUM_VM_POOLS;
2161 
2162     /* disable Rx and Tx for the VF*/
2163     core->mac[RXDCTL0 + (qn0 * 16)] &= ~E1000_RXDCTL_QUEUE_ENABLE;
2164     core->mac[RXDCTL0 + (qn1 * 16)] &= ~E1000_RXDCTL_QUEUE_ENABLE;
2165     core->mac[TXDCTL0 + (qn0 * 16)] &= ~E1000_TXDCTL_QUEUE_ENABLE;
2166     core->mac[TXDCTL0 + (qn1 * 16)] &= ~E1000_TXDCTL_QUEUE_ENABLE;
2167     core->mac[VFRE] &= ~BIT(vfn);
2168     core->mac[VFTE] &= ~BIT(vfn);
2169     /* indicate VF reset to PF */
2170     core->mac[VFLRE] |= BIT(vfn);
2171     /* VFLRE and mailbox use the same interrupt cause */
2172     mailbox_interrupt_to_pf(core);
2173 }
2174 
2175 static void igb_w1c(IGBCore *core, int index, uint32_t val)
2176 {
2177     core->mac[index] &= ~val;
2178 }
2179 
2180 static void igb_set_eimc(IGBCore *core, int index, uint32_t val)
2181 {
2182     bool msix = !!(core->mac[GPIE] & E1000_GPIE_MSIX_MODE);
2183     uint32_t mask = msix ? E1000_EICR_MSIX_MASK : E1000_EICR_LEGACY_MASK;
2184 
2185     trace_igb_irq_write_eimc(val, msix);
2186 
2187     /* Interrupts are disabled via a write to EIMC and reflected in EIMS. */
2188     igb_lower_interrupts(core, EIMS, val & mask);
2189 }
2190 
2191 static void igb_set_eiac(IGBCore *core, int index, uint32_t val)
2192 {
2193     bool msix = !!(core->mac[GPIE] & E1000_GPIE_MSIX_MODE);
2194 
2195     if (msix) {
2196         trace_igb_irq_write_eiac(val);
2197 
2198         /*
2199          * TODO: When using IOV, the bits that correspond to MSI-X vectors
2200          * that are assigned to a VF are read-only.
2201          */
2202         core->mac[EIAC] |= (val & E1000_EICR_MSIX_MASK);
2203     }
2204 }
2205 
2206 static void igb_set_eiam(IGBCore *core, int index, uint32_t val)
2207 {
2208     bool msix = !!(core->mac[GPIE] & E1000_GPIE_MSIX_MODE);
2209 
2210     /*
2211      * TODO: When using IOV, the bits that correspond to MSI-X vectors that
2212      * are assigned to a VF are read-only.
2213      */
2214     core->mac[EIAM] |=
2215         ~(val & (msix ? E1000_EICR_MSIX_MASK : E1000_EICR_LEGACY_MASK));
2216 
2217     trace_igb_irq_write_eiam(val, msix);
2218 }
2219 
2220 static void igb_set_eicr(IGBCore *core, int index, uint32_t val)
2221 {
2222     bool msix = !!(core->mac[GPIE] & E1000_GPIE_MSIX_MODE);
2223 
2224     /*
2225      * TODO: In IOV mode, only bit zero of this vector is available for the PF
2226      * function.
2227      */
2228     uint32_t mask = msix ? E1000_EICR_MSIX_MASK : E1000_EICR_LEGACY_MASK;
2229 
2230     trace_igb_irq_write_eicr(val, msix);
2231     igb_lower_interrupts(core, EICR, val & mask);
2232 }
2233 
2234 static void igb_set_vtctrl(IGBCore *core, int index, uint32_t val)
2235 {
2236     uint16_t vfn;
2237 
2238     if (val & E1000_CTRL_RST) {
2239         vfn = (index - PVTCTRL0) / 0x40;
2240         igb_vf_reset(core, vfn);
2241     }
2242 }
2243 
2244 static void igb_set_vteics(IGBCore *core, int index, uint32_t val)
2245 {
2246     uint16_t vfn = (index - PVTEICS0) / 0x40;
2247 
2248     core->mac[index] = val;
2249     igb_set_eics(core, EICS, (val & 0x7) << (22 - vfn * IGBVF_MSIX_VEC_NUM));
2250 }
2251 
2252 static void igb_set_vteims(IGBCore *core, int index, uint32_t val)
2253 {
2254     uint16_t vfn = (index - PVTEIMS0) / 0x40;
2255 
2256     core->mac[index] = val;
2257     igb_set_eims(core, EIMS, (val & 0x7) << (22 - vfn * IGBVF_MSIX_VEC_NUM));
2258 }
2259 
2260 static void igb_set_vteimc(IGBCore *core, int index, uint32_t val)
2261 {
2262     uint16_t vfn = (index - PVTEIMC0) / 0x40;
2263 
2264     core->mac[index] = val;
2265     igb_set_eimc(core, EIMC, (val & 0x7) << (22 - vfn * IGBVF_MSIX_VEC_NUM));
2266 }
2267 
2268 static void igb_set_vteiac(IGBCore *core, int index, uint32_t val)
2269 {
2270     uint16_t vfn = (index - PVTEIAC0) / 0x40;
2271 
2272     core->mac[index] = val;
2273     igb_set_eiac(core, EIAC, (val & 0x7) << (22 - vfn * IGBVF_MSIX_VEC_NUM));
2274 }
2275 
2276 static void igb_set_vteiam(IGBCore *core, int index, uint32_t val)
2277 {
2278     uint16_t vfn = (index - PVTEIAM0) / 0x40;
2279 
2280     core->mac[index] = val;
2281     igb_set_eiam(core, EIAM, (val & 0x7) << (22 - vfn * IGBVF_MSIX_VEC_NUM));
2282 }
2283 
2284 static void igb_set_vteicr(IGBCore *core, int index, uint32_t val)
2285 {
2286     uint16_t vfn = (index - PVTEICR0) / 0x40;
2287 
2288     core->mac[index] = val;
2289     igb_set_eicr(core, EICR, (val & 0x7) << (22 - vfn * IGBVF_MSIX_VEC_NUM));
2290 }
2291 
2292 static void igb_set_vtivar(IGBCore *core, int index, uint32_t val)
2293 {
2294     uint16_t vfn = (index - VTIVAR);
2295     uint16_t qn = vfn;
2296     uint8_t ent;
2297     int n;
2298 
2299     core->mac[index] = val;
2300 
2301     /* Get assigned vector associated with queue Rx#0. */
2302     if ((val & E1000_IVAR_VALID)) {
2303         n = igb_ivar_entry_rx(qn);
2304         ent = E1000_IVAR_VALID | (24 - vfn * IGBVF_MSIX_VEC_NUM - (2 - (val & 0x7)));
2305         core->mac[IVAR0 + n / 4] |= ent << 8 * (n % 4);
2306     }
2307 
2308     /* Get assigned vector associated with queue Tx#0 */
2309     ent = val >> 8;
2310     if ((ent & E1000_IVAR_VALID)) {
2311         n = igb_ivar_entry_tx(qn);
2312         ent = E1000_IVAR_VALID | (24 - vfn * IGBVF_MSIX_VEC_NUM - (2 - (ent & 0x7)));
2313         core->mac[IVAR0 + n / 4] |= ent << 8 * (n % 4);
2314     }
2315 
2316     /*
2317      * Ignoring assigned vectors associated with queues Rx#1 and Tx#1 for now.
2318      */
2319 }
2320 
2321 static inline void
2322 igb_autoneg_timer(void *opaque)
2323 {
2324     IGBCore *core = opaque;
2325     if (!qemu_get_queue(core->owner_nic)->link_down) {
2326         e1000x_update_regs_on_autoneg_done(core->mac, core->phy);
2327         igb_start_recv(core);
2328 
2329         igb_update_flowctl_status(core);
2330         /* signal link status change to the guest */
2331         igb_raise_interrupts(core, ICR, E1000_ICR_LSC);
2332     }
2333 }
2334 
2335 static inline uint16_t
2336 igb_get_reg_index_with_offset(const uint16_t *mac_reg_access, hwaddr addr)
2337 {
2338     uint16_t index = (addr & 0x1ffff) >> 2;
2339     return index + (mac_reg_access[index] & 0xfffe);
2340 }
2341 
2342 static const char igb_phy_regcap[MAX_PHY_REG_ADDRESS + 1] = {
2343     [MII_BMCR]                   = PHY_RW,
2344     [MII_BMSR]                   = PHY_R,
2345     [MII_PHYID1]                 = PHY_R,
2346     [MII_PHYID2]                 = PHY_R,
2347     [MII_ANAR]                   = PHY_RW,
2348     [MII_ANLPAR]                 = PHY_R,
2349     [MII_ANER]                   = PHY_R,
2350     [MII_ANNP]                   = PHY_RW,
2351     [MII_ANLPRNP]                = PHY_R,
2352     [MII_CTRL1000]               = PHY_RW,
2353     [MII_STAT1000]               = PHY_R,
2354     [MII_EXTSTAT]                = PHY_R,
2355 
2356     [IGP01E1000_PHY_PORT_CONFIG] = PHY_RW,
2357     [IGP01E1000_PHY_PORT_STATUS] = PHY_R,
2358     [IGP01E1000_PHY_PORT_CTRL]   = PHY_RW,
2359     [IGP01E1000_PHY_LINK_HEALTH] = PHY_R,
2360     [IGP02E1000_PHY_POWER_MGMT]  = PHY_RW,
2361     [IGP01E1000_PHY_PAGE_SELECT] = PHY_W
2362 };
2363 
2364 static void
2365 igb_phy_reg_write(IGBCore *core, uint32_t addr, uint16_t data)
2366 {
2367     assert(addr <= MAX_PHY_REG_ADDRESS);
2368 
2369     if (addr == MII_BMCR) {
2370         igb_set_phy_ctrl(core, data);
2371     } else {
2372         core->phy[addr] = data;
2373     }
2374 }
2375 
2376 static void
2377 igb_set_mdic(IGBCore *core, int index, uint32_t val)
2378 {
2379     uint32_t data = val & E1000_MDIC_DATA_MASK;
2380     uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT);
2381 
2382     if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) { /* phy # */
2383         val = core->mac[MDIC] | E1000_MDIC_ERROR;
2384     } else if (val & E1000_MDIC_OP_READ) {
2385         if (!(igb_phy_regcap[addr] & PHY_R)) {
2386             trace_igb_core_mdic_read_unhandled(addr);
2387             val |= E1000_MDIC_ERROR;
2388         } else {
2389             val = (val ^ data) | core->phy[addr];
2390             trace_igb_core_mdic_read(addr, val);
2391         }
2392     } else if (val & E1000_MDIC_OP_WRITE) {
2393         if (!(igb_phy_regcap[addr] & PHY_W)) {
2394             trace_igb_core_mdic_write_unhandled(addr);
2395             val |= E1000_MDIC_ERROR;
2396         } else {
2397             trace_igb_core_mdic_write(addr, data);
2398             igb_phy_reg_write(core, addr, data);
2399         }
2400     }
2401     core->mac[MDIC] = val | E1000_MDIC_READY;
2402 
2403     if (val & E1000_MDIC_INT_EN) {
2404         igb_raise_interrupts(core, ICR, E1000_ICR_MDAC);
2405     }
2406 }
2407 
2408 static void
2409 igb_set_rdt(IGBCore *core, int index, uint32_t val)
2410 {
2411     core->mac[index] = val & 0xffff;
2412     trace_e1000e_rx_set_rdt(igb_mq_queue_idx(RDT0, index), val);
2413     igb_start_recv(core);
2414 }
2415 
2416 static void
2417 igb_set_status(IGBCore *core, int index, uint32_t val)
2418 {
2419     if ((val & E1000_STATUS_PHYRA) == 0) {
2420         core->mac[index] &= ~E1000_STATUS_PHYRA;
2421     }
2422 }
2423 
2424 static void
2425 igb_set_ctrlext(IGBCore *core, int index, uint32_t val)
2426 {
2427     trace_igb_link_set_ext_params(!!(val & E1000_CTRL_EXT_ASDCHK),
2428                                   !!(val & E1000_CTRL_EXT_SPD_BYPS),
2429                                   !!(val & E1000_CTRL_EXT_PFRSTD));
2430 
2431     /* Zero self-clearing bits */
2432     val &= ~(E1000_CTRL_EXT_ASDCHK | E1000_CTRL_EXT_EE_RST);
2433     core->mac[CTRL_EXT] = val;
2434 
2435     if (core->mac[CTRL_EXT] & E1000_CTRL_EXT_PFRSTD) {
2436         for (int vfn = 0; vfn < IGB_MAX_VF_FUNCTIONS; vfn++) {
2437             core->mac[V2PMAILBOX0 + vfn] &= ~E1000_V2PMAILBOX_RSTI;
2438             core->mac[V2PMAILBOX0 + vfn] |= E1000_V2PMAILBOX_RSTD;
2439         }
2440     }
2441 }
2442 
2443 static void
2444 igb_set_pbaclr(IGBCore *core, int index, uint32_t val)
2445 {
2446     int i;
2447 
2448     core->mac[PBACLR] = val & E1000_PBACLR_VALID_MASK;
2449 
2450     if (!msix_enabled(core->owner)) {
2451         return;
2452     }
2453 
2454     for (i = 0; i < IGB_INTR_NUM; i++) {
2455         if (core->mac[PBACLR] & BIT(i)) {
2456             msix_clr_pending(core->owner, i);
2457         }
2458     }
2459 }
2460 
2461 static void
2462 igb_set_fcrth(IGBCore *core, int index, uint32_t val)
2463 {
2464     core->mac[FCRTH] = val & 0xFFF8;
2465 }
2466 
2467 static void
2468 igb_set_fcrtl(IGBCore *core, int index, uint32_t val)
2469 {
2470     core->mac[FCRTL] = val & 0x8000FFF8;
2471 }
2472 
2473 #define IGB_LOW_BITS_SET_FUNC(num)                             \
2474     static void                                                \
2475     igb_set_##num##bit(IGBCore *core, int index, uint32_t val) \
2476     {                                                          \
2477         core->mac[index] = val & (BIT(num) - 1);               \
2478     }
2479 
2480 IGB_LOW_BITS_SET_FUNC(4)
2481 IGB_LOW_BITS_SET_FUNC(13)
2482 IGB_LOW_BITS_SET_FUNC(16)
2483 
2484 static void
2485 igb_set_dlen(IGBCore *core, int index, uint32_t val)
2486 {
2487     core->mac[index] = val & 0xffff0;
2488 }
2489 
2490 static void
2491 igb_set_dbal(IGBCore *core, int index, uint32_t val)
2492 {
2493     core->mac[index] = val & E1000_XDBAL_MASK;
2494 }
2495 
2496 static void
2497 igb_set_tdt(IGBCore *core, int index, uint32_t val)
2498 {
2499     IGB_TxRing txr;
2500     int qn = igb_mq_queue_idx(TDT0, index);
2501 
2502     core->mac[index] = val & 0xffff;
2503 
2504     igb_tx_ring_init(core, &txr, qn);
2505     igb_start_xmit(core, &txr);
2506 }
2507 
2508 static void
2509 igb_set_ics(IGBCore *core, int index, uint32_t val)
2510 {
2511     trace_e1000e_irq_write_ics(val);
2512     igb_raise_interrupts(core, ICR, val);
2513 }
2514 
2515 static void
2516 igb_set_imc(IGBCore *core, int index, uint32_t val)
2517 {
2518     trace_e1000e_irq_ims_clear_set_imc(val);
2519     igb_lower_interrupts(core, IMS, val);
2520 }
2521 
2522 static void
2523 igb_set_ims(IGBCore *core, int index, uint32_t val)
2524 {
2525     igb_raise_interrupts(core, IMS, val & 0x77D4FBFD);
2526 }
2527 
2528 static void igb_nsicr(IGBCore *core)
2529 {
2530     /*
2531      * If GPIE.NSICR = 0, then the clear of IMS will occur only if at
2532      * least one bit is set in the IMS and there is a true interrupt as
2533      * reflected in ICR.INTA.
2534      */
2535     if ((core->mac[GPIE] & E1000_GPIE_NSICR) ||
2536         (core->mac[IMS] && (core->mac[ICR] & E1000_ICR_INT_ASSERTED))) {
2537         igb_lower_interrupts(core, IMS, core->mac[IAM]);
2538     }
2539 }
2540 
2541 static void igb_set_icr(IGBCore *core, int index, uint32_t val)
2542 {
2543     igb_nsicr(core);
2544     igb_lower_interrupts(core, ICR, val);
2545 }
2546 
2547 static uint32_t
2548 igb_mac_readreg(IGBCore *core, int index)
2549 {
2550     return core->mac[index];
2551 }
2552 
2553 static uint32_t
2554 igb_mac_ics_read(IGBCore *core, int index)
2555 {
2556     trace_e1000e_irq_read_ics(core->mac[ICS]);
2557     return core->mac[ICS];
2558 }
2559 
2560 static uint32_t
2561 igb_mac_ims_read(IGBCore *core, int index)
2562 {
2563     trace_e1000e_irq_read_ims(core->mac[IMS]);
2564     return core->mac[IMS];
2565 }
2566 
2567 static uint32_t
2568 igb_mac_swsm_read(IGBCore *core, int index)
2569 {
2570     uint32_t val = core->mac[SWSM];
2571     core->mac[SWSM] = val | E1000_SWSM_SMBI;
2572     return val;
2573 }
2574 
2575 static uint32_t
2576 igb_mac_eitr_read(IGBCore *core, int index)
2577 {
2578     return core->eitr_guest_value[index - EITR0];
2579 }
2580 
2581 static uint32_t igb_mac_vfmailbox_read(IGBCore *core, int index)
2582 {
2583     uint32_t val = core->mac[index];
2584 
2585     core->mac[index] &= ~(E1000_V2PMAILBOX_PFSTS | E1000_V2PMAILBOX_PFACK |
2586                           E1000_V2PMAILBOX_RSTD);
2587 
2588     return val;
2589 }
2590 
2591 static uint32_t
2592 igb_mac_icr_read(IGBCore *core, int index)
2593 {
2594     uint32_t ret = core->mac[ICR];
2595 
2596     if (core->mac[GPIE] & E1000_GPIE_NSICR) {
2597         trace_igb_irq_icr_clear_gpie_nsicr();
2598         igb_lower_interrupts(core, ICR, 0xffffffff);
2599     } else if (core->mac[IMS] == 0) {
2600         trace_e1000e_irq_icr_clear_zero_ims();
2601         igb_lower_interrupts(core, ICR, 0xffffffff);
2602     } else if (core->mac[ICR] & E1000_ICR_INT_ASSERTED) {
2603         igb_lower_interrupts(core, ICR, 0xffffffff);
2604     } else if (!msix_enabled(core->owner)) {
2605         trace_e1000e_irq_icr_clear_nonmsix_icr_read();
2606         igb_lower_interrupts(core, ICR, 0xffffffff);
2607     }
2608 
2609     igb_nsicr(core);
2610     return ret;
2611 }
2612 
2613 static uint32_t
2614 igb_mac_read_clr4(IGBCore *core, int index)
2615 {
2616     uint32_t ret = core->mac[index];
2617 
2618     core->mac[index] = 0;
2619     return ret;
2620 }
2621 
2622 static uint32_t
2623 igb_mac_read_clr8(IGBCore *core, int index)
2624 {
2625     uint32_t ret = core->mac[index];
2626 
2627     core->mac[index] = 0;
2628     core->mac[index - 1] = 0;
2629     return ret;
2630 }
2631 
2632 static uint32_t
2633 igb_get_ctrl(IGBCore *core, int index)
2634 {
2635     uint32_t val = core->mac[CTRL];
2636 
2637     trace_e1000e_link_read_params(
2638         !!(val & E1000_CTRL_ASDE),
2639         (val & E1000_CTRL_SPD_SEL) >> E1000_CTRL_SPD_SHIFT,
2640         !!(val & E1000_CTRL_FRCSPD),
2641         !!(val & E1000_CTRL_FRCDPX),
2642         !!(val & E1000_CTRL_RFCE),
2643         !!(val & E1000_CTRL_TFCE));
2644 
2645     return val;
2646 }
2647 
2648 static uint32_t igb_get_status(IGBCore *core, int index)
2649 {
2650     uint32_t res = core->mac[STATUS];
2651     uint16_t num_vfs = pcie_sriov_num_vfs(core->owner);
2652 
2653     if (core->mac[CTRL] & E1000_CTRL_FRCDPX) {
2654         res |= (core->mac[CTRL] & E1000_CTRL_FD) ? E1000_STATUS_FD : 0;
2655     } else {
2656         res |= E1000_STATUS_FD;
2657     }
2658 
2659     if ((core->mac[CTRL] & E1000_CTRL_FRCSPD) ||
2660         (core->mac[CTRL_EXT] & E1000_CTRL_EXT_SPD_BYPS)) {
2661         switch (core->mac[CTRL] & E1000_CTRL_SPD_SEL) {
2662         case E1000_CTRL_SPD_10:
2663             res |= E1000_STATUS_SPEED_10;
2664             break;
2665         case E1000_CTRL_SPD_100:
2666             res |= E1000_STATUS_SPEED_100;
2667             break;
2668         case E1000_CTRL_SPD_1000:
2669         default:
2670             res |= E1000_STATUS_SPEED_1000;
2671             break;
2672         }
2673     } else {
2674         res |= E1000_STATUS_SPEED_1000;
2675     }
2676 
2677     if (num_vfs) {
2678         res |= num_vfs << E1000_STATUS_NUM_VFS_SHIFT;
2679         res |= E1000_STATUS_IOV_MODE;
2680     }
2681 
2682     if (!(core->mac[CTRL] & E1000_CTRL_GIO_MASTER_DISABLE)) {
2683         res |= E1000_STATUS_GIO_MASTER_ENABLE;
2684     }
2685 
2686     return res;
2687 }
2688 
2689 static void
2690 igb_mac_writereg(IGBCore *core, int index, uint32_t val)
2691 {
2692     core->mac[index] = val;
2693 }
2694 
2695 static void
2696 igb_mac_setmacaddr(IGBCore *core, int index, uint32_t val)
2697 {
2698     uint32_t macaddr[2];
2699 
2700     core->mac[index] = val;
2701 
2702     macaddr[0] = cpu_to_le32(core->mac[RA]);
2703     macaddr[1] = cpu_to_le32(core->mac[RA + 1]);
2704     qemu_format_nic_info_str(qemu_get_queue(core->owner_nic),
2705         (uint8_t *) macaddr);
2706 
2707     trace_e1000e_mac_set_sw(MAC_ARG(macaddr));
2708 }
2709 
2710 static void
2711 igb_set_eecd(IGBCore *core, int index, uint32_t val)
2712 {
2713     static const uint32_t ro_bits = E1000_EECD_PRES          |
2714                                     E1000_EECD_AUTO_RD       |
2715                                     E1000_EECD_SIZE_EX_MASK;
2716 
2717     core->mac[EECD] = (core->mac[EECD] & ro_bits) | (val & ~ro_bits);
2718 }
2719 
2720 static void
2721 igb_set_eerd(IGBCore *core, int index, uint32_t val)
2722 {
2723     uint32_t addr = (val >> E1000_EERW_ADDR_SHIFT) & E1000_EERW_ADDR_MASK;
2724     uint32_t flags = 0;
2725     uint32_t data = 0;
2726 
2727     if ((addr < IGB_EEPROM_SIZE) && (val & E1000_EERW_START)) {
2728         data = core->eeprom[addr];
2729         flags = E1000_EERW_DONE;
2730     }
2731 
2732     core->mac[EERD] = flags                           |
2733                       (addr << E1000_EERW_ADDR_SHIFT) |
2734                       (data << E1000_EERW_DATA_SHIFT);
2735 }
2736 
2737 static void
2738 igb_set_eitr(IGBCore *core, int index, uint32_t val)
2739 {
2740     uint32_t eitr_num = index - EITR0;
2741 
2742     trace_igb_irq_eitr_set(eitr_num, val);
2743 
2744     core->eitr_guest_value[eitr_num] = val & ~E1000_EITR_CNT_IGNR;
2745     core->mac[index] = val & 0x7FFE;
2746 }
2747 
2748 static void
2749 igb_update_rx_offloads(IGBCore *core)
2750 {
2751     int cso_state = igb_rx_l4_cso_enabled(core);
2752 
2753     trace_e1000e_rx_set_cso(cso_state);
2754 
2755     if (core->has_vnet) {
2756         qemu_set_offload(qemu_get_queue(core->owner_nic)->peer,
2757                          cso_state, 0, 0, 0, 0, 0, 0);
2758     }
2759 }
2760 
2761 static void
2762 igb_set_rxcsum(IGBCore *core, int index, uint32_t val)
2763 {
2764     core->mac[RXCSUM] = val;
2765     igb_update_rx_offloads(core);
2766 }
2767 
2768 static void
2769 igb_set_gcr(IGBCore *core, int index, uint32_t val)
2770 {
2771     uint32_t ro_bits = core->mac[GCR] & E1000_GCR_RO_BITS;
2772     core->mac[GCR] = (val & ~E1000_GCR_RO_BITS) | ro_bits;
2773 }
2774 
2775 static uint32_t igb_get_systiml(IGBCore *core, int index)
2776 {
2777     e1000x_timestamp(core->mac, core->timadj, SYSTIML, SYSTIMH);
2778     return core->mac[SYSTIML];
2779 }
2780 
2781 static uint32_t igb_get_rxsatrh(IGBCore *core, int index)
2782 {
2783     core->mac[TSYNCRXCTL] &= ~E1000_TSYNCRXCTL_VALID;
2784     return core->mac[RXSATRH];
2785 }
2786 
2787 static uint32_t igb_get_txstmph(IGBCore *core, int index)
2788 {
2789     core->mac[TSYNCTXCTL] &= ~E1000_TSYNCTXCTL_VALID;
2790     return core->mac[TXSTMPH];
2791 }
2792 
2793 static void igb_set_timinca(IGBCore *core, int index, uint32_t val)
2794 {
2795     e1000x_set_timinca(core->mac, &core->timadj, val);
2796 }
2797 
2798 static void igb_set_timadjh(IGBCore *core, int index, uint32_t val)
2799 {
2800     core->mac[TIMADJH] = val;
2801     core->timadj += core->mac[TIMADJL] | ((int64_t)core->mac[TIMADJH] << 32);
2802 }
2803 
2804 #define igb_getreg(x)    [x] = igb_mac_readreg
2805 typedef uint32_t (*readops)(IGBCore *, int);
2806 static const readops igb_macreg_readops[] = {
2807     igb_getreg(WUFC),
2808     igb_getreg(MANC),
2809     igb_getreg(TOTL),
2810     igb_getreg(RDT0),
2811     igb_getreg(RDT1),
2812     igb_getreg(RDT2),
2813     igb_getreg(RDT3),
2814     igb_getreg(RDT4),
2815     igb_getreg(RDT5),
2816     igb_getreg(RDT6),
2817     igb_getreg(RDT7),
2818     igb_getreg(RDT8),
2819     igb_getreg(RDT9),
2820     igb_getreg(RDT10),
2821     igb_getreg(RDT11),
2822     igb_getreg(RDT12),
2823     igb_getreg(RDT13),
2824     igb_getreg(RDT14),
2825     igb_getreg(RDT15),
2826     igb_getreg(RDBAH0),
2827     igb_getreg(RDBAH1),
2828     igb_getreg(RDBAH2),
2829     igb_getreg(RDBAH3),
2830     igb_getreg(RDBAH4),
2831     igb_getreg(RDBAH5),
2832     igb_getreg(RDBAH6),
2833     igb_getreg(RDBAH7),
2834     igb_getreg(RDBAH8),
2835     igb_getreg(RDBAH9),
2836     igb_getreg(RDBAH10),
2837     igb_getreg(RDBAH11),
2838     igb_getreg(RDBAH12),
2839     igb_getreg(RDBAH13),
2840     igb_getreg(RDBAH14),
2841     igb_getreg(RDBAH15),
2842     igb_getreg(TDBAL0),
2843     igb_getreg(TDBAL1),
2844     igb_getreg(TDBAL2),
2845     igb_getreg(TDBAL3),
2846     igb_getreg(TDBAL4),
2847     igb_getreg(TDBAL5),
2848     igb_getreg(TDBAL6),
2849     igb_getreg(TDBAL7),
2850     igb_getreg(TDBAL8),
2851     igb_getreg(TDBAL9),
2852     igb_getreg(TDBAL10),
2853     igb_getreg(TDBAL11),
2854     igb_getreg(TDBAL12),
2855     igb_getreg(TDBAL13),
2856     igb_getreg(TDBAL14),
2857     igb_getreg(TDBAL15),
2858     igb_getreg(RDLEN0),
2859     igb_getreg(RDLEN1),
2860     igb_getreg(RDLEN2),
2861     igb_getreg(RDLEN3),
2862     igb_getreg(RDLEN4),
2863     igb_getreg(RDLEN5),
2864     igb_getreg(RDLEN6),
2865     igb_getreg(RDLEN7),
2866     igb_getreg(RDLEN8),
2867     igb_getreg(RDLEN9),
2868     igb_getreg(RDLEN10),
2869     igb_getreg(RDLEN11),
2870     igb_getreg(RDLEN12),
2871     igb_getreg(RDLEN13),
2872     igb_getreg(RDLEN14),
2873     igb_getreg(RDLEN15),
2874     igb_getreg(SRRCTL0),
2875     igb_getreg(SRRCTL1),
2876     igb_getreg(SRRCTL2),
2877     igb_getreg(SRRCTL3),
2878     igb_getreg(SRRCTL4),
2879     igb_getreg(SRRCTL5),
2880     igb_getreg(SRRCTL6),
2881     igb_getreg(SRRCTL7),
2882     igb_getreg(SRRCTL8),
2883     igb_getreg(SRRCTL9),
2884     igb_getreg(SRRCTL10),
2885     igb_getreg(SRRCTL11),
2886     igb_getreg(SRRCTL12),
2887     igb_getreg(SRRCTL13),
2888     igb_getreg(SRRCTL14),
2889     igb_getreg(SRRCTL15),
2890     igb_getreg(LATECOL),
2891     igb_getreg(XONTXC),
2892     igb_getreg(TDFH),
2893     igb_getreg(TDFT),
2894     igb_getreg(TDFHS),
2895     igb_getreg(TDFTS),
2896     igb_getreg(TDFPC),
2897     igb_getreg(WUS),
2898     igb_getreg(RDFH),
2899     igb_getreg(RDFT),
2900     igb_getreg(RDFHS),
2901     igb_getreg(RDFTS),
2902     igb_getreg(RDFPC),
2903     igb_getreg(GORCL),
2904     igb_getreg(MGTPRC),
2905     igb_getreg(EERD),
2906     igb_getreg(EIAC),
2907     igb_getreg(MANC2H),
2908     igb_getreg(RXCSUM),
2909     igb_getreg(GSCL_3),
2910     igb_getreg(GSCN_2),
2911     igb_getreg(FCAH),
2912     igb_getreg(FCRTH),
2913     igb_getreg(FLOP),
2914     igb_getreg(RXSTMPH),
2915     igb_getreg(TXSTMPL),
2916     igb_getreg(TIMADJL),
2917     igb_getreg(RDH0),
2918     igb_getreg(RDH1),
2919     igb_getreg(RDH2),
2920     igb_getreg(RDH3),
2921     igb_getreg(RDH4),
2922     igb_getreg(RDH5),
2923     igb_getreg(RDH6),
2924     igb_getreg(RDH7),
2925     igb_getreg(RDH8),
2926     igb_getreg(RDH9),
2927     igb_getreg(RDH10),
2928     igb_getreg(RDH11),
2929     igb_getreg(RDH12),
2930     igb_getreg(RDH13),
2931     igb_getreg(RDH14),
2932     igb_getreg(RDH15),
2933     igb_getreg(TDT0),
2934     igb_getreg(TDT1),
2935     igb_getreg(TDT2),
2936     igb_getreg(TDT3),
2937     igb_getreg(TDT4),
2938     igb_getreg(TDT5),
2939     igb_getreg(TDT6),
2940     igb_getreg(TDT7),
2941     igb_getreg(TDT8),
2942     igb_getreg(TDT9),
2943     igb_getreg(TDT10),
2944     igb_getreg(TDT11),
2945     igb_getreg(TDT12),
2946     igb_getreg(TDT13),
2947     igb_getreg(TDT14),
2948     igb_getreg(TDT15),
2949     igb_getreg(TNCRS),
2950     igb_getreg(RJC),
2951     igb_getreg(IAM),
2952     igb_getreg(GSCL_2),
2953     igb_getreg(TIPG),
2954     igb_getreg(FLMNGCTL),
2955     igb_getreg(FLMNGCNT),
2956     igb_getreg(TSYNCTXCTL),
2957     igb_getreg(EEMNGDATA),
2958     igb_getreg(CTRL_EXT),
2959     igb_getreg(SYSTIMH),
2960     igb_getreg(EEMNGCTL),
2961     igb_getreg(FLMNGDATA),
2962     igb_getreg(TSYNCRXCTL),
2963     igb_getreg(LEDCTL),
2964     igb_getreg(TCTL),
2965     igb_getreg(TCTL_EXT),
2966     igb_getreg(DTXCTL),
2967     igb_getreg(RXPBS),
2968     igb_getreg(TDH0),
2969     igb_getreg(TDH1),
2970     igb_getreg(TDH2),
2971     igb_getreg(TDH3),
2972     igb_getreg(TDH4),
2973     igb_getreg(TDH5),
2974     igb_getreg(TDH6),
2975     igb_getreg(TDH7),
2976     igb_getreg(TDH8),
2977     igb_getreg(TDH9),
2978     igb_getreg(TDH10),
2979     igb_getreg(TDH11),
2980     igb_getreg(TDH12),
2981     igb_getreg(TDH13),
2982     igb_getreg(TDH14),
2983     igb_getreg(TDH15),
2984     igb_getreg(ECOL),
2985     igb_getreg(DC),
2986     igb_getreg(RLEC),
2987     igb_getreg(XOFFTXC),
2988     igb_getreg(RFC),
2989     igb_getreg(RNBC),
2990     igb_getreg(MGTPTC),
2991     igb_getreg(TIMINCA),
2992     igb_getreg(FACTPS),
2993     igb_getreg(GSCL_1),
2994     igb_getreg(GSCN_0),
2995     igb_getreg(PBACLR),
2996     igb_getreg(FCTTV),
2997     igb_getreg(RXSATRL),
2998     igb_getreg(TORL),
2999     igb_getreg(TDLEN0),
3000     igb_getreg(TDLEN1),
3001     igb_getreg(TDLEN2),
3002     igb_getreg(TDLEN3),
3003     igb_getreg(TDLEN4),
3004     igb_getreg(TDLEN5),
3005     igb_getreg(TDLEN6),
3006     igb_getreg(TDLEN7),
3007     igb_getreg(TDLEN8),
3008     igb_getreg(TDLEN9),
3009     igb_getreg(TDLEN10),
3010     igb_getreg(TDLEN11),
3011     igb_getreg(TDLEN12),
3012     igb_getreg(TDLEN13),
3013     igb_getreg(TDLEN14),
3014     igb_getreg(TDLEN15),
3015     igb_getreg(MCC),
3016     igb_getreg(WUC),
3017     igb_getreg(EECD),
3018     igb_getreg(FCRTV),
3019     igb_getreg(TXDCTL0),
3020     igb_getreg(TXDCTL1),
3021     igb_getreg(TXDCTL2),
3022     igb_getreg(TXDCTL3),
3023     igb_getreg(TXDCTL4),
3024     igb_getreg(TXDCTL5),
3025     igb_getreg(TXDCTL6),
3026     igb_getreg(TXDCTL7),
3027     igb_getreg(TXDCTL8),
3028     igb_getreg(TXDCTL9),
3029     igb_getreg(TXDCTL10),
3030     igb_getreg(TXDCTL11),
3031     igb_getreg(TXDCTL12),
3032     igb_getreg(TXDCTL13),
3033     igb_getreg(TXDCTL14),
3034     igb_getreg(TXDCTL15),
3035     igb_getreg(TXCTL0),
3036     igb_getreg(TXCTL1),
3037     igb_getreg(TXCTL2),
3038     igb_getreg(TXCTL3),
3039     igb_getreg(TXCTL4),
3040     igb_getreg(TXCTL5),
3041     igb_getreg(TXCTL6),
3042     igb_getreg(TXCTL7),
3043     igb_getreg(TXCTL8),
3044     igb_getreg(TXCTL9),
3045     igb_getreg(TXCTL10),
3046     igb_getreg(TXCTL11),
3047     igb_getreg(TXCTL12),
3048     igb_getreg(TXCTL13),
3049     igb_getreg(TXCTL14),
3050     igb_getreg(TXCTL15),
3051     igb_getreg(TDWBAL0),
3052     igb_getreg(TDWBAL1),
3053     igb_getreg(TDWBAL2),
3054     igb_getreg(TDWBAL3),
3055     igb_getreg(TDWBAL4),
3056     igb_getreg(TDWBAL5),
3057     igb_getreg(TDWBAL6),
3058     igb_getreg(TDWBAL7),
3059     igb_getreg(TDWBAL8),
3060     igb_getreg(TDWBAL9),
3061     igb_getreg(TDWBAL10),
3062     igb_getreg(TDWBAL11),
3063     igb_getreg(TDWBAL12),
3064     igb_getreg(TDWBAL13),
3065     igb_getreg(TDWBAL14),
3066     igb_getreg(TDWBAL15),
3067     igb_getreg(TDWBAH0),
3068     igb_getreg(TDWBAH1),
3069     igb_getreg(TDWBAH2),
3070     igb_getreg(TDWBAH3),
3071     igb_getreg(TDWBAH4),
3072     igb_getreg(TDWBAH5),
3073     igb_getreg(TDWBAH6),
3074     igb_getreg(TDWBAH7),
3075     igb_getreg(TDWBAH8),
3076     igb_getreg(TDWBAH9),
3077     igb_getreg(TDWBAH10),
3078     igb_getreg(TDWBAH11),
3079     igb_getreg(TDWBAH12),
3080     igb_getreg(TDWBAH13),
3081     igb_getreg(TDWBAH14),
3082     igb_getreg(TDWBAH15),
3083     igb_getreg(PVTCTRL0),
3084     igb_getreg(PVTCTRL1),
3085     igb_getreg(PVTCTRL2),
3086     igb_getreg(PVTCTRL3),
3087     igb_getreg(PVTCTRL4),
3088     igb_getreg(PVTCTRL5),
3089     igb_getreg(PVTCTRL6),
3090     igb_getreg(PVTCTRL7),
3091     igb_getreg(PVTEIMS0),
3092     igb_getreg(PVTEIMS1),
3093     igb_getreg(PVTEIMS2),
3094     igb_getreg(PVTEIMS3),
3095     igb_getreg(PVTEIMS4),
3096     igb_getreg(PVTEIMS5),
3097     igb_getreg(PVTEIMS6),
3098     igb_getreg(PVTEIMS7),
3099     igb_getreg(PVTEIAC0),
3100     igb_getreg(PVTEIAC1),
3101     igb_getreg(PVTEIAC2),
3102     igb_getreg(PVTEIAC3),
3103     igb_getreg(PVTEIAC4),
3104     igb_getreg(PVTEIAC5),
3105     igb_getreg(PVTEIAC6),
3106     igb_getreg(PVTEIAC7),
3107     igb_getreg(PVTEIAM0),
3108     igb_getreg(PVTEIAM1),
3109     igb_getreg(PVTEIAM2),
3110     igb_getreg(PVTEIAM3),
3111     igb_getreg(PVTEIAM4),
3112     igb_getreg(PVTEIAM5),
3113     igb_getreg(PVTEIAM6),
3114     igb_getreg(PVTEIAM7),
3115     igb_getreg(PVFGPRC0),
3116     igb_getreg(PVFGPRC1),
3117     igb_getreg(PVFGPRC2),
3118     igb_getreg(PVFGPRC3),
3119     igb_getreg(PVFGPRC4),
3120     igb_getreg(PVFGPRC5),
3121     igb_getreg(PVFGPRC6),
3122     igb_getreg(PVFGPRC7),
3123     igb_getreg(PVFGPTC0),
3124     igb_getreg(PVFGPTC1),
3125     igb_getreg(PVFGPTC2),
3126     igb_getreg(PVFGPTC3),
3127     igb_getreg(PVFGPTC4),
3128     igb_getreg(PVFGPTC5),
3129     igb_getreg(PVFGPTC6),
3130     igb_getreg(PVFGPTC7),
3131     igb_getreg(PVFGORC0),
3132     igb_getreg(PVFGORC1),
3133     igb_getreg(PVFGORC2),
3134     igb_getreg(PVFGORC3),
3135     igb_getreg(PVFGORC4),
3136     igb_getreg(PVFGORC5),
3137     igb_getreg(PVFGORC6),
3138     igb_getreg(PVFGORC7),
3139     igb_getreg(PVFGOTC0),
3140     igb_getreg(PVFGOTC1),
3141     igb_getreg(PVFGOTC2),
3142     igb_getreg(PVFGOTC3),
3143     igb_getreg(PVFGOTC4),
3144     igb_getreg(PVFGOTC5),
3145     igb_getreg(PVFGOTC6),
3146     igb_getreg(PVFGOTC7),
3147     igb_getreg(PVFMPRC0),
3148     igb_getreg(PVFMPRC1),
3149     igb_getreg(PVFMPRC2),
3150     igb_getreg(PVFMPRC3),
3151     igb_getreg(PVFMPRC4),
3152     igb_getreg(PVFMPRC5),
3153     igb_getreg(PVFMPRC6),
3154     igb_getreg(PVFMPRC7),
3155     igb_getreg(PVFGPRLBC0),
3156     igb_getreg(PVFGPRLBC1),
3157     igb_getreg(PVFGPRLBC2),
3158     igb_getreg(PVFGPRLBC3),
3159     igb_getreg(PVFGPRLBC4),
3160     igb_getreg(PVFGPRLBC5),
3161     igb_getreg(PVFGPRLBC6),
3162     igb_getreg(PVFGPRLBC7),
3163     igb_getreg(PVFGPTLBC0),
3164     igb_getreg(PVFGPTLBC1),
3165     igb_getreg(PVFGPTLBC2),
3166     igb_getreg(PVFGPTLBC3),
3167     igb_getreg(PVFGPTLBC4),
3168     igb_getreg(PVFGPTLBC5),
3169     igb_getreg(PVFGPTLBC6),
3170     igb_getreg(PVFGPTLBC7),
3171     igb_getreg(PVFGORLBC0),
3172     igb_getreg(PVFGORLBC1),
3173     igb_getreg(PVFGORLBC2),
3174     igb_getreg(PVFGORLBC3),
3175     igb_getreg(PVFGORLBC4),
3176     igb_getreg(PVFGORLBC5),
3177     igb_getreg(PVFGORLBC6),
3178     igb_getreg(PVFGORLBC7),
3179     igb_getreg(PVFGOTLBC0),
3180     igb_getreg(PVFGOTLBC1),
3181     igb_getreg(PVFGOTLBC2),
3182     igb_getreg(PVFGOTLBC3),
3183     igb_getreg(PVFGOTLBC4),
3184     igb_getreg(PVFGOTLBC5),
3185     igb_getreg(PVFGOTLBC6),
3186     igb_getreg(PVFGOTLBC7),
3187     igb_getreg(RCTL),
3188     igb_getreg(MDIC),
3189     igb_getreg(FCRUC),
3190     igb_getreg(VET),
3191     igb_getreg(RDBAL0),
3192     igb_getreg(RDBAL1),
3193     igb_getreg(RDBAL2),
3194     igb_getreg(RDBAL3),
3195     igb_getreg(RDBAL4),
3196     igb_getreg(RDBAL5),
3197     igb_getreg(RDBAL6),
3198     igb_getreg(RDBAL7),
3199     igb_getreg(RDBAL8),
3200     igb_getreg(RDBAL9),
3201     igb_getreg(RDBAL10),
3202     igb_getreg(RDBAL11),
3203     igb_getreg(RDBAL12),
3204     igb_getreg(RDBAL13),
3205     igb_getreg(RDBAL14),
3206     igb_getreg(RDBAL15),
3207     igb_getreg(TDBAH0),
3208     igb_getreg(TDBAH1),
3209     igb_getreg(TDBAH2),
3210     igb_getreg(TDBAH3),
3211     igb_getreg(TDBAH4),
3212     igb_getreg(TDBAH5),
3213     igb_getreg(TDBAH6),
3214     igb_getreg(TDBAH7),
3215     igb_getreg(TDBAH8),
3216     igb_getreg(TDBAH9),
3217     igb_getreg(TDBAH10),
3218     igb_getreg(TDBAH11),
3219     igb_getreg(TDBAH12),
3220     igb_getreg(TDBAH13),
3221     igb_getreg(TDBAH14),
3222     igb_getreg(TDBAH15),
3223     igb_getreg(SCC),
3224     igb_getreg(COLC),
3225     igb_getreg(XOFFRXC),
3226     igb_getreg(IPAV),
3227     igb_getreg(GOTCL),
3228     igb_getreg(MGTPDC),
3229     igb_getreg(GCR),
3230     igb_getreg(MFVAL),
3231     igb_getreg(FUNCTAG),
3232     igb_getreg(GSCL_4),
3233     igb_getreg(GSCN_3),
3234     igb_getreg(MRQC),
3235     igb_getreg(FCT),
3236     igb_getreg(FLA),
3237     igb_getreg(RXDCTL0),
3238     igb_getreg(RXDCTL1),
3239     igb_getreg(RXDCTL2),
3240     igb_getreg(RXDCTL3),
3241     igb_getreg(RXDCTL4),
3242     igb_getreg(RXDCTL5),
3243     igb_getreg(RXDCTL6),
3244     igb_getreg(RXDCTL7),
3245     igb_getreg(RXDCTL8),
3246     igb_getreg(RXDCTL9),
3247     igb_getreg(RXDCTL10),
3248     igb_getreg(RXDCTL11),
3249     igb_getreg(RXDCTL12),
3250     igb_getreg(RXDCTL13),
3251     igb_getreg(RXDCTL14),
3252     igb_getreg(RXDCTL15),
3253     igb_getreg(RXSTMPL),
3254     igb_getreg(TIMADJH),
3255     igb_getreg(FCRTL),
3256     igb_getreg(XONRXC),
3257     igb_getreg(RFCTL),
3258     igb_getreg(GSCN_1),
3259     igb_getreg(FCAL),
3260     igb_getreg(GPIE),
3261     igb_getreg(TXPBS),
3262     igb_getreg(RLPML),
3263 
3264     [TOTH]    = igb_mac_read_clr8,
3265     [GOTCH]   = igb_mac_read_clr8,
3266     [PRC64]   = igb_mac_read_clr4,
3267     [PRC255]  = igb_mac_read_clr4,
3268     [PRC1023] = igb_mac_read_clr4,
3269     [PTC64]   = igb_mac_read_clr4,
3270     [PTC255]  = igb_mac_read_clr4,
3271     [PTC1023] = igb_mac_read_clr4,
3272     [GPRC]    = igb_mac_read_clr4,
3273     [TPT]     = igb_mac_read_clr4,
3274     [RUC]     = igb_mac_read_clr4,
3275     [BPRC]    = igb_mac_read_clr4,
3276     [MPTC]    = igb_mac_read_clr4,
3277     [IAC]     = igb_mac_read_clr4,
3278     [ICR]     = igb_mac_icr_read,
3279     [STATUS]  = igb_get_status,
3280     [ICS]     = igb_mac_ics_read,
3281     /*
3282      * 8.8.10: Reading the IMC register returns the value of the IMS register.
3283      */
3284     [IMC]     = igb_mac_ims_read,
3285     [TORH]    = igb_mac_read_clr8,
3286     [GORCH]   = igb_mac_read_clr8,
3287     [PRC127]  = igb_mac_read_clr4,
3288     [PRC511]  = igb_mac_read_clr4,
3289     [PRC1522] = igb_mac_read_clr4,
3290     [PTC127]  = igb_mac_read_clr4,
3291     [PTC511]  = igb_mac_read_clr4,
3292     [PTC1522] = igb_mac_read_clr4,
3293     [GPTC]    = igb_mac_read_clr4,
3294     [TPR]     = igb_mac_read_clr4,
3295     [ROC]     = igb_mac_read_clr4,
3296     [MPRC]    = igb_mac_read_clr4,
3297     [BPTC]    = igb_mac_read_clr4,
3298     [TSCTC]   = igb_mac_read_clr4,
3299     [CTRL]    = igb_get_ctrl,
3300     [SWSM]    = igb_mac_swsm_read,
3301     [IMS]     = igb_mac_ims_read,
3302     [SYSTIML] = igb_get_systiml,
3303     [RXSATRH] = igb_get_rxsatrh,
3304     [TXSTMPH] = igb_get_txstmph,
3305 
3306     [CRCERRS ... MPC]      = igb_mac_readreg,
3307     [IP6AT ... IP6AT + 3]  = igb_mac_readreg,
3308     [IP4AT ... IP4AT + 6]  = igb_mac_readreg,
3309     [RA ... RA + 31]       = igb_mac_readreg,
3310     [RA2 ... RA2 + 31]     = igb_mac_readreg,
3311     [WUPM ... WUPM + 31]   = igb_mac_readreg,
3312     [MTA ... MTA + E1000_MC_TBL_SIZE - 1]    = igb_mac_readreg,
3313     [VFTA ... VFTA + E1000_VLAN_FILTER_TBL_SIZE - 1]  = igb_mac_readreg,
3314     [FFMT ... FFMT + 254]  = igb_mac_readreg,
3315     [MDEF ... MDEF + 7]    = igb_mac_readreg,
3316     [FTFT ... FTFT + 254]  = igb_mac_readreg,
3317     [RETA ... RETA + 31]   = igb_mac_readreg,
3318     [RSSRK ... RSSRK + 9]  = igb_mac_readreg,
3319     [MAVTV0 ... MAVTV3]    = igb_mac_readreg,
3320     [EITR0 ... EITR0 + IGB_INTR_NUM - 1] = igb_mac_eitr_read,
3321     [PVTEICR0] = igb_mac_read_clr4,
3322     [PVTEICR1] = igb_mac_read_clr4,
3323     [PVTEICR2] = igb_mac_read_clr4,
3324     [PVTEICR3] = igb_mac_read_clr4,
3325     [PVTEICR4] = igb_mac_read_clr4,
3326     [PVTEICR5] = igb_mac_read_clr4,
3327     [PVTEICR6] = igb_mac_read_clr4,
3328     [PVTEICR7] = igb_mac_read_clr4,
3329 
3330     /* IGB specific: */
3331     [FWSM]       = igb_mac_readreg,
3332     [SW_FW_SYNC] = igb_mac_readreg,
3333     [HTCBDPC]    = igb_mac_read_clr4,
3334     [EICR]       = igb_mac_read_clr4,
3335     [EIMS]       = igb_mac_readreg,
3336     [EIAM]       = igb_mac_readreg,
3337     [IVAR0 ... IVAR0 + 7] = igb_mac_readreg,
3338     igb_getreg(IVAR_MISC),
3339     igb_getreg(TSYNCRXCFG),
3340     [ETQF0 ... ETQF0 + 7] = igb_mac_readreg,
3341     igb_getreg(VT_CTL),
3342     [P2VMAILBOX0 ... P2VMAILBOX7] = igb_mac_readreg,
3343     [V2PMAILBOX0 ... V2PMAILBOX7] = igb_mac_vfmailbox_read,
3344     igb_getreg(MBVFICR),
3345     [VMBMEM0 ... VMBMEM0 + 127] = igb_mac_readreg,
3346     igb_getreg(MBVFIMR),
3347     igb_getreg(VFLRE),
3348     igb_getreg(VFRE),
3349     igb_getreg(VFTE),
3350     igb_getreg(QDE),
3351     igb_getreg(DTXSWC),
3352     igb_getreg(RPLOLR),
3353     [VLVF0 ... VLVF0 + E1000_VLVF_ARRAY_SIZE - 1] = igb_mac_readreg,
3354     [VMVIR0 ... VMVIR7] = igb_mac_readreg,
3355     [VMOLR0 ... VMOLR7] = igb_mac_readreg,
3356     [WVBR] = igb_mac_read_clr4,
3357     [RQDPC0] = igb_mac_read_clr4,
3358     [RQDPC1] = igb_mac_read_clr4,
3359     [RQDPC2] = igb_mac_read_clr4,
3360     [RQDPC3] = igb_mac_read_clr4,
3361     [RQDPC4] = igb_mac_read_clr4,
3362     [RQDPC5] = igb_mac_read_clr4,
3363     [RQDPC6] = igb_mac_read_clr4,
3364     [RQDPC7] = igb_mac_read_clr4,
3365     [RQDPC8] = igb_mac_read_clr4,
3366     [RQDPC9] = igb_mac_read_clr4,
3367     [RQDPC10] = igb_mac_read_clr4,
3368     [RQDPC11] = igb_mac_read_clr4,
3369     [RQDPC12] = igb_mac_read_clr4,
3370     [RQDPC13] = igb_mac_read_clr4,
3371     [RQDPC14] = igb_mac_read_clr4,
3372     [RQDPC15] = igb_mac_read_clr4,
3373     [VTIVAR ... VTIVAR + 7] = igb_mac_readreg,
3374     [VTIVAR_MISC ... VTIVAR_MISC + 7] = igb_mac_readreg,
3375 };
3376 enum { IGB_NREADOPS = ARRAY_SIZE(igb_macreg_readops) };
3377 
3378 #define igb_putreg(x)    [x] = igb_mac_writereg
3379 typedef void (*writeops)(IGBCore *, int, uint32_t);
3380 static const writeops igb_macreg_writeops[] = {
3381     igb_putreg(SWSM),
3382     igb_putreg(WUFC),
3383     igb_putreg(RDBAH0),
3384     igb_putreg(RDBAH1),
3385     igb_putreg(RDBAH2),
3386     igb_putreg(RDBAH3),
3387     igb_putreg(RDBAH4),
3388     igb_putreg(RDBAH5),
3389     igb_putreg(RDBAH6),
3390     igb_putreg(RDBAH7),
3391     igb_putreg(RDBAH8),
3392     igb_putreg(RDBAH9),
3393     igb_putreg(RDBAH10),
3394     igb_putreg(RDBAH11),
3395     igb_putreg(RDBAH12),
3396     igb_putreg(RDBAH13),
3397     igb_putreg(RDBAH14),
3398     igb_putreg(RDBAH15),
3399     igb_putreg(SRRCTL0),
3400     igb_putreg(SRRCTL1),
3401     igb_putreg(SRRCTL2),
3402     igb_putreg(SRRCTL3),
3403     igb_putreg(SRRCTL4),
3404     igb_putreg(SRRCTL5),
3405     igb_putreg(SRRCTL6),
3406     igb_putreg(SRRCTL7),
3407     igb_putreg(SRRCTL8),
3408     igb_putreg(SRRCTL9),
3409     igb_putreg(SRRCTL10),
3410     igb_putreg(SRRCTL11),
3411     igb_putreg(SRRCTL12),
3412     igb_putreg(SRRCTL13),
3413     igb_putreg(SRRCTL14),
3414     igb_putreg(SRRCTL15),
3415     igb_putreg(RXDCTL0),
3416     igb_putreg(RXDCTL1),
3417     igb_putreg(RXDCTL2),
3418     igb_putreg(RXDCTL3),
3419     igb_putreg(RXDCTL4),
3420     igb_putreg(RXDCTL5),
3421     igb_putreg(RXDCTL6),
3422     igb_putreg(RXDCTL7),
3423     igb_putreg(RXDCTL8),
3424     igb_putreg(RXDCTL9),
3425     igb_putreg(RXDCTL10),
3426     igb_putreg(RXDCTL11),
3427     igb_putreg(RXDCTL12),
3428     igb_putreg(RXDCTL13),
3429     igb_putreg(RXDCTL14),
3430     igb_putreg(RXDCTL15),
3431     igb_putreg(LEDCTL),
3432     igb_putreg(TCTL),
3433     igb_putreg(TCTL_EXT),
3434     igb_putreg(DTXCTL),
3435     igb_putreg(RXPBS),
3436     igb_putreg(RQDPC0),
3437     igb_putreg(FCAL),
3438     igb_putreg(FCRUC),
3439     igb_putreg(WUC),
3440     igb_putreg(WUS),
3441     igb_putreg(IPAV),
3442     igb_putreg(TDBAH0),
3443     igb_putreg(TDBAH1),
3444     igb_putreg(TDBAH2),
3445     igb_putreg(TDBAH3),
3446     igb_putreg(TDBAH4),
3447     igb_putreg(TDBAH5),
3448     igb_putreg(TDBAH6),
3449     igb_putreg(TDBAH7),
3450     igb_putreg(TDBAH8),
3451     igb_putreg(TDBAH9),
3452     igb_putreg(TDBAH10),
3453     igb_putreg(TDBAH11),
3454     igb_putreg(TDBAH12),
3455     igb_putreg(TDBAH13),
3456     igb_putreg(TDBAH14),
3457     igb_putreg(TDBAH15),
3458     igb_putreg(IAM),
3459     igb_putreg(MANC),
3460     igb_putreg(MANC2H),
3461     igb_putreg(MFVAL),
3462     igb_putreg(FACTPS),
3463     igb_putreg(FUNCTAG),
3464     igb_putreg(GSCL_1),
3465     igb_putreg(GSCL_2),
3466     igb_putreg(GSCL_3),
3467     igb_putreg(GSCL_4),
3468     igb_putreg(GSCN_0),
3469     igb_putreg(GSCN_1),
3470     igb_putreg(GSCN_2),
3471     igb_putreg(GSCN_3),
3472     igb_putreg(MRQC),
3473     igb_putreg(FLOP),
3474     igb_putreg(FLA),
3475     igb_putreg(TXDCTL0),
3476     igb_putreg(TXDCTL1),
3477     igb_putreg(TXDCTL2),
3478     igb_putreg(TXDCTL3),
3479     igb_putreg(TXDCTL4),
3480     igb_putreg(TXDCTL5),
3481     igb_putreg(TXDCTL6),
3482     igb_putreg(TXDCTL7),
3483     igb_putreg(TXDCTL8),
3484     igb_putreg(TXDCTL9),
3485     igb_putreg(TXDCTL10),
3486     igb_putreg(TXDCTL11),
3487     igb_putreg(TXDCTL12),
3488     igb_putreg(TXDCTL13),
3489     igb_putreg(TXDCTL14),
3490     igb_putreg(TXDCTL15),
3491     igb_putreg(TXCTL0),
3492     igb_putreg(TXCTL1),
3493     igb_putreg(TXCTL2),
3494     igb_putreg(TXCTL3),
3495     igb_putreg(TXCTL4),
3496     igb_putreg(TXCTL5),
3497     igb_putreg(TXCTL6),
3498     igb_putreg(TXCTL7),
3499     igb_putreg(TXCTL8),
3500     igb_putreg(TXCTL9),
3501     igb_putreg(TXCTL10),
3502     igb_putreg(TXCTL11),
3503     igb_putreg(TXCTL12),
3504     igb_putreg(TXCTL13),
3505     igb_putreg(TXCTL14),
3506     igb_putreg(TXCTL15),
3507     igb_putreg(TDWBAL0),
3508     igb_putreg(TDWBAL1),
3509     igb_putreg(TDWBAL2),
3510     igb_putreg(TDWBAL3),
3511     igb_putreg(TDWBAL4),
3512     igb_putreg(TDWBAL5),
3513     igb_putreg(TDWBAL6),
3514     igb_putreg(TDWBAL7),
3515     igb_putreg(TDWBAL8),
3516     igb_putreg(TDWBAL9),
3517     igb_putreg(TDWBAL10),
3518     igb_putreg(TDWBAL11),
3519     igb_putreg(TDWBAL12),
3520     igb_putreg(TDWBAL13),
3521     igb_putreg(TDWBAL14),
3522     igb_putreg(TDWBAL15),
3523     igb_putreg(TDWBAH0),
3524     igb_putreg(TDWBAH1),
3525     igb_putreg(TDWBAH2),
3526     igb_putreg(TDWBAH3),
3527     igb_putreg(TDWBAH4),
3528     igb_putreg(TDWBAH5),
3529     igb_putreg(TDWBAH6),
3530     igb_putreg(TDWBAH7),
3531     igb_putreg(TDWBAH8),
3532     igb_putreg(TDWBAH9),
3533     igb_putreg(TDWBAH10),
3534     igb_putreg(TDWBAH11),
3535     igb_putreg(TDWBAH12),
3536     igb_putreg(TDWBAH13),
3537     igb_putreg(TDWBAH14),
3538     igb_putreg(TDWBAH15),
3539     igb_putreg(TIPG),
3540     igb_putreg(RXSTMPH),
3541     igb_putreg(RXSTMPL),
3542     igb_putreg(RXSATRL),
3543     igb_putreg(RXSATRH),
3544     igb_putreg(TXSTMPL),
3545     igb_putreg(TXSTMPH),
3546     igb_putreg(SYSTIML),
3547     igb_putreg(SYSTIMH),
3548     igb_putreg(TIMADJL),
3549     igb_putreg(TSYNCRXCTL),
3550     igb_putreg(TSYNCTXCTL),
3551     igb_putreg(EEMNGCTL),
3552     igb_putreg(GPIE),
3553     igb_putreg(TXPBS),
3554     igb_putreg(RLPML),
3555     igb_putreg(VET),
3556 
3557     [TDH0]     = igb_set_16bit,
3558     [TDH1]     = igb_set_16bit,
3559     [TDH2]     = igb_set_16bit,
3560     [TDH3]     = igb_set_16bit,
3561     [TDH4]     = igb_set_16bit,
3562     [TDH5]     = igb_set_16bit,
3563     [TDH6]     = igb_set_16bit,
3564     [TDH7]     = igb_set_16bit,
3565     [TDH8]     = igb_set_16bit,
3566     [TDH9]     = igb_set_16bit,
3567     [TDH10]    = igb_set_16bit,
3568     [TDH11]    = igb_set_16bit,
3569     [TDH12]    = igb_set_16bit,
3570     [TDH13]    = igb_set_16bit,
3571     [TDH14]    = igb_set_16bit,
3572     [TDH15]    = igb_set_16bit,
3573     [TDT0]     = igb_set_tdt,
3574     [TDT1]     = igb_set_tdt,
3575     [TDT2]     = igb_set_tdt,
3576     [TDT3]     = igb_set_tdt,
3577     [TDT4]     = igb_set_tdt,
3578     [TDT5]     = igb_set_tdt,
3579     [TDT6]     = igb_set_tdt,
3580     [TDT7]     = igb_set_tdt,
3581     [TDT8]     = igb_set_tdt,
3582     [TDT9]     = igb_set_tdt,
3583     [TDT10]    = igb_set_tdt,
3584     [TDT11]    = igb_set_tdt,
3585     [TDT12]    = igb_set_tdt,
3586     [TDT13]    = igb_set_tdt,
3587     [TDT14]    = igb_set_tdt,
3588     [TDT15]    = igb_set_tdt,
3589     [MDIC]     = igb_set_mdic,
3590     [ICS]      = igb_set_ics,
3591     [RDH0]     = igb_set_16bit,
3592     [RDH1]     = igb_set_16bit,
3593     [RDH2]     = igb_set_16bit,
3594     [RDH3]     = igb_set_16bit,
3595     [RDH4]     = igb_set_16bit,
3596     [RDH5]     = igb_set_16bit,
3597     [RDH6]     = igb_set_16bit,
3598     [RDH7]     = igb_set_16bit,
3599     [RDH8]     = igb_set_16bit,
3600     [RDH9]     = igb_set_16bit,
3601     [RDH10]    = igb_set_16bit,
3602     [RDH11]    = igb_set_16bit,
3603     [RDH12]    = igb_set_16bit,
3604     [RDH13]    = igb_set_16bit,
3605     [RDH14]    = igb_set_16bit,
3606     [RDH15]    = igb_set_16bit,
3607     [RDT0]     = igb_set_rdt,
3608     [RDT1]     = igb_set_rdt,
3609     [RDT2]     = igb_set_rdt,
3610     [RDT3]     = igb_set_rdt,
3611     [RDT4]     = igb_set_rdt,
3612     [RDT5]     = igb_set_rdt,
3613     [RDT6]     = igb_set_rdt,
3614     [RDT7]     = igb_set_rdt,
3615     [RDT8]     = igb_set_rdt,
3616     [RDT9]     = igb_set_rdt,
3617     [RDT10]    = igb_set_rdt,
3618     [RDT11]    = igb_set_rdt,
3619     [RDT12]    = igb_set_rdt,
3620     [RDT13]    = igb_set_rdt,
3621     [RDT14]    = igb_set_rdt,
3622     [RDT15]    = igb_set_rdt,
3623     [IMC]      = igb_set_imc,
3624     [IMS]      = igb_set_ims,
3625     [ICR]      = igb_set_icr,
3626     [EECD]     = igb_set_eecd,
3627     [RCTL]     = igb_set_rx_control,
3628     [CTRL]     = igb_set_ctrl,
3629     [EERD]     = igb_set_eerd,
3630     [TDFH]     = igb_set_13bit,
3631     [TDFT]     = igb_set_13bit,
3632     [TDFHS]    = igb_set_13bit,
3633     [TDFTS]    = igb_set_13bit,
3634     [TDFPC]    = igb_set_13bit,
3635     [RDFH]     = igb_set_13bit,
3636     [RDFT]     = igb_set_13bit,
3637     [RDFHS]    = igb_set_13bit,
3638     [RDFTS]    = igb_set_13bit,
3639     [RDFPC]    = igb_set_13bit,
3640     [GCR]      = igb_set_gcr,
3641     [RXCSUM]   = igb_set_rxcsum,
3642     [TDLEN0]   = igb_set_dlen,
3643     [TDLEN1]   = igb_set_dlen,
3644     [TDLEN2]   = igb_set_dlen,
3645     [TDLEN3]   = igb_set_dlen,
3646     [TDLEN4]   = igb_set_dlen,
3647     [TDLEN5]   = igb_set_dlen,
3648     [TDLEN6]   = igb_set_dlen,
3649     [TDLEN7]   = igb_set_dlen,
3650     [TDLEN8]   = igb_set_dlen,
3651     [TDLEN9]   = igb_set_dlen,
3652     [TDLEN10]  = igb_set_dlen,
3653     [TDLEN11]  = igb_set_dlen,
3654     [TDLEN12]  = igb_set_dlen,
3655     [TDLEN13]  = igb_set_dlen,
3656     [TDLEN14]  = igb_set_dlen,
3657     [TDLEN15]  = igb_set_dlen,
3658     [RDLEN0]   = igb_set_dlen,
3659     [RDLEN1]   = igb_set_dlen,
3660     [RDLEN2]   = igb_set_dlen,
3661     [RDLEN3]   = igb_set_dlen,
3662     [RDLEN4]   = igb_set_dlen,
3663     [RDLEN5]   = igb_set_dlen,
3664     [RDLEN6]   = igb_set_dlen,
3665     [RDLEN7]   = igb_set_dlen,
3666     [RDLEN8]   = igb_set_dlen,
3667     [RDLEN9]   = igb_set_dlen,
3668     [RDLEN10]  = igb_set_dlen,
3669     [RDLEN11]  = igb_set_dlen,
3670     [RDLEN12]  = igb_set_dlen,
3671     [RDLEN13]  = igb_set_dlen,
3672     [RDLEN14]  = igb_set_dlen,
3673     [RDLEN15]  = igb_set_dlen,
3674     [TDBAL0]   = igb_set_dbal,
3675     [TDBAL1]   = igb_set_dbal,
3676     [TDBAL2]   = igb_set_dbal,
3677     [TDBAL3]   = igb_set_dbal,
3678     [TDBAL4]   = igb_set_dbal,
3679     [TDBAL5]   = igb_set_dbal,
3680     [TDBAL6]   = igb_set_dbal,
3681     [TDBAL7]   = igb_set_dbal,
3682     [TDBAL8]   = igb_set_dbal,
3683     [TDBAL9]   = igb_set_dbal,
3684     [TDBAL10]  = igb_set_dbal,
3685     [TDBAL11]  = igb_set_dbal,
3686     [TDBAL12]  = igb_set_dbal,
3687     [TDBAL13]  = igb_set_dbal,
3688     [TDBAL14]  = igb_set_dbal,
3689     [TDBAL15]  = igb_set_dbal,
3690     [RDBAL0]   = igb_set_dbal,
3691     [RDBAL1]   = igb_set_dbal,
3692     [RDBAL2]   = igb_set_dbal,
3693     [RDBAL3]   = igb_set_dbal,
3694     [RDBAL4]   = igb_set_dbal,
3695     [RDBAL5]   = igb_set_dbal,
3696     [RDBAL6]   = igb_set_dbal,
3697     [RDBAL7]   = igb_set_dbal,
3698     [RDBAL8]   = igb_set_dbal,
3699     [RDBAL9]   = igb_set_dbal,
3700     [RDBAL10]  = igb_set_dbal,
3701     [RDBAL11]  = igb_set_dbal,
3702     [RDBAL12]  = igb_set_dbal,
3703     [RDBAL13]  = igb_set_dbal,
3704     [RDBAL14]  = igb_set_dbal,
3705     [RDBAL15]  = igb_set_dbal,
3706     [STATUS]   = igb_set_status,
3707     [PBACLR]   = igb_set_pbaclr,
3708     [CTRL_EXT] = igb_set_ctrlext,
3709     [FCAH]     = igb_set_16bit,
3710     [FCT]      = igb_set_16bit,
3711     [FCTTV]    = igb_set_16bit,
3712     [FCRTV]    = igb_set_16bit,
3713     [FCRTH]    = igb_set_fcrth,
3714     [FCRTL]    = igb_set_fcrtl,
3715     [CTRL_DUP] = igb_set_ctrl,
3716     [RFCTL]    = igb_set_rfctl,
3717     [TIMINCA]  = igb_set_timinca,
3718     [TIMADJH]  = igb_set_timadjh,
3719 
3720     [IP6AT ... IP6AT + 3]    = igb_mac_writereg,
3721     [IP4AT ... IP4AT + 6]    = igb_mac_writereg,
3722     [RA]                     = igb_mac_writereg,
3723     [RA + 1]                 = igb_mac_setmacaddr,
3724     [RA + 2 ... RA + 31]     = igb_mac_writereg,
3725     [RA2 ... RA2 + 31]       = igb_mac_writereg,
3726     [WUPM ... WUPM + 31]     = igb_mac_writereg,
3727     [MTA ... MTA + E1000_MC_TBL_SIZE - 1] = igb_mac_writereg,
3728     [VFTA ... VFTA + E1000_VLAN_FILTER_TBL_SIZE - 1] = igb_mac_writereg,
3729     [FFMT ... FFMT + 254]    = igb_set_4bit,
3730     [MDEF ... MDEF + 7]      = igb_mac_writereg,
3731     [FTFT ... FTFT + 254]    = igb_mac_writereg,
3732     [RETA ... RETA + 31]     = igb_mac_writereg,
3733     [RSSRK ... RSSRK + 9]    = igb_mac_writereg,
3734     [MAVTV0 ... MAVTV3]      = igb_mac_writereg,
3735     [EITR0 ... EITR0 + IGB_INTR_NUM - 1] = igb_set_eitr,
3736 
3737     /* IGB specific: */
3738     [FWSM]     = igb_mac_writereg,
3739     [SW_FW_SYNC] = igb_mac_writereg,
3740     [EICR] = igb_set_eicr,
3741     [EICS] = igb_set_eics,
3742     [EIAC] = igb_set_eiac,
3743     [EIAM] = igb_set_eiam,
3744     [EIMC] = igb_set_eimc,
3745     [EIMS] = igb_set_eims,
3746     [IVAR0 ... IVAR0 + 7] = igb_mac_writereg,
3747     igb_putreg(IVAR_MISC),
3748     igb_putreg(TSYNCRXCFG),
3749     [ETQF0 ... ETQF0 + 7] = igb_mac_writereg,
3750     igb_putreg(VT_CTL),
3751     [P2VMAILBOX0 ... P2VMAILBOX7] = igb_set_pfmailbox,
3752     [V2PMAILBOX0 ... V2PMAILBOX7] = igb_set_vfmailbox,
3753     [MBVFICR] = igb_w1c,
3754     [VMBMEM0 ... VMBMEM0 + 127] = igb_mac_writereg,
3755     igb_putreg(MBVFIMR),
3756     [VFLRE] = igb_w1c,
3757     igb_putreg(VFRE),
3758     igb_putreg(VFTE),
3759     igb_putreg(QDE),
3760     igb_putreg(DTXSWC),
3761     igb_putreg(RPLOLR),
3762     [VLVF0 ... VLVF0 + E1000_VLVF_ARRAY_SIZE - 1] = igb_mac_writereg,
3763     [VMVIR0 ... VMVIR7] = igb_mac_writereg,
3764     [VMOLR0 ... VMOLR7] = igb_mac_writereg,
3765     [UTA ... UTA + E1000_MC_TBL_SIZE - 1] = igb_mac_writereg,
3766     [PVTCTRL0] = igb_set_vtctrl,
3767     [PVTCTRL1] = igb_set_vtctrl,
3768     [PVTCTRL2] = igb_set_vtctrl,
3769     [PVTCTRL3] = igb_set_vtctrl,
3770     [PVTCTRL4] = igb_set_vtctrl,
3771     [PVTCTRL5] = igb_set_vtctrl,
3772     [PVTCTRL6] = igb_set_vtctrl,
3773     [PVTCTRL7] = igb_set_vtctrl,
3774     [PVTEICS0] = igb_set_vteics,
3775     [PVTEICS1] = igb_set_vteics,
3776     [PVTEICS2] = igb_set_vteics,
3777     [PVTEICS3] = igb_set_vteics,
3778     [PVTEICS4] = igb_set_vteics,
3779     [PVTEICS5] = igb_set_vteics,
3780     [PVTEICS6] = igb_set_vteics,
3781     [PVTEICS7] = igb_set_vteics,
3782     [PVTEIMS0] = igb_set_vteims,
3783     [PVTEIMS1] = igb_set_vteims,
3784     [PVTEIMS2] = igb_set_vteims,
3785     [PVTEIMS3] = igb_set_vteims,
3786     [PVTEIMS4] = igb_set_vteims,
3787     [PVTEIMS5] = igb_set_vteims,
3788     [PVTEIMS6] = igb_set_vteims,
3789     [PVTEIMS7] = igb_set_vteims,
3790     [PVTEIMC0] = igb_set_vteimc,
3791     [PVTEIMC1] = igb_set_vteimc,
3792     [PVTEIMC2] = igb_set_vteimc,
3793     [PVTEIMC3] = igb_set_vteimc,
3794     [PVTEIMC4] = igb_set_vteimc,
3795     [PVTEIMC5] = igb_set_vteimc,
3796     [PVTEIMC6] = igb_set_vteimc,
3797     [PVTEIMC7] = igb_set_vteimc,
3798     [PVTEIAC0] = igb_set_vteiac,
3799     [PVTEIAC1] = igb_set_vteiac,
3800     [PVTEIAC2] = igb_set_vteiac,
3801     [PVTEIAC3] = igb_set_vteiac,
3802     [PVTEIAC4] = igb_set_vteiac,
3803     [PVTEIAC5] = igb_set_vteiac,
3804     [PVTEIAC6] = igb_set_vteiac,
3805     [PVTEIAC7] = igb_set_vteiac,
3806     [PVTEIAM0] = igb_set_vteiam,
3807     [PVTEIAM1] = igb_set_vteiam,
3808     [PVTEIAM2] = igb_set_vteiam,
3809     [PVTEIAM3] = igb_set_vteiam,
3810     [PVTEIAM4] = igb_set_vteiam,
3811     [PVTEIAM5] = igb_set_vteiam,
3812     [PVTEIAM6] = igb_set_vteiam,
3813     [PVTEIAM7] = igb_set_vteiam,
3814     [PVTEICR0] = igb_set_vteicr,
3815     [PVTEICR1] = igb_set_vteicr,
3816     [PVTEICR2] = igb_set_vteicr,
3817     [PVTEICR3] = igb_set_vteicr,
3818     [PVTEICR4] = igb_set_vteicr,
3819     [PVTEICR5] = igb_set_vteicr,
3820     [PVTEICR6] = igb_set_vteicr,
3821     [PVTEICR7] = igb_set_vteicr,
3822     [VTIVAR ... VTIVAR + 7] = igb_set_vtivar,
3823     [VTIVAR_MISC ... VTIVAR_MISC + 7] = igb_mac_writereg
3824 };
3825 enum { IGB_NWRITEOPS = ARRAY_SIZE(igb_macreg_writeops) };
3826 
3827 enum { MAC_ACCESS_PARTIAL = 1 };
3828 
3829 /*
3830  * The array below combines alias offsets of the index values for the
3831  * MAC registers that have aliases, with the indication of not fully
3832  * implemented registers (lowest bit). This combination is possible
3833  * because all of the offsets are even.
3834  */
3835 static const uint16_t mac_reg_access[E1000E_MAC_SIZE] = {
3836     /* Alias index offsets */
3837     [FCRTL_A] = 0x07fe,
3838     [RDFH_A]  = 0xe904, [RDFT_A]  = 0xe904,
3839     [TDFH_A]  = 0xed00, [TDFT_A]  = 0xed00,
3840     [RA_A ... RA_A + 31]      = 0x14f0,
3841     [VFTA_A ... VFTA_A + E1000_VLAN_FILTER_TBL_SIZE - 1] = 0x1400,
3842 
3843     [RDBAL0_A] = 0x2600,
3844     [RDBAH0_A] = 0x2600,
3845     [RDLEN0_A] = 0x2600,
3846     [SRRCTL0_A] = 0x2600,
3847     [RDH0_A] = 0x2600,
3848     [RDT0_A] = 0x2600,
3849     [RXDCTL0_A] = 0x2600,
3850     [RXCTL0_A] = 0x2600,
3851     [RQDPC0_A] = 0x2600,
3852     [RDBAL1_A] = 0x25D0,
3853     [RDBAL2_A] = 0x25A0,
3854     [RDBAL3_A] = 0x2570,
3855     [RDBAH1_A] = 0x25D0,
3856     [RDBAH2_A] = 0x25A0,
3857     [RDBAH3_A] = 0x2570,
3858     [RDLEN1_A] = 0x25D0,
3859     [RDLEN2_A] = 0x25A0,
3860     [RDLEN3_A] = 0x2570,
3861     [SRRCTL1_A] = 0x25D0,
3862     [SRRCTL2_A] = 0x25A0,
3863     [SRRCTL3_A] = 0x2570,
3864     [RDH1_A] = 0x25D0,
3865     [RDH2_A] = 0x25A0,
3866     [RDH3_A] = 0x2570,
3867     [RDT1_A] = 0x25D0,
3868     [RDT2_A] = 0x25A0,
3869     [RDT3_A] = 0x2570,
3870     [RXDCTL1_A] = 0x25D0,
3871     [RXDCTL2_A] = 0x25A0,
3872     [RXDCTL3_A] = 0x2570,
3873     [RXCTL1_A] = 0x25D0,
3874     [RXCTL2_A] = 0x25A0,
3875     [RXCTL3_A] = 0x2570,
3876     [RQDPC1_A] = 0x25D0,
3877     [RQDPC2_A] = 0x25A0,
3878     [RQDPC3_A] = 0x2570,
3879     [TDBAL0_A] = 0x2A00,
3880     [TDBAH0_A] = 0x2A00,
3881     [TDLEN0_A] = 0x2A00,
3882     [TDH0_A] = 0x2A00,
3883     [TDT0_A] = 0x2A00,
3884     [TXCTL0_A] = 0x2A00,
3885     [TDWBAL0_A] = 0x2A00,
3886     [TDWBAH0_A] = 0x2A00,
3887     [TDBAL1_A] = 0x29D0,
3888     [TDBAL2_A] = 0x29A0,
3889     [TDBAL3_A] = 0x2970,
3890     [TDBAH1_A] = 0x29D0,
3891     [TDBAH2_A] = 0x29A0,
3892     [TDBAH3_A] = 0x2970,
3893     [TDLEN1_A] = 0x29D0,
3894     [TDLEN2_A] = 0x29A0,
3895     [TDLEN3_A] = 0x2970,
3896     [TDH1_A] = 0x29D0,
3897     [TDH2_A] = 0x29A0,
3898     [TDH3_A] = 0x2970,
3899     [TDT1_A] = 0x29D0,
3900     [TDT2_A] = 0x29A0,
3901     [TDT3_A] = 0x2970,
3902     [TXDCTL0_A] = 0x2A00,
3903     [TXDCTL1_A] = 0x29D0,
3904     [TXDCTL2_A] = 0x29A0,
3905     [TXDCTL3_A] = 0x2970,
3906     [TXCTL1_A] = 0x29D0,
3907     [TXCTL2_A] = 0x29A0,
3908     [TXCTL3_A] = 0x29D0,
3909     [TDWBAL1_A] = 0x29D0,
3910     [TDWBAL2_A] = 0x29A0,
3911     [TDWBAL3_A] = 0x2970,
3912     [TDWBAH1_A] = 0x29D0,
3913     [TDWBAH2_A] = 0x29A0,
3914     [TDWBAH3_A] = 0x2970,
3915 
3916     /* Access options */
3917     [RDFH]  = MAC_ACCESS_PARTIAL,    [RDFT]  = MAC_ACCESS_PARTIAL,
3918     [RDFHS] = MAC_ACCESS_PARTIAL,    [RDFTS] = MAC_ACCESS_PARTIAL,
3919     [RDFPC] = MAC_ACCESS_PARTIAL,
3920     [TDFH]  = MAC_ACCESS_PARTIAL,    [TDFT]  = MAC_ACCESS_PARTIAL,
3921     [TDFHS] = MAC_ACCESS_PARTIAL,    [TDFTS] = MAC_ACCESS_PARTIAL,
3922     [TDFPC] = MAC_ACCESS_PARTIAL,    [EECD]  = MAC_ACCESS_PARTIAL,
3923     [FLA]   = MAC_ACCESS_PARTIAL,
3924     [FCAL]  = MAC_ACCESS_PARTIAL,    [FCAH]  = MAC_ACCESS_PARTIAL,
3925     [FCT]   = MAC_ACCESS_PARTIAL,    [FCTTV] = MAC_ACCESS_PARTIAL,
3926     [FCRTV] = MAC_ACCESS_PARTIAL,    [FCRTL] = MAC_ACCESS_PARTIAL,
3927     [FCRTH] = MAC_ACCESS_PARTIAL,
3928     [MAVTV0 ... MAVTV3] = MAC_ACCESS_PARTIAL
3929 };
3930 
3931 void
3932 igb_core_write(IGBCore *core, hwaddr addr, uint64_t val, unsigned size)
3933 {
3934     uint16_t index = igb_get_reg_index_with_offset(mac_reg_access, addr);
3935 
3936     if (index < IGB_NWRITEOPS && igb_macreg_writeops[index]) {
3937         if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
3938             trace_e1000e_wrn_regs_write_trivial(index << 2);
3939         }
3940         trace_e1000e_core_write(index << 2, size, val);
3941         igb_macreg_writeops[index](core, index, val);
3942     } else if (index < IGB_NREADOPS && igb_macreg_readops[index]) {
3943         trace_e1000e_wrn_regs_write_ro(index << 2, size, val);
3944     } else {
3945         trace_e1000e_wrn_regs_write_unknown(index << 2, size, val);
3946     }
3947 }
3948 
3949 uint64_t
3950 igb_core_read(IGBCore *core, hwaddr addr, unsigned size)
3951 {
3952     uint64_t val;
3953     uint16_t index = igb_get_reg_index_with_offset(mac_reg_access, addr);
3954 
3955     if (index < IGB_NREADOPS && igb_macreg_readops[index]) {
3956         if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
3957             trace_e1000e_wrn_regs_read_trivial(index << 2);
3958         }
3959         val = igb_macreg_readops[index](core, index);
3960         trace_e1000e_core_read(index << 2, size, val);
3961         return val;
3962     } else {
3963         trace_e1000e_wrn_regs_read_unknown(index << 2, size);
3964     }
3965     return 0;
3966 }
3967 
3968 static inline void
3969 igb_autoneg_pause(IGBCore *core)
3970 {
3971     timer_del(core->autoneg_timer);
3972 }
3973 
3974 static void
3975 igb_autoneg_resume(IGBCore *core)
3976 {
3977     if (igb_have_autoneg(core) &&
3978         !(core->phy[MII_BMSR] & MII_BMSR_AN_COMP)) {
3979         qemu_get_queue(core->owner_nic)->link_down = false;
3980         timer_mod(core->autoneg_timer,
3981                   qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
3982     }
3983 }
3984 
3985 static void
3986 igb_vm_state_change(void *opaque, bool running, RunState state)
3987 {
3988     IGBCore *core = opaque;
3989 
3990     if (running) {
3991         trace_e1000e_vm_state_running();
3992         igb_intrmgr_resume(core);
3993         igb_autoneg_resume(core);
3994     } else {
3995         trace_e1000e_vm_state_stopped();
3996         igb_autoneg_pause(core);
3997         igb_intrmgr_pause(core);
3998     }
3999 }
4000 
4001 void
4002 igb_core_pci_realize(IGBCore        *core,
4003                      const uint16_t *eeprom_templ,
4004                      uint32_t        eeprom_size,
4005                      const uint8_t  *macaddr)
4006 {
4007     int i;
4008 
4009     core->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
4010                                        igb_autoneg_timer, core);
4011     igb_intrmgr_pci_realize(core);
4012 
4013     core->vmstate = qemu_add_vm_change_state_handler(igb_vm_state_change, core);
4014 
4015     for (i = 0; i < IGB_NUM_QUEUES; i++) {
4016         net_tx_pkt_init(&core->tx[i].tx_pkt, E1000E_MAX_TX_FRAGS);
4017     }
4018 
4019     net_rx_pkt_init(&core->rx_pkt);
4020 
4021     e1000x_core_prepare_eeprom(core->eeprom,
4022                                eeprom_templ,
4023                                eeprom_size,
4024                                PCI_DEVICE_GET_CLASS(core->owner)->device_id,
4025                                macaddr);
4026     igb_update_rx_offloads(core);
4027 }
4028 
4029 void
4030 igb_core_pci_uninit(IGBCore *core)
4031 {
4032     int i;
4033 
4034     timer_free(core->autoneg_timer);
4035 
4036     igb_intrmgr_pci_unint(core);
4037 
4038     qemu_del_vm_change_state_handler(core->vmstate);
4039 
4040     for (i = 0; i < IGB_NUM_QUEUES; i++) {
4041         net_tx_pkt_uninit(core->tx[i].tx_pkt);
4042     }
4043 
4044     net_rx_pkt_uninit(core->rx_pkt);
4045 }
4046 
4047 static const uint16_t
4048 igb_phy_reg_init[] = {
4049     [MII_BMCR] = MII_BMCR_SPEED1000 |
4050                  MII_BMCR_FD        |
4051                  MII_BMCR_AUTOEN,
4052 
4053     [MII_BMSR] = MII_BMSR_EXTCAP    |
4054                  MII_BMSR_LINK_ST   |
4055                  MII_BMSR_AUTONEG   |
4056                  MII_BMSR_MFPS      |
4057                  MII_BMSR_EXTSTAT   |
4058                  MII_BMSR_10T_HD    |
4059                  MII_BMSR_10T_FD    |
4060                  MII_BMSR_100TX_HD  |
4061                  MII_BMSR_100TX_FD,
4062 
4063     [MII_PHYID1]            = IGP03E1000_E_PHY_ID >> 16,
4064     [MII_PHYID2]            = (IGP03E1000_E_PHY_ID & 0xfff0) | 1,
4065     [MII_ANAR]              = MII_ANAR_CSMACD | MII_ANAR_10 |
4066                               MII_ANAR_10FD | MII_ANAR_TX |
4067                               MII_ANAR_TXFD | MII_ANAR_PAUSE |
4068                               MII_ANAR_PAUSE_ASYM,
4069     [MII_ANLPAR]            = MII_ANLPAR_10 | MII_ANLPAR_10FD |
4070                               MII_ANLPAR_TX | MII_ANLPAR_TXFD |
4071                               MII_ANLPAR_T4 | MII_ANLPAR_PAUSE,
4072     [MII_ANER]              = MII_ANER_NP | MII_ANER_NWAY,
4073     [MII_ANNP]              = 0x1 | MII_ANNP_MP,
4074     [MII_CTRL1000]          = MII_CTRL1000_HALF | MII_CTRL1000_FULL |
4075                               MII_CTRL1000_PORT | MII_CTRL1000_MASTER,
4076     [MII_STAT1000]          = MII_STAT1000_HALF | MII_STAT1000_FULL |
4077                               MII_STAT1000_ROK | MII_STAT1000_LOK,
4078     [MII_EXTSTAT]           = MII_EXTSTAT_1000T_HD | MII_EXTSTAT_1000T_FD,
4079 
4080     [IGP01E1000_PHY_PORT_CONFIG] = BIT(5) | BIT(8),
4081     [IGP01E1000_PHY_PORT_STATUS] = IGP01E1000_PSSR_SPEED_1000MBPS,
4082     [IGP02E1000_PHY_POWER_MGMT]  = BIT(0) | BIT(3) | IGP02E1000_PM_D3_LPLU |
4083                                    IGP01E1000_PSCFR_SMART_SPEED
4084 };
4085 
4086 static const uint32_t igb_mac_reg_init[] = {
4087     [LEDCTL]        = 2 | (3 << 8) | BIT(15) | (6 << 16) | (7 << 24),
4088     [EEMNGCTL]      = BIT(31),
4089     [TXDCTL0]       = E1000_TXDCTL_QUEUE_ENABLE,
4090     [RXDCTL0]       = E1000_RXDCTL_QUEUE_ENABLE | (1 << 16),
4091     [RXDCTL1]       = 1 << 16,
4092     [RXDCTL2]       = 1 << 16,
4093     [RXDCTL3]       = 1 << 16,
4094     [RXDCTL4]       = 1 << 16,
4095     [RXDCTL5]       = 1 << 16,
4096     [RXDCTL6]       = 1 << 16,
4097     [RXDCTL7]       = 1 << 16,
4098     [RXDCTL8]       = 1 << 16,
4099     [RXDCTL9]       = 1 << 16,
4100     [RXDCTL10]      = 1 << 16,
4101     [RXDCTL11]      = 1 << 16,
4102     [RXDCTL12]      = 1 << 16,
4103     [RXDCTL13]      = 1 << 16,
4104     [RXDCTL14]      = 1 << 16,
4105     [RXDCTL15]      = 1 << 16,
4106     [TIPG]          = 0x08 | (0x04 << 10) | (0x06 << 20),
4107     [CTRL]          = E1000_CTRL_FD | E1000_CTRL_LRST | E1000_CTRL_SPD_1000 |
4108                       E1000_CTRL_ADVD3WUC,
4109     [STATUS]        = E1000_STATUS_PHYRA | BIT(31),
4110     [EECD]          = E1000_EECD_FWE_DIS | E1000_EECD_PRES |
4111                       (2 << E1000_EECD_SIZE_EX_SHIFT),
4112     [GCR]           = E1000_L0S_ADJUST |
4113                       E1000_GCR_CMPL_TMOUT_RESEND |
4114                       E1000_GCR_CAP_VER2 |
4115                       E1000_L1_ENTRY_LATENCY_MSB |
4116                       E1000_L1_ENTRY_LATENCY_LSB,
4117     [RXCSUM]        = E1000_RXCSUM_IPOFLD | E1000_RXCSUM_TUOFLD,
4118     [TXPBS]         = 0x28,
4119     [RXPBS]         = 0x40,
4120     [TCTL]          = E1000_TCTL_PSP | (0xF << E1000_CT_SHIFT) |
4121                       (0x40 << E1000_COLD_SHIFT) | (0x1 << 26) | (0xA << 28),
4122     [TCTL_EXT]      = 0x40 | (0x42 << 10),
4123     [DTXCTL]        = E1000_DTXCTL_8023LL | E1000_DTXCTL_SPOOF_INT,
4124     [VET]           = ETH_P_VLAN | (ETH_P_VLAN << 16),
4125 
4126     [V2PMAILBOX0 ... V2PMAILBOX0 + IGB_MAX_VF_FUNCTIONS - 1] = E1000_V2PMAILBOX_RSTI,
4127     [MBVFIMR]       = 0xFF,
4128     [VFRE]          = 0xFF,
4129     [VFTE]          = 0xFF,
4130     [VMOLR0 ... VMOLR0 + 7] = 0x2600 | E1000_VMOLR_STRCRC,
4131     [RPLOLR]        = E1000_RPLOLR_STRCRC,
4132     [RLPML]         = 0x2600,
4133     [TXCTL0]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4134                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4135                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4136     [TXCTL1]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4137                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4138                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4139     [TXCTL2]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4140                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4141                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4142     [TXCTL3]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4143                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4144                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4145     [TXCTL4]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4146                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4147                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4148     [TXCTL5]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4149                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4150                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4151     [TXCTL6]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4152                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4153                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4154     [TXCTL7]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4155                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4156                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4157     [TXCTL8]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4158                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4159                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4160     [TXCTL9]        = E1000_DCA_TXCTRL_DATA_RRO_EN |
4161                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4162                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4163     [TXCTL10]       = E1000_DCA_TXCTRL_DATA_RRO_EN |
4164                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4165                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4166     [TXCTL11]       = E1000_DCA_TXCTRL_DATA_RRO_EN |
4167                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4168                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4169     [TXCTL12]       = E1000_DCA_TXCTRL_DATA_RRO_EN |
4170                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4171                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4172     [TXCTL13]       = E1000_DCA_TXCTRL_DATA_RRO_EN |
4173                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4174                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4175     [TXCTL14]       = E1000_DCA_TXCTRL_DATA_RRO_EN |
4176                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4177                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4178     [TXCTL15]       = E1000_DCA_TXCTRL_DATA_RRO_EN |
4179                       E1000_DCA_TXCTRL_TX_WB_RO_EN |
4180                       E1000_DCA_TXCTRL_DESC_RRO_EN,
4181 };
4182 
4183 static void igb_reset(IGBCore *core, bool sw)
4184 {
4185     struct igb_tx *tx;
4186     int i;
4187 
4188     timer_del(core->autoneg_timer);
4189 
4190     igb_intrmgr_reset(core);
4191 
4192     memset(core->phy, 0, sizeof core->phy);
4193     memcpy(core->phy, igb_phy_reg_init, sizeof igb_phy_reg_init);
4194 
4195     for (i = 0; i < E1000E_MAC_SIZE; i++) {
4196         if (sw &&
4197             (i == RXPBS || i == TXPBS ||
4198              (i >= EITR0 && i < EITR0 + IGB_INTR_NUM))) {
4199             continue;
4200         }
4201 
4202         core->mac[i] = i < ARRAY_SIZE(igb_mac_reg_init) ?
4203                        igb_mac_reg_init[i] : 0;
4204     }
4205 
4206     if (qemu_get_queue(core->owner_nic)->link_down) {
4207         igb_link_down(core);
4208     }
4209 
4210     e1000x_reset_mac_addr(core->owner_nic, core->mac, core->permanent_mac);
4211 
4212     for (int vfn = 0; vfn < IGB_MAX_VF_FUNCTIONS; vfn++) {
4213         /* Set RSTI, so VF can identify a PF reset is in progress */
4214         core->mac[V2PMAILBOX0 + vfn] |= E1000_V2PMAILBOX_RSTI;
4215     }
4216 
4217     for (i = 0; i < ARRAY_SIZE(core->tx); i++) {
4218         tx = &core->tx[i];
4219         memset(tx->ctx, 0, sizeof(tx->ctx));
4220         tx->first = true;
4221         tx->skip_cp = false;
4222     }
4223 }
4224 
4225 void
4226 igb_core_reset(IGBCore *core)
4227 {
4228     igb_reset(core, false);
4229 }
4230 
4231 void igb_core_pre_save(IGBCore *core)
4232 {
4233     int i;
4234     NetClientState *nc = qemu_get_queue(core->owner_nic);
4235 
4236     /*
4237      * If link is down and auto-negotiation is supported and ongoing,
4238      * complete auto-negotiation immediately. This allows us to look
4239      * at MII_BMSR_AN_COMP to infer link status on load.
4240      */
4241     if (nc->link_down && igb_have_autoneg(core)) {
4242         core->phy[MII_BMSR] |= MII_BMSR_AN_COMP;
4243         igb_update_flowctl_status(core);
4244     }
4245 
4246     for (i = 0; i < ARRAY_SIZE(core->tx); i++) {
4247         if (net_tx_pkt_has_fragments(core->tx[i].tx_pkt)) {
4248             core->tx[i].skip_cp = true;
4249         }
4250     }
4251 }
4252 
4253 int
4254 igb_core_post_load(IGBCore *core)
4255 {
4256     NetClientState *nc = qemu_get_queue(core->owner_nic);
4257 
4258     /*
4259      * nc.link_down can't be migrated, so infer link_down according
4260      * to link status bit in core.mac[STATUS].
4261      */
4262     nc->link_down = (core->mac[STATUS] & E1000_STATUS_LU) == 0;
4263 
4264     return 0;
4265 }
4266