ethdev: fix max Rx packet length
[dpdk.git] / drivers / net / igc / igc_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2020 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <string.h>
7
8 #include <rte_string_fns.h>
9 #include <rte_pci.h>
10 #include <rte_bus_pci.h>
11 #include <ethdev_driver.h>
12 #include <ethdev_pci.h>
13 #include <rte_malloc.h>
14 #include <rte_alarm.h>
15
16 #include "igc_logs.h"
17 #include "igc_txrx.h"
18 #include "igc_filter.h"
19 #include "igc_flow.h"
20
21 #define IGC_INTEL_VENDOR_ID             0x8086
22
23 #define IGC_FC_PAUSE_TIME               0x0680
24 #define IGC_LINK_UPDATE_CHECK_TIMEOUT   90  /* 9s */
25 #define IGC_LINK_UPDATE_CHECK_INTERVAL  100 /* ms */
26
27 #define IGC_MISC_VEC_ID                 RTE_INTR_VEC_ZERO_OFFSET
28 #define IGC_RX_VEC_START                RTE_INTR_VEC_RXTX_OFFSET
29 #define IGC_MSIX_OTHER_INTR_VEC         0   /* MSI-X other interrupt vector */
30 #define IGC_FLAG_NEED_LINK_UPDATE       (1u << 0)       /* need update link */
31
32 #define IGC_DEFAULT_RX_FREE_THRESH      32
33
34 #define IGC_DEFAULT_RX_PTHRESH          8
35 #define IGC_DEFAULT_RX_HTHRESH          8
36 #define IGC_DEFAULT_RX_WTHRESH          4
37
38 #define IGC_DEFAULT_TX_PTHRESH          8
39 #define IGC_DEFAULT_TX_HTHRESH          1
40 #define IGC_DEFAULT_TX_WTHRESH          16
41
42 /* MSI-X other interrupt vector */
43 #define IGC_MSIX_OTHER_INTR_VEC         0
44
45 /* External VLAN Enable bit mask */
46 #define IGC_CTRL_EXT_EXT_VLAN           (1u << 26)
47
48 /* Speed select */
49 #define IGC_CTRL_SPEED_MASK             (7u << 8)
50 #define IGC_CTRL_SPEED_2500             (6u << 8)
51
52 /* External VLAN Ether Type bit mask and shift */
53 #define IGC_VET_EXT                     0xFFFF0000
54 #define IGC_VET_EXT_SHIFT               16
55
56 /* Force EEE Auto-negotiation */
57 #define IGC_EEER_EEE_FRC_AN             (1u << 28)
58
59 /* Per Queue Good Packets Received Count */
60 #define IGC_PQGPRC(idx)         (0x10010 + 0x100 * (idx))
61 /* Per Queue Good Octets Received Count */
62 #define IGC_PQGORC(idx)         (0x10018 + 0x100 * (idx))
63 /* Per Queue Good Octets Transmitted Count */
64 #define IGC_PQGOTC(idx)         (0x10034 + 0x100 * (idx))
65 /* Per Queue Multicast Packets Received Count */
66 #define IGC_PQMPRC(idx)         (0x10038 + 0x100 * (idx))
67 /* Transmit Queue Drop Packet Count */
68 #define IGC_TQDPC(idx)          (0xe030 + 0x40 * (idx))
69
70 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
71 #define U32_0_IN_U64            0       /* lower bytes of u64 */
72 #define U32_1_IN_U64            1       /* higher bytes of u64 */
73 #else
74 #define U32_0_IN_U64            1
75 #define U32_1_IN_U64            0
76 #endif
77
78 #define IGC_ALARM_INTERVAL      8000000u
79 /* us, about 13.6s some per-queue registers will wrap around back to 0. */
80
81 static const struct rte_eth_desc_lim rx_desc_lim = {
82         .nb_max = IGC_MAX_RXD,
83         .nb_min = IGC_MIN_RXD,
84         .nb_align = IGC_RXD_ALIGN,
85 };
86
87 static const struct rte_eth_desc_lim tx_desc_lim = {
88         .nb_max = IGC_MAX_TXD,
89         .nb_min = IGC_MIN_TXD,
90         .nb_align = IGC_TXD_ALIGN,
91         .nb_seg_max = IGC_TX_MAX_SEG,
92         .nb_mtu_seg_max = IGC_TX_MAX_MTU_SEG,
93 };
94
95 static const struct rte_pci_id pci_id_igc_map[] = {
96         { RTE_PCI_DEVICE(IGC_INTEL_VENDOR_ID, IGC_DEV_ID_I225_LM) },
97         { RTE_PCI_DEVICE(IGC_INTEL_VENDOR_ID, IGC_DEV_ID_I225_V)  },
98         { RTE_PCI_DEVICE(IGC_INTEL_VENDOR_ID, IGC_DEV_ID_I225_I)  },
99         { RTE_PCI_DEVICE(IGC_INTEL_VENDOR_ID, IGC_DEV_ID_I225_K)  },
100         { .vendor_id = 0, /* sentinel */ },
101 };
102
103 /* store statistics names and its offset in stats structure */
104 struct rte_igc_xstats_name_off {
105         char name[RTE_ETH_XSTATS_NAME_SIZE];
106         unsigned int offset;
107 };
108
109 static const struct rte_igc_xstats_name_off rte_igc_stats_strings[] = {
110         {"rx_crc_errors", offsetof(struct igc_hw_stats, crcerrs)},
111         {"rx_align_errors", offsetof(struct igc_hw_stats, algnerrc)},
112         {"rx_errors", offsetof(struct igc_hw_stats, rxerrc)},
113         {"rx_missed_packets", offsetof(struct igc_hw_stats, mpc)},
114         {"tx_single_collision_packets", offsetof(struct igc_hw_stats, scc)},
115         {"tx_multiple_collision_packets", offsetof(struct igc_hw_stats, mcc)},
116         {"tx_excessive_collision_packets", offsetof(struct igc_hw_stats,
117                 ecol)},
118         {"tx_late_collisions", offsetof(struct igc_hw_stats, latecol)},
119         {"tx_total_collisions", offsetof(struct igc_hw_stats, colc)},
120         {"tx_deferred_packets", offsetof(struct igc_hw_stats, dc)},
121         {"tx_no_carrier_sense_packets", offsetof(struct igc_hw_stats, tncrs)},
122         {"tx_discarded_packets", offsetof(struct igc_hw_stats, htdpmc)},
123         {"rx_length_errors", offsetof(struct igc_hw_stats, rlec)},
124         {"rx_xon_packets", offsetof(struct igc_hw_stats, xonrxc)},
125         {"tx_xon_packets", offsetof(struct igc_hw_stats, xontxc)},
126         {"rx_xoff_packets", offsetof(struct igc_hw_stats, xoffrxc)},
127         {"tx_xoff_packets", offsetof(struct igc_hw_stats, xofftxc)},
128         {"rx_flow_control_unsupported_packets", offsetof(struct igc_hw_stats,
129                 fcruc)},
130         {"rx_size_64_packets", offsetof(struct igc_hw_stats, prc64)},
131         {"rx_size_65_to_127_packets", offsetof(struct igc_hw_stats, prc127)},
132         {"rx_size_128_to_255_packets", offsetof(struct igc_hw_stats, prc255)},
133         {"rx_size_256_to_511_packets", offsetof(struct igc_hw_stats, prc511)},
134         {"rx_size_512_to_1023_packets", offsetof(struct igc_hw_stats,
135                 prc1023)},
136         {"rx_size_1024_to_max_packets", offsetof(struct igc_hw_stats,
137                 prc1522)},
138         {"rx_broadcast_packets", offsetof(struct igc_hw_stats, bprc)},
139         {"rx_multicast_packets", offsetof(struct igc_hw_stats, mprc)},
140         {"rx_undersize_errors", offsetof(struct igc_hw_stats, ruc)},
141         {"rx_fragment_errors", offsetof(struct igc_hw_stats, rfc)},
142         {"rx_oversize_errors", offsetof(struct igc_hw_stats, roc)},
143         {"rx_jabber_errors", offsetof(struct igc_hw_stats, rjc)},
144         {"rx_no_buffers", offsetof(struct igc_hw_stats, rnbc)},
145         {"rx_management_packets", offsetof(struct igc_hw_stats, mgprc)},
146         {"rx_management_dropped", offsetof(struct igc_hw_stats, mgpdc)},
147         {"tx_management_packets", offsetof(struct igc_hw_stats, mgptc)},
148         {"rx_total_packets", offsetof(struct igc_hw_stats, tpr)},
149         {"tx_total_packets", offsetof(struct igc_hw_stats, tpt)},
150         {"rx_total_bytes", offsetof(struct igc_hw_stats, tor)},
151         {"tx_total_bytes", offsetof(struct igc_hw_stats, tot)},
152         {"tx_size_64_packets", offsetof(struct igc_hw_stats, ptc64)},
153         {"tx_size_65_to_127_packets", offsetof(struct igc_hw_stats, ptc127)},
154         {"tx_size_128_to_255_packets", offsetof(struct igc_hw_stats, ptc255)},
155         {"tx_size_256_to_511_packets", offsetof(struct igc_hw_stats, ptc511)},
156         {"tx_size_512_to_1023_packets", offsetof(struct igc_hw_stats,
157                 ptc1023)},
158         {"tx_size_1023_to_max_packets", offsetof(struct igc_hw_stats,
159                 ptc1522)},
160         {"tx_multicast_packets", offsetof(struct igc_hw_stats, mptc)},
161         {"tx_broadcast_packets", offsetof(struct igc_hw_stats, bptc)},
162         {"tx_tso_packets", offsetof(struct igc_hw_stats, tsctc)},
163         {"rx_sent_to_host_packets", offsetof(struct igc_hw_stats, rpthc)},
164         {"tx_sent_by_host_packets", offsetof(struct igc_hw_stats, hgptc)},
165         {"interrupt_assert_count", offsetof(struct igc_hw_stats, iac)},
166         {"rx_descriptor_lower_threshold",
167                 offsetof(struct igc_hw_stats, icrxdmtc)},
168 };
169
170 #define IGC_NB_XSTATS (sizeof(rte_igc_stats_strings) / \
171                 sizeof(rte_igc_stats_strings[0]))
172
173 static int eth_igc_configure(struct rte_eth_dev *dev);
174 static int eth_igc_link_update(struct rte_eth_dev *dev, int wait_to_complete);
175 static int eth_igc_stop(struct rte_eth_dev *dev);
176 static int eth_igc_start(struct rte_eth_dev *dev);
177 static int eth_igc_set_link_up(struct rte_eth_dev *dev);
178 static int eth_igc_set_link_down(struct rte_eth_dev *dev);
179 static int eth_igc_close(struct rte_eth_dev *dev);
180 static int eth_igc_reset(struct rte_eth_dev *dev);
181 static int eth_igc_promiscuous_enable(struct rte_eth_dev *dev);
182 static int eth_igc_promiscuous_disable(struct rte_eth_dev *dev);
183 static int eth_igc_fw_version_get(struct rte_eth_dev *dev,
184                                 char *fw_version, size_t fw_size);
185 static int eth_igc_infos_get(struct rte_eth_dev *dev,
186                         struct rte_eth_dev_info *dev_info);
187 static int eth_igc_led_on(struct rte_eth_dev *dev);
188 static int eth_igc_led_off(struct rte_eth_dev *dev);
189 static const uint32_t *eth_igc_supported_ptypes_get(struct rte_eth_dev *dev);
190 static int eth_igc_rar_set(struct rte_eth_dev *dev,
191                 struct rte_ether_addr *mac_addr, uint32_t index, uint32_t pool);
192 static void eth_igc_rar_clear(struct rte_eth_dev *dev, uint32_t index);
193 static int eth_igc_default_mac_addr_set(struct rte_eth_dev *dev,
194                         struct rte_ether_addr *addr);
195 static int eth_igc_set_mc_addr_list(struct rte_eth_dev *dev,
196                          struct rte_ether_addr *mc_addr_set,
197                          uint32_t nb_mc_addr);
198 static int eth_igc_allmulticast_enable(struct rte_eth_dev *dev);
199 static int eth_igc_allmulticast_disable(struct rte_eth_dev *dev);
200 static int eth_igc_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
201 static int eth_igc_stats_get(struct rte_eth_dev *dev,
202                         struct rte_eth_stats *rte_stats);
203 static int eth_igc_xstats_get(struct rte_eth_dev *dev,
204                         struct rte_eth_xstat *xstats, unsigned int n);
205 static int eth_igc_xstats_get_by_id(struct rte_eth_dev *dev,
206                                 const uint64_t *ids,
207                                 uint64_t *values, unsigned int n);
208 static int eth_igc_xstats_get_names(struct rte_eth_dev *dev,
209                                 struct rte_eth_xstat_name *xstats_names,
210                                 unsigned int size);
211 static int eth_igc_xstats_get_names_by_id(struct rte_eth_dev *dev,
212                 const uint64_t *ids, struct rte_eth_xstat_name *xstats_names,
213                 unsigned int limit);
214 static int eth_igc_xstats_reset(struct rte_eth_dev *dev);
215 static int
216 eth_igc_queue_stats_mapping_set(struct rte_eth_dev *dev,
217         uint16_t queue_id, uint8_t stat_idx, uint8_t is_rx);
218 static int
219 eth_igc_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id);
220 static int
221 eth_igc_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id);
222 static int
223 eth_igc_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf);
224 static int
225 eth_igc_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf);
226 static int eth_igc_rss_reta_update(struct rte_eth_dev *dev,
227                         struct rte_eth_rss_reta_entry64 *reta_conf,
228                         uint16_t reta_size);
229 static int eth_igc_rss_reta_query(struct rte_eth_dev *dev,
230                        struct rte_eth_rss_reta_entry64 *reta_conf,
231                        uint16_t reta_size);
232 static int eth_igc_rss_hash_update(struct rte_eth_dev *dev,
233                         struct rte_eth_rss_conf *rss_conf);
234 static int eth_igc_rss_hash_conf_get(struct rte_eth_dev *dev,
235                         struct rte_eth_rss_conf *rss_conf);
236 static int
237 eth_igc_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on);
238 static int eth_igc_vlan_offload_set(struct rte_eth_dev *dev, int mask);
239 static int eth_igc_vlan_tpid_set(struct rte_eth_dev *dev,
240                       enum rte_vlan_type vlan_type, uint16_t tpid);
241
242 static const struct eth_dev_ops eth_igc_ops = {
243         .dev_configure          = eth_igc_configure,
244         .link_update            = eth_igc_link_update,
245         .dev_stop               = eth_igc_stop,
246         .dev_start              = eth_igc_start,
247         .dev_close              = eth_igc_close,
248         .dev_reset              = eth_igc_reset,
249         .dev_set_link_up        = eth_igc_set_link_up,
250         .dev_set_link_down      = eth_igc_set_link_down,
251         .promiscuous_enable     = eth_igc_promiscuous_enable,
252         .promiscuous_disable    = eth_igc_promiscuous_disable,
253         .allmulticast_enable    = eth_igc_allmulticast_enable,
254         .allmulticast_disable   = eth_igc_allmulticast_disable,
255         .fw_version_get         = eth_igc_fw_version_get,
256         .dev_infos_get          = eth_igc_infos_get,
257         .dev_led_on             = eth_igc_led_on,
258         .dev_led_off            = eth_igc_led_off,
259         .dev_supported_ptypes_get = eth_igc_supported_ptypes_get,
260         .mtu_set                = eth_igc_mtu_set,
261         .mac_addr_add           = eth_igc_rar_set,
262         .mac_addr_remove        = eth_igc_rar_clear,
263         .mac_addr_set           = eth_igc_default_mac_addr_set,
264         .set_mc_addr_list       = eth_igc_set_mc_addr_list,
265
266         .rx_queue_setup         = eth_igc_rx_queue_setup,
267         .rx_queue_release       = eth_igc_rx_queue_release,
268         .tx_queue_setup         = eth_igc_tx_queue_setup,
269         .tx_queue_release       = eth_igc_tx_queue_release,
270         .tx_done_cleanup        = eth_igc_tx_done_cleanup,
271         .rxq_info_get           = eth_igc_rxq_info_get,
272         .txq_info_get           = eth_igc_txq_info_get,
273         .stats_get              = eth_igc_stats_get,
274         .xstats_get             = eth_igc_xstats_get,
275         .xstats_get_by_id       = eth_igc_xstats_get_by_id,
276         .xstats_get_names_by_id = eth_igc_xstats_get_names_by_id,
277         .xstats_get_names       = eth_igc_xstats_get_names,
278         .stats_reset            = eth_igc_xstats_reset,
279         .xstats_reset           = eth_igc_xstats_reset,
280         .queue_stats_mapping_set = eth_igc_queue_stats_mapping_set,
281         .rx_queue_intr_enable   = eth_igc_rx_queue_intr_enable,
282         .rx_queue_intr_disable  = eth_igc_rx_queue_intr_disable,
283         .flow_ctrl_get          = eth_igc_flow_ctrl_get,
284         .flow_ctrl_set          = eth_igc_flow_ctrl_set,
285         .reta_update            = eth_igc_rss_reta_update,
286         .reta_query             = eth_igc_rss_reta_query,
287         .rss_hash_update        = eth_igc_rss_hash_update,
288         .rss_hash_conf_get      = eth_igc_rss_hash_conf_get,
289         .vlan_filter_set        = eth_igc_vlan_filter_set,
290         .vlan_offload_set       = eth_igc_vlan_offload_set,
291         .vlan_tpid_set          = eth_igc_vlan_tpid_set,
292         .vlan_strip_queue_set   = eth_igc_vlan_strip_queue_set,
293         .flow_ops_get           = eth_igc_flow_ops_get,
294 };
295
296 /*
297  * multiple queue mode checking
298  */
299 static int
300 igc_check_mq_mode(struct rte_eth_dev *dev)
301 {
302         enum rte_eth_rx_mq_mode rx_mq_mode = dev->data->dev_conf.rxmode.mq_mode;
303         enum rte_eth_tx_mq_mode tx_mq_mode = dev->data->dev_conf.txmode.mq_mode;
304
305         if (RTE_ETH_DEV_SRIOV(dev).active != 0) {
306                 PMD_INIT_LOG(ERR, "SRIOV is not supported.");
307                 return -EINVAL;
308         }
309
310         if (rx_mq_mode != ETH_MQ_RX_NONE &&
311                 rx_mq_mode != ETH_MQ_RX_RSS) {
312                 /* RSS together with VMDq not supported*/
313                 PMD_INIT_LOG(ERR, "RX mode %d is not supported.",
314                                 rx_mq_mode);
315                 return -EINVAL;
316         }
317
318         /* To no break software that set invalid mode, only display
319          * warning if invalid mode is used.
320          */
321         if (tx_mq_mode != ETH_MQ_TX_NONE)
322                 PMD_INIT_LOG(WARNING,
323                         "TX mode %d is not supported. Due to meaningless in this driver, just ignore",
324                         tx_mq_mode);
325
326         return 0;
327 }
328
329 static int
330 eth_igc_configure(struct rte_eth_dev *dev)
331 {
332         struct igc_interrupt *intr = IGC_DEV_PRIVATE_INTR(dev);
333         int ret;
334
335         PMD_INIT_FUNC_TRACE();
336
337         if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
338                 dev->data->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
339
340         ret  = igc_check_mq_mode(dev);
341         if (ret != 0)
342                 return ret;
343
344         intr->flags |= IGC_FLAG_NEED_LINK_UPDATE;
345         return 0;
346 }
347
348 static int
349 eth_igc_set_link_up(struct rte_eth_dev *dev)
350 {
351         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
352
353         if (hw->phy.media_type == igc_media_type_copper)
354                 igc_power_up_phy(hw);
355         else
356                 igc_power_up_fiber_serdes_link(hw);
357         return 0;
358 }
359
360 static int
361 eth_igc_set_link_down(struct rte_eth_dev *dev)
362 {
363         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
364
365         if (hw->phy.media_type == igc_media_type_copper)
366                 igc_power_down_phy(hw);
367         else
368                 igc_shutdown_fiber_serdes_link(hw);
369         return 0;
370 }
371
372 /*
373  * disable other interrupt
374  */
375 static void
376 igc_intr_other_disable(struct rte_eth_dev *dev)
377 {
378         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
379         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
380         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
381
382         if (rte_intr_allow_others(intr_handle) &&
383                 dev->data->dev_conf.intr_conf.lsc) {
384                 IGC_WRITE_REG(hw, IGC_EIMC, 1u << IGC_MSIX_OTHER_INTR_VEC);
385         }
386
387         IGC_WRITE_REG(hw, IGC_IMC, ~0);
388         IGC_WRITE_FLUSH(hw);
389 }
390
391 /*
392  * enable other interrupt
393  */
394 static inline void
395 igc_intr_other_enable(struct rte_eth_dev *dev)
396 {
397         struct igc_interrupt *intr = IGC_DEV_PRIVATE_INTR(dev);
398         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
399         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
400         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
401
402         if (rte_intr_allow_others(intr_handle) &&
403                 dev->data->dev_conf.intr_conf.lsc) {
404                 IGC_WRITE_REG(hw, IGC_EIMS, 1u << IGC_MSIX_OTHER_INTR_VEC);
405         }
406
407         IGC_WRITE_REG(hw, IGC_IMS, intr->mask);
408         IGC_WRITE_FLUSH(hw);
409 }
410
411 /*
412  * It reads ICR and gets interrupt causes, check it and set a bit flag
413  * to update link status.
414  */
415 static void
416 eth_igc_interrupt_get_status(struct rte_eth_dev *dev)
417 {
418         uint32_t icr;
419         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
420         struct igc_interrupt *intr = IGC_DEV_PRIVATE_INTR(dev);
421
422         /* read-on-clear nic registers here */
423         icr = IGC_READ_REG(hw, IGC_ICR);
424
425         intr->flags = 0;
426         if (icr & IGC_ICR_LSC)
427                 intr->flags |= IGC_FLAG_NEED_LINK_UPDATE;
428 }
429
430 /* return 0 means link status changed, -1 means not changed */
431 static int
432 eth_igc_link_update(struct rte_eth_dev *dev, int wait_to_complete)
433 {
434         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
435         struct rte_eth_link link;
436         int link_check, count;
437
438         link_check = 0;
439         hw->mac.get_link_status = 1;
440
441         /* possible wait-to-complete in up to 9 seconds */
442         for (count = 0; count < IGC_LINK_UPDATE_CHECK_TIMEOUT; count++) {
443                 /* Read the real link status */
444                 switch (hw->phy.media_type) {
445                 case igc_media_type_copper:
446                         /* Do the work to read phy */
447                         igc_check_for_link(hw);
448                         link_check = !hw->mac.get_link_status;
449                         break;
450
451                 case igc_media_type_fiber:
452                         igc_check_for_link(hw);
453                         link_check = (IGC_READ_REG(hw, IGC_STATUS) &
454                                       IGC_STATUS_LU);
455                         break;
456
457                 case igc_media_type_internal_serdes:
458                         igc_check_for_link(hw);
459                         link_check = hw->mac.serdes_has_link;
460                         break;
461
462                 default:
463                         break;
464                 }
465                 if (link_check || wait_to_complete == 0)
466                         break;
467                 rte_delay_ms(IGC_LINK_UPDATE_CHECK_INTERVAL);
468         }
469         memset(&link, 0, sizeof(link));
470
471         /* Now we check if a transition has happened */
472         if (link_check) {
473                 uint16_t duplex, speed;
474                 hw->mac.ops.get_link_up_info(hw, &speed, &duplex);
475                 link.link_duplex = (duplex == FULL_DUPLEX) ?
476                                 ETH_LINK_FULL_DUPLEX :
477                                 ETH_LINK_HALF_DUPLEX;
478                 link.link_speed = speed;
479                 link.link_status = ETH_LINK_UP;
480                 link.link_autoneg = !(dev->data->dev_conf.link_speeds &
481                                 ETH_LINK_SPEED_FIXED);
482
483                 if (speed == SPEED_2500) {
484                         uint32_t tipg = IGC_READ_REG(hw, IGC_TIPG);
485                         if ((tipg & IGC_TIPG_IPGT_MASK) != 0x0b) {
486                                 tipg &= ~IGC_TIPG_IPGT_MASK;
487                                 tipg |= 0x0b;
488                                 IGC_WRITE_REG(hw, IGC_TIPG, tipg);
489                         }
490                 }
491         } else {
492                 link.link_speed = 0;
493                 link.link_duplex = ETH_LINK_HALF_DUPLEX;
494                 link.link_status = ETH_LINK_DOWN;
495                 link.link_autoneg = ETH_LINK_FIXED;
496         }
497
498         return rte_eth_linkstatus_set(dev, &link);
499 }
500
501 /*
502  * It executes link_update after knowing an interrupt is present.
503  */
504 static void
505 eth_igc_interrupt_action(struct rte_eth_dev *dev)
506 {
507         struct igc_interrupt *intr = IGC_DEV_PRIVATE_INTR(dev);
508         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
509         struct rte_eth_link link;
510         int ret;
511
512         if (intr->flags & IGC_FLAG_NEED_LINK_UPDATE) {
513                 intr->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
514
515                 /* set get_link_status to check register later */
516                 ret = eth_igc_link_update(dev, 0);
517
518                 /* check if link has changed */
519                 if (ret < 0)
520                         return;
521
522                 rte_eth_linkstatus_get(dev, &link);
523                 if (link.link_status)
524                         PMD_DRV_LOG(INFO,
525                                 " Port %d: Link Up - speed %u Mbps - %s",
526                                 dev->data->port_id,
527                                 (unsigned int)link.link_speed,
528                                 link.link_duplex == ETH_LINK_FULL_DUPLEX ?
529                                 "full-duplex" : "half-duplex");
530                 else
531                         PMD_DRV_LOG(INFO, " Port %d: Link Down",
532                                 dev->data->port_id);
533
534                 PMD_DRV_LOG(DEBUG, "PCI Address: " PCI_PRI_FMT,
535                                 pci_dev->addr.domain,
536                                 pci_dev->addr.bus,
537                                 pci_dev->addr.devid,
538                                 pci_dev->addr.function);
539                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
540         }
541 }
542
543 /*
544  * Interrupt handler which shall be registered at first.
545  *
546  * @handle
547  *  Pointer to interrupt handle.
548  * @param
549  *  The address of parameter (struct rte_eth_dev *) registered before.
550  */
551 static void
552 eth_igc_interrupt_handler(void *param)
553 {
554         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
555
556         eth_igc_interrupt_get_status(dev);
557         eth_igc_interrupt_action(dev);
558 }
559
560 static void igc_read_queue_stats_register(struct rte_eth_dev *dev);
561
562 /*
563  * Update the queue status every IGC_ALARM_INTERVAL time.
564  * @param
565  *  The address of parameter (struct rte_eth_dev *) registered before.
566  */
567 static void
568 igc_update_queue_stats_handler(void *param)
569 {
570         struct rte_eth_dev *dev = param;
571         igc_read_queue_stats_register(dev);
572         rte_eal_alarm_set(IGC_ALARM_INTERVAL,
573                         igc_update_queue_stats_handler, dev);
574 }
575
576 /*
577  * rx,tx enable/disable
578  */
579 static void
580 eth_igc_rxtx_control(struct rte_eth_dev *dev, bool enable)
581 {
582         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
583         uint32_t tctl, rctl;
584
585         tctl = IGC_READ_REG(hw, IGC_TCTL);
586         rctl = IGC_READ_REG(hw, IGC_RCTL);
587
588         if (enable) {
589                 /* enable Tx/Rx */
590                 tctl |= IGC_TCTL_EN;
591                 rctl |= IGC_RCTL_EN;
592         } else {
593                 /* disable Tx/Rx */
594                 tctl &= ~IGC_TCTL_EN;
595                 rctl &= ~IGC_RCTL_EN;
596         }
597         IGC_WRITE_REG(hw, IGC_TCTL, tctl);
598         IGC_WRITE_REG(hw, IGC_RCTL, rctl);
599         IGC_WRITE_FLUSH(hw);
600 }
601
602 /*
603  *  This routine disables all traffic on the adapter by issuing a
604  *  global reset on the MAC.
605  */
606 static int
607 eth_igc_stop(struct rte_eth_dev *dev)
608 {
609         struct igc_adapter *adapter = IGC_DEV_PRIVATE(dev);
610         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
611         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
612         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
613         struct rte_eth_link link;
614
615         dev->data->dev_started = 0;
616         adapter->stopped = 1;
617
618         /* disable receive and transmit */
619         eth_igc_rxtx_control(dev, false);
620
621         /* disable all MSI-X interrupts */
622         IGC_WRITE_REG(hw, IGC_EIMC, 0x1f);
623         IGC_WRITE_FLUSH(hw);
624
625         /* clear all MSI-X interrupts */
626         IGC_WRITE_REG(hw, IGC_EICR, 0x1f);
627
628         igc_intr_other_disable(dev);
629
630         rte_eal_alarm_cancel(igc_update_queue_stats_handler, dev);
631
632         /* disable intr eventfd mapping */
633         rte_intr_disable(intr_handle);
634
635         igc_reset_hw(hw);
636
637         /* disable all wake up */
638         IGC_WRITE_REG(hw, IGC_WUC, 0);
639
640         /* disable checking EEE operation in MAC loopback mode */
641         igc_read_reg_check_clear_bits(hw, IGC_EEER, IGC_EEER_EEE_FRC_AN);
642
643         /* Set bit for Go Link disconnect */
644         igc_read_reg_check_set_bits(hw, IGC_82580_PHY_POWER_MGMT,
645                         IGC_82580_PM_GO_LINKD);
646
647         /* Power down the phy. Needed to make the link go Down */
648         eth_igc_set_link_down(dev);
649
650         igc_dev_clear_queues(dev);
651
652         /* clear the recorded link status */
653         memset(&link, 0, sizeof(link));
654         rte_eth_linkstatus_set(dev, &link);
655
656         if (!rte_intr_allow_others(intr_handle))
657                 /* resume to the default handler */
658                 rte_intr_callback_register(intr_handle,
659                                            eth_igc_interrupt_handler,
660                                            (void *)dev);
661
662         /* Clean datapath event and queue/vec mapping */
663         rte_intr_efd_disable(intr_handle);
664         if (intr_handle->intr_vec != NULL) {
665                 rte_free(intr_handle->intr_vec);
666                 intr_handle->intr_vec = NULL;
667         }
668
669         return 0;
670 }
671
672 /*
673  * write interrupt vector allocation register
674  * @hw
675  *  board private structure
676  * @queue_index
677  *  queue index, valid 0,1,2,3
678  * @tx
679  *  tx:1, rx:0
680  * @msix_vector
681  *  msix-vector, valid 0,1,2,3,4
682  */
683 static void
684 igc_write_ivar(struct igc_hw *hw, uint8_t queue_index,
685                 bool tx, uint8_t msix_vector)
686 {
687         uint8_t offset = 0;
688         uint8_t reg_index = queue_index >> 1;
689         uint32_t val;
690
691         /*
692          * IVAR(0)
693          * bit31...24   bit23...16      bit15...8       bit7...0
694          * TX1          RX1             TX0             RX0
695          *
696          * IVAR(1)
697          * bit31...24   bit23...16      bit15...8       bit7...0
698          * TX3          RX3             TX2             RX2
699          */
700
701         if (tx)
702                 offset = 8;
703
704         if (queue_index & 1)
705                 offset += 16;
706
707         val = IGC_READ_REG_ARRAY(hw, IGC_IVAR0, reg_index);
708
709         /* clear bits */
710         val &= ~((uint32_t)0xFF << offset);
711
712         /* write vector and valid bit */
713         val |= (uint32_t)(msix_vector | IGC_IVAR_VALID) << offset;
714
715         IGC_WRITE_REG_ARRAY(hw, IGC_IVAR0, reg_index, val);
716 }
717
718 /* Sets up the hardware to generate MSI-X interrupts properly
719  * @hw
720  *  board private structure
721  */
722 static void
723 igc_configure_msix_intr(struct rte_eth_dev *dev)
724 {
725         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
726         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
727         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
728
729         uint32_t intr_mask;
730         uint32_t vec = IGC_MISC_VEC_ID;
731         uint32_t base = IGC_MISC_VEC_ID;
732         uint32_t misc_shift = 0;
733         int i;
734
735         /* won't configure msix register if no mapping is done
736          * between intr vector and event fd
737          */
738         if (!rte_intr_dp_is_en(intr_handle))
739                 return;
740
741         if (rte_intr_allow_others(intr_handle)) {
742                 base = IGC_RX_VEC_START;
743                 vec = base;
744                 misc_shift = 1;
745         }
746
747         /* turn on MSI-X capability first */
748         IGC_WRITE_REG(hw, IGC_GPIE, IGC_GPIE_MSIX_MODE |
749                                 IGC_GPIE_PBA | IGC_GPIE_EIAME |
750                                 IGC_GPIE_NSICR);
751         intr_mask = RTE_LEN2MASK(intr_handle->nb_efd, uint32_t) <<
752                 misc_shift;
753
754         if (dev->data->dev_conf.intr_conf.lsc)
755                 intr_mask |= (1u << IGC_MSIX_OTHER_INTR_VEC);
756
757         /* enable msix auto-clear */
758         igc_read_reg_check_set_bits(hw, IGC_EIAC, intr_mask);
759
760         /* set other cause interrupt vector */
761         igc_read_reg_check_set_bits(hw, IGC_IVAR_MISC,
762                 (uint32_t)(IGC_MSIX_OTHER_INTR_VEC | IGC_IVAR_VALID) << 8);
763
764         /* enable auto-mask */
765         igc_read_reg_check_set_bits(hw, IGC_EIAM, intr_mask);
766
767         for (i = 0; i < dev->data->nb_rx_queues; i++) {
768                 igc_write_ivar(hw, i, 0, vec);
769                 intr_handle->intr_vec[i] = vec;
770                 if (vec < base + intr_handle->nb_efd - 1)
771                         vec++;
772         }
773
774         IGC_WRITE_FLUSH(hw);
775 }
776
777 /**
778  * It enables the interrupt mask and then enable the interrupt.
779  *
780  * @dev
781  *  Pointer to struct rte_eth_dev.
782  * @on
783  *  Enable or Disable
784  */
785 static void
786 igc_lsc_interrupt_setup(struct rte_eth_dev *dev, uint8_t on)
787 {
788         struct igc_interrupt *intr = IGC_DEV_PRIVATE_INTR(dev);
789
790         if (on)
791                 intr->mask |= IGC_ICR_LSC;
792         else
793                 intr->mask &= ~IGC_ICR_LSC;
794 }
795
796 /*
797  * It enables the interrupt.
798  * It will be called once only during nic initialized.
799  */
800 static void
801 igc_rxq_interrupt_setup(struct rte_eth_dev *dev)
802 {
803         uint32_t mask;
804         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
805         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
806         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
807         int misc_shift = rte_intr_allow_others(intr_handle) ? 1 : 0;
808
809         /* won't configure msix register if no mapping is done
810          * between intr vector and event fd
811          */
812         if (!rte_intr_dp_is_en(intr_handle))
813                 return;
814
815         mask = RTE_LEN2MASK(intr_handle->nb_efd, uint32_t) << misc_shift;
816         IGC_WRITE_REG(hw, IGC_EIMS, mask);
817 }
818
819 /*
820  *  Get hardware rx-buffer size.
821  */
822 static inline int
823 igc_get_rx_buffer_size(struct igc_hw *hw)
824 {
825         return (IGC_READ_REG(hw, IGC_RXPBS) & 0x3f) << 10;
826 }
827
828 /*
829  * igc_hw_control_acquire sets CTRL_EXT:DRV_LOAD bit.
830  * For ASF and Pass Through versions of f/w this means
831  * that the driver is loaded.
832  */
833 static void
834 igc_hw_control_acquire(struct igc_hw *hw)
835 {
836         uint32_t ctrl_ext;
837
838         /* Let firmware know the driver has taken over */
839         ctrl_ext = IGC_READ_REG(hw, IGC_CTRL_EXT);
840         IGC_WRITE_REG(hw, IGC_CTRL_EXT, ctrl_ext | IGC_CTRL_EXT_DRV_LOAD);
841 }
842
843 /*
844  * igc_hw_control_release resets CTRL_EXT:DRV_LOAD bit.
845  * For ASF and Pass Through versions of f/w this means that the
846  * driver is no longer loaded.
847  */
848 static void
849 igc_hw_control_release(struct igc_hw *hw)
850 {
851         uint32_t ctrl_ext;
852
853         /* Let firmware taken over control of h/w */
854         ctrl_ext = IGC_READ_REG(hw, IGC_CTRL_EXT);
855         IGC_WRITE_REG(hw, IGC_CTRL_EXT,
856                         ctrl_ext & ~IGC_CTRL_EXT_DRV_LOAD);
857 }
858
859 static int
860 igc_hardware_init(struct igc_hw *hw)
861 {
862         uint32_t rx_buf_size;
863         int diag;
864
865         /* Let the firmware know the OS is in control */
866         igc_hw_control_acquire(hw);
867
868         /* Issue a global reset */
869         igc_reset_hw(hw);
870
871         /* disable all wake up */
872         IGC_WRITE_REG(hw, IGC_WUC, 0);
873
874         /*
875          * Hardware flow control
876          * - High water mark should allow for at least two standard size (1518)
877          *   frames to be received after sending an XOFF.
878          * - Low water mark works best when it is very near the high water mark.
879          *   This allows the receiver to restart by sending XON when it has
880          *   drained a bit. Here we use an arbitrary value of 1500 which will
881          *   restart after one full frame is pulled from the buffer. There
882          *   could be several smaller frames in the buffer and if so they will
883          *   not trigger the XON until their total number reduces the buffer
884          *   by 1500.
885          */
886         rx_buf_size = igc_get_rx_buffer_size(hw);
887         hw->fc.high_water = rx_buf_size - (RTE_ETHER_MAX_LEN * 2);
888         hw->fc.low_water = hw->fc.high_water - 1500;
889         hw->fc.pause_time = IGC_FC_PAUSE_TIME;
890         hw->fc.send_xon = 1;
891         hw->fc.requested_mode = igc_fc_full;
892
893         diag = igc_init_hw(hw);
894         if (diag < 0)
895                 return diag;
896
897         igc_get_phy_info(hw);
898         igc_check_for_link(hw);
899
900         return 0;
901 }
902
903 static int
904 eth_igc_start(struct rte_eth_dev *dev)
905 {
906         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
907         struct igc_adapter *adapter = IGC_DEV_PRIVATE(dev);
908         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
909         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
910         uint32_t *speeds;
911         int ret;
912
913         PMD_INIT_FUNC_TRACE();
914
915         /* disable all MSI-X interrupts */
916         IGC_WRITE_REG(hw, IGC_EIMC, 0x1f);
917         IGC_WRITE_FLUSH(hw);
918
919         /* clear all MSI-X interrupts */
920         IGC_WRITE_REG(hw, IGC_EICR, 0x1f);
921
922         /* disable uio/vfio intr/eventfd mapping */
923         if (!adapter->stopped)
924                 rte_intr_disable(intr_handle);
925
926         /* Power up the phy. Needed to make the link go Up */
927         eth_igc_set_link_up(dev);
928
929         /* Put the address into the Receive Address Array */
930         igc_rar_set(hw, hw->mac.addr, 0);
931
932         /* Initialize the hardware */
933         if (igc_hardware_init(hw)) {
934                 PMD_DRV_LOG(ERR, "Unable to initialize the hardware");
935                 return -EIO;
936         }
937         adapter->stopped = 0;
938
939         /* check and configure queue intr-vector mapping */
940         if (rte_intr_cap_multiple(intr_handle) &&
941                 dev->data->dev_conf.intr_conf.rxq) {
942                 uint32_t intr_vector = dev->data->nb_rx_queues;
943                 if (rte_intr_efd_enable(intr_handle, intr_vector))
944                         return -1;
945         }
946
947         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
948                 intr_handle->intr_vec = rte_zmalloc("intr_vec",
949                         dev->data->nb_rx_queues * sizeof(int), 0);
950                 if (intr_handle->intr_vec == NULL) {
951                         PMD_DRV_LOG(ERR,
952                                 "Failed to allocate %d rx_queues intr_vec",
953                                 dev->data->nb_rx_queues);
954                         return -ENOMEM;
955                 }
956         }
957
958         /* configure msix for rx interrupt */
959         igc_configure_msix_intr(dev);
960
961         igc_tx_init(dev);
962
963         /* This can fail when allocating mbufs for descriptor rings */
964         ret = igc_rx_init(dev);
965         if (ret) {
966                 PMD_DRV_LOG(ERR, "Unable to initialize RX hardware");
967                 igc_dev_clear_queues(dev);
968                 return ret;
969         }
970
971         igc_clear_hw_cntrs_base_generic(hw);
972
973         /* VLAN Offload Settings */
974         eth_igc_vlan_offload_set(dev,
975                 ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK |
976                 ETH_VLAN_EXTEND_MASK);
977
978         /* Setup link speed and duplex */
979         speeds = &dev->data->dev_conf.link_speeds;
980         if (*speeds == ETH_LINK_SPEED_AUTONEG) {
981                 hw->phy.autoneg_advertised = IGC_ALL_SPEED_DUPLEX_2500;
982                 hw->mac.autoneg = 1;
983         } else {
984                 int num_speeds = 0;
985
986                 if (*speeds & ETH_LINK_SPEED_FIXED) {
987                         PMD_DRV_LOG(ERR,
988                                     "Force speed mode currently not supported");
989                         igc_dev_clear_queues(dev);
990                         return -EINVAL;
991                 }
992
993                 hw->phy.autoneg_advertised = 0;
994                 hw->mac.autoneg = 1;
995
996                 if (*speeds & ~(ETH_LINK_SPEED_10M_HD | ETH_LINK_SPEED_10M |
997                                 ETH_LINK_SPEED_100M_HD | ETH_LINK_SPEED_100M |
998                                 ETH_LINK_SPEED_1G | ETH_LINK_SPEED_2_5G)) {
999                         num_speeds = -1;
1000                         goto error_invalid_config;
1001                 }
1002                 if (*speeds & ETH_LINK_SPEED_10M_HD) {
1003                         hw->phy.autoneg_advertised |= ADVERTISE_10_HALF;
1004                         num_speeds++;
1005                 }
1006                 if (*speeds & ETH_LINK_SPEED_10M) {
1007                         hw->phy.autoneg_advertised |= ADVERTISE_10_FULL;
1008                         num_speeds++;
1009                 }
1010                 if (*speeds & ETH_LINK_SPEED_100M_HD) {
1011                         hw->phy.autoneg_advertised |= ADVERTISE_100_HALF;
1012                         num_speeds++;
1013                 }
1014                 if (*speeds & ETH_LINK_SPEED_100M) {
1015                         hw->phy.autoneg_advertised |= ADVERTISE_100_FULL;
1016                         num_speeds++;
1017                 }
1018                 if (*speeds & ETH_LINK_SPEED_1G) {
1019                         hw->phy.autoneg_advertised |= ADVERTISE_1000_FULL;
1020                         num_speeds++;
1021                 }
1022                 if (*speeds & ETH_LINK_SPEED_2_5G) {
1023                         hw->phy.autoneg_advertised |= ADVERTISE_2500_FULL;
1024                         num_speeds++;
1025                 }
1026                 if (num_speeds == 0)
1027                         goto error_invalid_config;
1028         }
1029
1030         igc_setup_link(hw);
1031
1032         if (rte_intr_allow_others(intr_handle)) {
1033                 /* check if lsc interrupt is enabled */
1034                 if (dev->data->dev_conf.intr_conf.lsc)
1035                         igc_lsc_interrupt_setup(dev, 1);
1036                 else
1037                         igc_lsc_interrupt_setup(dev, 0);
1038         } else {
1039                 rte_intr_callback_unregister(intr_handle,
1040                                              eth_igc_interrupt_handler,
1041                                              (void *)dev);
1042                 if (dev->data->dev_conf.intr_conf.lsc)
1043                         PMD_DRV_LOG(INFO,
1044                                 "LSC won't enable because of no intr multiplex");
1045         }
1046
1047         /* enable uio/vfio intr/eventfd mapping */
1048         rte_intr_enable(intr_handle);
1049
1050         rte_eal_alarm_set(IGC_ALARM_INTERVAL,
1051                         igc_update_queue_stats_handler, dev);
1052
1053         /* check if rxq interrupt is enabled */
1054         if (dev->data->dev_conf.intr_conf.rxq &&
1055                         rte_intr_dp_is_en(intr_handle))
1056                 igc_rxq_interrupt_setup(dev);
1057
1058         /* resume enabled intr since hw reset */
1059         igc_intr_other_enable(dev);
1060
1061         eth_igc_rxtx_control(dev, true);
1062         eth_igc_link_update(dev, 0);
1063
1064         /* configure MAC-loopback mode */
1065         if (dev->data->dev_conf.lpbk_mode == 1) {
1066                 uint32_t reg_val;
1067
1068                 reg_val = IGC_READ_REG(hw, IGC_CTRL);
1069                 reg_val &= ~IGC_CTRL_SPEED_MASK;
1070                 reg_val |= IGC_CTRL_SLU | IGC_CTRL_FRCSPD |
1071                         IGC_CTRL_FRCDPX | IGC_CTRL_FD | IGC_CTRL_SPEED_2500;
1072                 IGC_WRITE_REG(hw, IGC_CTRL, reg_val);
1073
1074                 igc_read_reg_check_set_bits(hw, IGC_EEER, IGC_EEER_EEE_FRC_AN);
1075         }
1076
1077         return 0;
1078
1079 error_invalid_config:
1080         PMD_DRV_LOG(ERR, "Invalid advertised speeds (%u) for port %u",
1081                      dev->data->dev_conf.link_speeds, dev->data->port_id);
1082         igc_dev_clear_queues(dev);
1083         return -EINVAL;
1084 }
1085
1086 static int
1087 igc_reset_swfw_lock(struct igc_hw *hw)
1088 {
1089         int ret_val;
1090
1091         /*
1092          * Do mac ops initialization manually here, since we will need
1093          * some function pointers set by this call.
1094          */
1095         ret_val = igc_init_mac_params(hw);
1096         if (ret_val)
1097                 return ret_val;
1098
1099         /*
1100          * SMBI lock should not fail in this early stage. If this is the case,
1101          * it is due to an improper exit of the application.
1102          * So force the release of the faulty lock.
1103          */
1104         if (igc_get_hw_semaphore_generic(hw) < 0)
1105                 PMD_DRV_LOG(DEBUG, "SMBI lock released");
1106
1107         igc_put_hw_semaphore_generic(hw);
1108
1109         if (hw->mac.ops.acquire_swfw_sync != NULL) {
1110                 uint16_t mask;
1111
1112                 /*
1113                  * Phy lock should not fail in this early stage.
1114                  * If this is the case, it is due to an improper exit of the
1115                  * application. So force the release of the faulty lock.
1116                  */
1117                 mask = IGC_SWFW_PHY0_SM;
1118                 if (hw->mac.ops.acquire_swfw_sync(hw, mask) < 0) {
1119                         PMD_DRV_LOG(DEBUG, "SWFW phy%d lock released",
1120                                     hw->bus.func);
1121                 }
1122                 hw->mac.ops.release_swfw_sync(hw, mask);
1123
1124                 /*
1125                  * This one is more tricky since it is common to all ports; but
1126                  * swfw_sync retries last long enough (1s) to be almost sure
1127                  * that if lock can not be taken it is due to an improper lock
1128                  * of the semaphore.
1129                  */
1130                 mask = IGC_SWFW_EEP_SM;
1131                 if (hw->mac.ops.acquire_swfw_sync(hw, mask) < 0)
1132                         PMD_DRV_LOG(DEBUG, "SWFW common locks released");
1133
1134                 hw->mac.ops.release_swfw_sync(hw, mask);
1135         }
1136
1137         return IGC_SUCCESS;
1138 }
1139
1140 /*
1141  * free all rx/tx queues.
1142  */
1143 static void
1144 igc_dev_free_queues(struct rte_eth_dev *dev)
1145 {
1146         uint16_t i;
1147
1148         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1149                 eth_igc_rx_queue_release(dev, i);
1150                 dev->data->rx_queues[i] = NULL;
1151         }
1152         dev->data->nb_rx_queues = 0;
1153
1154         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1155                 eth_igc_tx_queue_release(dev, i);
1156                 dev->data->tx_queues[i] = NULL;
1157         }
1158         dev->data->nb_tx_queues = 0;
1159 }
1160
1161 static int
1162 eth_igc_close(struct rte_eth_dev *dev)
1163 {
1164         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1165         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1166         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1167         struct igc_adapter *adapter = IGC_DEV_PRIVATE(dev);
1168         int retry = 0;
1169         int ret = 0;
1170
1171         PMD_INIT_FUNC_TRACE();
1172         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1173                 return 0;
1174
1175         if (!adapter->stopped)
1176                 ret = eth_igc_stop(dev);
1177
1178         igc_flow_flush(dev, NULL);
1179         igc_clear_all_filter(dev);
1180
1181         igc_intr_other_disable(dev);
1182         do {
1183                 int ret = rte_intr_callback_unregister(intr_handle,
1184                                 eth_igc_interrupt_handler, dev);
1185                 if (ret >= 0 || ret == -ENOENT || ret == -EINVAL)
1186                         break;
1187
1188                 PMD_DRV_LOG(ERR, "intr callback unregister failed: %d", ret);
1189                 DELAY(200 * 1000); /* delay 200ms */
1190         } while (retry++ < 5);
1191
1192         igc_phy_hw_reset(hw);
1193         igc_hw_control_release(hw);
1194         igc_dev_free_queues(dev);
1195
1196         /* Reset any pending lock */
1197         igc_reset_swfw_lock(hw);
1198
1199         return ret;
1200 }
1201
1202 static void
1203 igc_identify_hardware(struct rte_eth_dev *dev, struct rte_pci_device *pci_dev)
1204 {
1205         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1206
1207         hw->vendor_id = pci_dev->id.vendor_id;
1208         hw->device_id = pci_dev->id.device_id;
1209         hw->subsystem_vendor_id = pci_dev->id.subsystem_vendor_id;
1210         hw->subsystem_device_id = pci_dev->id.subsystem_device_id;
1211 }
1212
1213 static int
1214 eth_igc_dev_init(struct rte_eth_dev *dev)
1215 {
1216         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1217         struct igc_adapter *igc = IGC_DEV_PRIVATE(dev);
1218         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1219         int i, error = 0;
1220
1221         PMD_INIT_FUNC_TRACE();
1222         dev->dev_ops = &eth_igc_ops;
1223         dev->rx_queue_count = eth_igc_rx_queue_count;
1224         dev->rx_descriptor_status = eth_igc_rx_descriptor_status;
1225         dev->tx_descriptor_status = eth_igc_tx_descriptor_status;
1226
1227         /*
1228          * for secondary processes, we don't initialize any further as primary
1229          * has already done this work. Only check we don't need a different
1230          * RX function.
1231          */
1232         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1233                 return 0;
1234
1235         rte_eth_copy_pci_info(dev, pci_dev);
1236         dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1237
1238         hw->back = pci_dev;
1239         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
1240
1241         igc_identify_hardware(dev, pci_dev);
1242         if (igc_setup_init_funcs(hw, false) != IGC_SUCCESS) {
1243                 error = -EIO;
1244                 goto err_late;
1245         }
1246
1247         igc_get_bus_info(hw);
1248
1249         /* Reset any pending lock */
1250         if (igc_reset_swfw_lock(hw) != IGC_SUCCESS) {
1251                 error = -EIO;
1252                 goto err_late;
1253         }
1254
1255         /* Finish initialization */
1256         if (igc_setup_init_funcs(hw, true) != IGC_SUCCESS) {
1257                 error = -EIO;
1258                 goto err_late;
1259         }
1260
1261         hw->mac.autoneg = 1;
1262         hw->phy.autoneg_wait_to_complete = 0;
1263         hw->phy.autoneg_advertised = IGC_ALL_SPEED_DUPLEX_2500;
1264
1265         /* Copper options */
1266         if (hw->phy.media_type == igc_media_type_copper) {
1267                 hw->phy.mdix = 0; /* AUTO_ALL_MODES */
1268                 hw->phy.disable_polarity_correction = 0;
1269                 hw->phy.ms_type = igc_ms_hw_default;
1270         }
1271
1272         /*
1273          * Start from a known state, this is important in reading the nvm
1274          * and mac from that.
1275          */
1276         igc_reset_hw(hw);
1277
1278         /* Make sure we have a good EEPROM before we read from it */
1279         if (igc_validate_nvm_checksum(hw) < 0) {
1280                 /*
1281                  * Some PCI-E parts fail the first check due to
1282                  * the link being in sleep state, call it again,
1283                  * if it fails a second time its a real issue.
1284                  */
1285                 if (igc_validate_nvm_checksum(hw) < 0) {
1286                         PMD_INIT_LOG(ERR, "EEPROM checksum invalid");
1287                         error = -EIO;
1288                         goto err_late;
1289                 }
1290         }
1291
1292         /* Read the permanent MAC address out of the EEPROM */
1293         if (igc_read_mac_addr(hw) != 0) {
1294                 PMD_INIT_LOG(ERR, "EEPROM error while reading MAC address");
1295                 error = -EIO;
1296                 goto err_late;
1297         }
1298
1299         /* Allocate memory for storing MAC addresses */
1300         dev->data->mac_addrs = rte_zmalloc("igc",
1301                 RTE_ETHER_ADDR_LEN * hw->mac.rar_entry_count, 0);
1302         if (dev->data->mac_addrs == NULL) {
1303                 PMD_INIT_LOG(ERR, "Failed to allocate %d bytes for storing MAC",
1304                                 RTE_ETHER_ADDR_LEN * hw->mac.rar_entry_count);
1305                 error = -ENOMEM;
1306                 goto err_late;
1307         }
1308
1309         /* Copy the permanent MAC address */
1310         rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.addr,
1311                         &dev->data->mac_addrs[0]);
1312
1313         /* Now initialize the hardware */
1314         if (igc_hardware_init(hw) != 0) {
1315                 PMD_INIT_LOG(ERR, "Hardware initialization failed");
1316                 rte_free(dev->data->mac_addrs);
1317                 dev->data->mac_addrs = NULL;
1318                 error = -ENODEV;
1319                 goto err_late;
1320         }
1321
1322         hw->mac.get_link_status = 1;
1323         igc->stopped = 0;
1324
1325         /* Indicate SOL/IDER usage */
1326         if (igc_check_reset_block(hw) < 0)
1327                 PMD_INIT_LOG(ERR,
1328                         "PHY reset is blocked due to SOL/IDER session.");
1329
1330         PMD_INIT_LOG(DEBUG, "port_id %d vendorID=0x%x deviceID=0x%x",
1331                         dev->data->port_id, pci_dev->id.vendor_id,
1332                         pci_dev->id.device_id);
1333
1334         rte_intr_callback_register(&pci_dev->intr_handle,
1335                         eth_igc_interrupt_handler, (void *)dev);
1336
1337         /* enable uio/vfio intr/eventfd mapping */
1338         rte_intr_enable(&pci_dev->intr_handle);
1339
1340         /* enable support intr */
1341         igc_intr_other_enable(dev);
1342
1343         /* initiate queue status */
1344         for (i = 0; i < IGC_QUEUE_PAIRS_NUM; i++) {
1345                 igc->txq_stats_map[i] = -1;
1346                 igc->rxq_stats_map[i] = -1;
1347         }
1348
1349         igc_flow_init(dev);
1350         igc_clear_all_filter(dev);
1351         return 0;
1352
1353 err_late:
1354         igc_hw_control_release(hw);
1355         return error;
1356 }
1357
1358 static int
1359 eth_igc_dev_uninit(__rte_unused struct rte_eth_dev *eth_dev)
1360 {
1361         PMD_INIT_FUNC_TRACE();
1362         eth_igc_close(eth_dev);
1363         return 0;
1364 }
1365
1366 static int
1367 eth_igc_reset(struct rte_eth_dev *dev)
1368 {
1369         int ret;
1370
1371         PMD_INIT_FUNC_TRACE();
1372
1373         ret = eth_igc_dev_uninit(dev);
1374         if (ret)
1375                 return ret;
1376
1377         return eth_igc_dev_init(dev);
1378 }
1379
1380 static int
1381 eth_igc_promiscuous_enable(struct rte_eth_dev *dev)
1382 {
1383         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1384         uint32_t rctl;
1385
1386         rctl = IGC_READ_REG(hw, IGC_RCTL);
1387         rctl |= (IGC_RCTL_UPE | IGC_RCTL_MPE);
1388         IGC_WRITE_REG(hw, IGC_RCTL, rctl);
1389         return 0;
1390 }
1391
1392 static int
1393 eth_igc_promiscuous_disable(struct rte_eth_dev *dev)
1394 {
1395         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1396         uint32_t rctl;
1397
1398         rctl = IGC_READ_REG(hw, IGC_RCTL);
1399         rctl &= (~IGC_RCTL_UPE);
1400         if (dev->data->all_multicast == 1)
1401                 rctl |= IGC_RCTL_MPE;
1402         else
1403                 rctl &= (~IGC_RCTL_MPE);
1404         IGC_WRITE_REG(hw, IGC_RCTL, rctl);
1405         return 0;
1406 }
1407
1408 static int
1409 eth_igc_allmulticast_enable(struct rte_eth_dev *dev)
1410 {
1411         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1412         uint32_t rctl;
1413
1414         rctl = IGC_READ_REG(hw, IGC_RCTL);
1415         rctl |= IGC_RCTL_MPE;
1416         IGC_WRITE_REG(hw, IGC_RCTL, rctl);
1417         return 0;
1418 }
1419
1420 static int
1421 eth_igc_allmulticast_disable(struct rte_eth_dev *dev)
1422 {
1423         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1424         uint32_t rctl;
1425
1426         if (dev->data->promiscuous == 1)
1427                 return 0;       /* must remain in all_multicast mode */
1428
1429         rctl = IGC_READ_REG(hw, IGC_RCTL);
1430         rctl &= (~IGC_RCTL_MPE);
1431         IGC_WRITE_REG(hw, IGC_RCTL, rctl);
1432         return 0;
1433 }
1434
1435 static int
1436 eth_igc_fw_version_get(struct rte_eth_dev *dev, char *fw_version,
1437                        size_t fw_size)
1438 {
1439         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1440         struct igc_fw_version fw;
1441         int ret;
1442
1443         igc_get_fw_version(hw, &fw);
1444
1445         /* if option rom is valid, display its version too */
1446         if (fw.or_valid) {
1447                 ret = snprintf(fw_version, fw_size,
1448                          "%d.%d, 0x%08x, %d.%d.%d",
1449                          fw.eep_major, fw.eep_minor, fw.etrack_id,
1450                          fw.or_major, fw.or_build, fw.or_patch);
1451         /* no option rom */
1452         } else {
1453                 if (fw.etrack_id != 0X0000) {
1454                         ret = snprintf(fw_version, fw_size,
1455                                  "%d.%d, 0x%08x",
1456                                  fw.eep_major, fw.eep_minor,
1457                                  fw.etrack_id);
1458                 } else {
1459                         ret = snprintf(fw_version, fw_size,
1460                                  "%d.%d.%d",
1461                                  fw.eep_major, fw.eep_minor,
1462                                  fw.eep_build);
1463                 }
1464         }
1465         if (ret < 0)
1466                 return -EINVAL;
1467
1468         ret += 1; /* add the size of '\0' */
1469         if (fw_size < (size_t)ret)
1470                 return ret;
1471         else
1472                 return 0;
1473 }
1474
1475 static int
1476 eth_igc_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
1477 {
1478         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1479
1480         dev_info->min_rx_bufsize = 256; /* See BSIZE field of RCTL register. */
1481         dev_info->max_rx_pktlen = MAX_RX_JUMBO_FRAME_SIZE;
1482         dev_info->max_mac_addrs = hw->mac.rar_entry_count;
1483         dev_info->rx_offload_capa = IGC_RX_OFFLOAD_ALL;
1484         dev_info->tx_offload_capa = IGC_TX_OFFLOAD_ALL;
1485         dev_info->rx_queue_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP;
1486
1487         dev_info->max_rx_queues = IGC_QUEUE_PAIRS_NUM;
1488         dev_info->max_tx_queues = IGC_QUEUE_PAIRS_NUM;
1489         dev_info->max_vmdq_pools = 0;
1490
1491         dev_info->hash_key_size = IGC_HKEY_MAX_INDEX * sizeof(uint32_t);
1492         dev_info->reta_size = ETH_RSS_RETA_SIZE_128;
1493         dev_info->flow_type_rss_offloads = IGC_RSS_OFFLOAD_ALL;
1494
1495         dev_info->default_rxconf = (struct rte_eth_rxconf) {
1496                 .rx_thresh = {
1497                         .pthresh = IGC_DEFAULT_RX_PTHRESH,
1498                         .hthresh = IGC_DEFAULT_RX_HTHRESH,
1499                         .wthresh = IGC_DEFAULT_RX_WTHRESH,
1500                 },
1501                 .rx_free_thresh = IGC_DEFAULT_RX_FREE_THRESH,
1502                 .rx_drop_en = 0,
1503                 .offloads = 0,
1504         };
1505
1506         dev_info->default_txconf = (struct rte_eth_txconf) {
1507                 .tx_thresh = {
1508                         .pthresh = IGC_DEFAULT_TX_PTHRESH,
1509                         .hthresh = IGC_DEFAULT_TX_HTHRESH,
1510                         .wthresh = IGC_DEFAULT_TX_WTHRESH,
1511                 },
1512                 .offloads = 0,
1513         };
1514
1515         dev_info->rx_desc_lim = rx_desc_lim;
1516         dev_info->tx_desc_lim = tx_desc_lim;
1517
1518         dev_info->speed_capa = ETH_LINK_SPEED_10M_HD | ETH_LINK_SPEED_10M |
1519                         ETH_LINK_SPEED_100M_HD | ETH_LINK_SPEED_100M |
1520                         ETH_LINK_SPEED_1G | ETH_LINK_SPEED_2_5G;
1521
1522         dev_info->max_mtu = dev_info->max_rx_pktlen - IGC_ETH_OVERHEAD;
1523         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
1524         return 0;
1525 }
1526
1527 static int
1528 eth_igc_led_on(struct rte_eth_dev *dev)
1529 {
1530         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1531
1532         return igc_led_on(hw) == IGC_SUCCESS ? 0 : -ENOTSUP;
1533 }
1534
1535 static int
1536 eth_igc_led_off(struct rte_eth_dev *dev)
1537 {
1538         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1539
1540         return igc_led_off(hw) == IGC_SUCCESS ? 0 : -ENOTSUP;
1541 }
1542
1543 static const uint32_t *
1544 eth_igc_supported_ptypes_get(__rte_unused struct rte_eth_dev *dev)
1545 {
1546         static const uint32_t ptypes[] = {
1547                 /* refers to rx_desc_pkt_info_to_pkt_type() */
1548                 RTE_PTYPE_L2_ETHER,
1549                 RTE_PTYPE_L3_IPV4,
1550                 RTE_PTYPE_L3_IPV4_EXT,
1551                 RTE_PTYPE_L3_IPV6,
1552                 RTE_PTYPE_L3_IPV6_EXT,
1553                 RTE_PTYPE_L4_TCP,
1554                 RTE_PTYPE_L4_UDP,
1555                 RTE_PTYPE_L4_SCTP,
1556                 RTE_PTYPE_TUNNEL_IP,
1557                 RTE_PTYPE_INNER_L3_IPV6,
1558                 RTE_PTYPE_INNER_L3_IPV6_EXT,
1559                 RTE_PTYPE_INNER_L4_TCP,
1560                 RTE_PTYPE_INNER_L4_UDP,
1561                 RTE_PTYPE_UNKNOWN
1562         };
1563
1564         return ptypes;
1565 }
1566
1567 static int
1568 eth_igc_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1569 {
1570         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1571         uint32_t frame_size = mtu + IGC_ETH_OVERHEAD;
1572         uint32_t rctl;
1573
1574         /* if extend vlan has been enabled */
1575         if (IGC_READ_REG(hw, IGC_CTRL_EXT) & IGC_CTRL_EXT_EXT_VLAN)
1576                 frame_size += VLAN_TAG_SIZE;
1577
1578         /* check that mtu is within the allowed range */
1579         if (mtu < RTE_ETHER_MIN_MTU ||
1580                 frame_size > MAX_RX_JUMBO_FRAME_SIZE)
1581                 return -EINVAL;
1582
1583         /*
1584          * If device is started, refuse mtu that requires the support of
1585          * scattered packets when this feature has not been enabled before.
1586          */
1587         if (dev->data->dev_started && !dev->data->scattered_rx &&
1588             frame_size > dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM) {
1589                 PMD_INIT_LOG(ERR, "Stop port first.");
1590                 return -EINVAL;
1591         }
1592
1593         rctl = IGC_READ_REG(hw, IGC_RCTL);
1594
1595         /* switch to jumbo mode if needed */
1596         if (mtu > RTE_ETHER_MTU) {
1597                 dev->data->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1598                 rctl |= IGC_RCTL_LPE;
1599         } else {
1600                 dev->data->dev_conf.rxmode.offloads &= ~DEV_RX_OFFLOAD_JUMBO_FRAME;
1601                 rctl &= ~IGC_RCTL_LPE;
1602         }
1603         IGC_WRITE_REG(hw, IGC_RCTL, rctl);
1604
1605         IGC_WRITE_REG(hw, IGC_RLPML, frame_size);
1606
1607         return 0;
1608 }
1609
1610 static int
1611 eth_igc_rar_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
1612                 uint32_t index, uint32_t pool)
1613 {
1614         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1615
1616         igc_rar_set(hw, mac_addr->addr_bytes, index);
1617         RTE_SET_USED(pool);
1618         return 0;
1619 }
1620
1621 static void
1622 eth_igc_rar_clear(struct rte_eth_dev *dev, uint32_t index)
1623 {
1624         uint8_t addr[RTE_ETHER_ADDR_LEN];
1625         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1626
1627         memset(addr, 0, sizeof(addr));
1628         igc_rar_set(hw, addr, index);
1629 }
1630
1631 static int
1632 eth_igc_default_mac_addr_set(struct rte_eth_dev *dev,
1633                         struct rte_ether_addr *addr)
1634 {
1635         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1636         igc_rar_set(hw, addr->addr_bytes, 0);
1637         return 0;
1638 }
1639
1640 static int
1641 eth_igc_set_mc_addr_list(struct rte_eth_dev *dev,
1642                          struct rte_ether_addr *mc_addr_set,
1643                          uint32_t nb_mc_addr)
1644 {
1645         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1646         igc_update_mc_addr_list(hw, (u8 *)mc_addr_set, nb_mc_addr);
1647         return 0;
1648 }
1649
1650 /*
1651  * Read hardware registers
1652  */
1653 static void
1654 igc_read_stats_registers(struct igc_hw *hw, struct igc_hw_stats *stats)
1655 {
1656         int pause_frames;
1657
1658         uint64_t old_gprc  = stats->gprc;
1659         uint64_t old_gptc  = stats->gptc;
1660         uint64_t old_tpr   = stats->tpr;
1661         uint64_t old_tpt   = stats->tpt;
1662         uint64_t old_rpthc = stats->rpthc;
1663         uint64_t old_hgptc = stats->hgptc;
1664
1665         stats->crcerrs += IGC_READ_REG(hw, IGC_CRCERRS);
1666         stats->algnerrc += IGC_READ_REG(hw, IGC_ALGNERRC);
1667         stats->rxerrc += IGC_READ_REG(hw, IGC_RXERRC);
1668         stats->mpc += IGC_READ_REG(hw, IGC_MPC);
1669         stats->scc += IGC_READ_REG(hw, IGC_SCC);
1670         stats->ecol += IGC_READ_REG(hw, IGC_ECOL);
1671
1672         stats->mcc += IGC_READ_REG(hw, IGC_MCC);
1673         stats->latecol += IGC_READ_REG(hw, IGC_LATECOL);
1674         stats->colc += IGC_READ_REG(hw, IGC_COLC);
1675
1676         stats->dc += IGC_READ_REG(hw, IGC_DC);
1677         stats->tncrs += IGC_READ_REG(hw, IGC_TNCRS);
1678         stats->htdpmc += IGC_READ_REG(hw, IGC_HTDPMC);
1679         stats->rlec += IGC_READ_REG(hw, IGC_RLEC);
1680         stats->xonrxc += IGC_READ_REG(hw, IGC_XONRXC);
1681         stats->xontxc += IGC_READ_REG(hw, IGC_XONTXC);
1682
1683         /*
1684          * For watchdog management we need to know if we have been
1685          * paused during the last interval, so capture that here.
1686          */
1687         pause_frames = IGC_READ_REG(hw, IGC_XOFFRXC);
1688         stats->xoffrxc += pause_frames;
1689         stats->xofftxc += IGC_READ_REG(hw, IGC_XOFFTXC);
1690         stats->fcruc += IGC_READ_REG(hw, IGC_FCRUC);
1691         stats->prc64 += IGC_READ_REG(hw, IGC_PRC64);
1692         stats->prc127 += IGC_READ_REG(hw, IGC_PRC127);
1693         stats->prc255 += IGC_READ_REG(hw, IGC_PRC255);
1694         stats->prc511 += IGC_READ_REG(hw, IGC_PRC511);
1695         stats->prc1023 += IGC_READ_REG(hw, IGC_PRC1023);
1696         stats->prc1522 += IGC_READ_REG(hw, IGC_PRC1522);
1697         stats->gprc += IGC_READ_REG(hw, IGC_GPRC);
1698         stats->bprc += IGC_READ_REG(hw, IGC_BPRC);
1699         stats->mprc += IGC_READ_REG(hw, IGC_MPRC);
1700         stats->gptc += IGC_READ_REG(hw, IGC_GPTC);
1701
1702         /* For the 64-bit byte counters the low dword must be read first. */
1703         /* Both registers clear on the read of the high dword */
1704
1705         /* Workaround CRC bytes included in size, take away 4 bytes/packet */
1706         stats->gorc += IGC_READ_REG(hw, IGC_GORCL);
1707         stats->gorc += ((uint64_t)IGC_READ_REG(hw, IGC_GORCH) << 32);
1708         stats->gorc -= (stats->gprc - old_gprc) * RTE_ETHER_CRC_LEN;
1709         stats->gotc += IGC_READ_REG(hw, IGC_GOTCL);
1710         stats->gotc += ((uint64_t)IGC_READ_REG(hw, IGC_GOTCH) << 32);
1711         stats->gotc -= (stats->gptc - old_gptc) * RTE_ETHER_CRC_LEN;
1712
1713         stats->rnbc += IGC_READ_REG(hw, IGC_RNBC);
1714         stats->ruc += IGC_READ_REG(hw, IGC_RUC);
1715         stats->rfc += IGC_READ_REG(hw, IGC_RFC);
1716         stats->roc += IGC_READ_REG(hw, IGC_ROC);
1717         stats->rjc += IGC_READ_REG(hw, IGC_RJC);
1718
1719         stats->mgprc += IGC_READ_REG(hw, IGC_MGTPRC);
1720         stats->mgpdc += IGC_READ_REG(hw, IGC_MGTPDC);
1721         stats->mgptc += IGC_READ_REG(hw, IGC_MGTPTC);
1722         stats->b2ospc += IGC_READ_REG(hw, IGC_B2OSPC);
1723         stats->b2ogprc += IGC_READ_REG(hw, IGC_B2OGPRC);
1724         stats->o2bgptc += IGC_READ_REG(hw, IGC_O2BGPTC);
1725         stats->o2bspc += IGC_READ_REG(hw, IGC_O2BSPC);
1726
1727         stats->tpr += IGC_READ_REG(hw, IGC_TPR);
1728         stats->tpt += IGC_READ_REG(hw, IGC_TPT);
1729
1730         stats->tor += IGC_READ_REG(hw, IGC_TORL);
1731         stats->tor += ((uint64_t)IGC_READ_REG(hw, IGC_TORH) << 32);
1732         stats->tor -= (stats->tpr - old_tpr) * RTE_ETHER_CRC_LEN;
1733         stats->tot += IGC_READ_REG(hw, IGC_TOTL);
1734         stats->tot += ((uint64_t)IGC_READ_REG(hw, IGC_TOTH) << 32);
1735         stats->tot -= (stats->tpt - old_tpt) * RTE_ETHER_CRC_LEN;
1736
1737         stats->ptc64 += IGC_READ_REG(hw, IGC_PTC64);
1738         stats->ptc127 += IGC_READ_REG(hw, IGC_PTC127);
1739         stats->ptc255 += IGC_READ_REG(hw, IGC_PTC255);
1740         stats->ptc511 += IGC_READ_REG(hw, IGC_PTC511);
1741         stats->ptc1023 += IGC_READ_REG(hw, IGC_PTC1023);
1742         stats->ptc1522 += IGC_READ_REG(hw, IGC_PTC1522);
1743         stats->mptc += IGC_READ_REG(hw, IGC_MPTC);
1744         stats->bptc += IGC_READ_REG(hw, IGC_BPTC);
1745         stats->tsctc += IGC_READ_REG(hw, IGC_TSCTC);
1746
1747         stats->iac += IGC_READ_REG(hw, IGC_IAC);
1748         stats->rpthc += IGC_READ_REG(hw, IGC_RPTHC);
1749         stats->hgptc += IGC_READ_REG(hw, IGC_HGPTC);
1750         stats->icrxdmtc += IGC_READ_REG(hw, IGC_ICRXDMTC);
1751
1752         /* Host to Card Statistics */
1753         stats->hgorc += IGC_READ_REG(hw, IGC_HGORCL);
1754         stats->hgorc += ((uint64_t)IGC_READ_REG(hw, IGC_HGORCH) << 32);
1755         stats->hgorc -= (stats->rpthc - old_rpthc) * RTE_ETHER_CRC_LEN;
1756         stats->hgotc += IGC_READ_REG(hw, IGC_HGOTCL);
1757         stats->hgotc += ((uint64_t)IGC_READ_REG(hw, IGC_HGOTCH) << 32);
1758         stats->hgotc -= (stats->hgptc - old_hgptc) * RTE_ETHER_CRC_LEN;
1759         stats->lenerrs += IGC_READ_REG(hw, IGC_LENERRS);
1760 }
1761
1762 /*
1763  * Write 0 to all queue status registers
1764  */
1765 static void
1766 igc_reset_queue_stats_register(struct igc_hw *hw)
1767 {
1768         int i;
1769
1770         for (i = 0; i < IGC_QUEUE_PAIRS_NUM; i++) {
1771                 IGC_WRITE_REG(hw, IGC_PQGPRC(i), 0);
1772                 IGC_WRITE_REG(hw, IGC_PQGPTC(i), 0);
1773                 IGC_WRITE_REG(hw, IGC_PQGORC(i), 0);
1774                 IGC_WRITE_REG(hw, IGC_PQGOTC(i), 0);
1775                 IGC_WRITE_REG(hw, IGC_PQMPRC(i), 0);
1776                 IGC_WRITE_REG(hw, IGC_RQDPC(i), 0);
1777                 IGC_WRITE_REG(hw, IGC_TQDPC(i), 0);
1778         }
1779 }
1780
1781 /*
1782  * Read all hardware queue status registers
1783  */
1784 static void
1785 igc_read_queue_stats_register(struct rte_eth_dev *dev)
1786 {
1787         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1788         struct igc_hw_queue_stats *queue_stats =
1789                                 IGC_DEV_PRIVATE_QUEUE_STATS(dev);
1790         int i;
1791
1792         /*
1793          * This register is not cleared on read. Furthermore, the register wraps
1794          * around back to 0x00000000 on the next increment when reaching a value
1795          * of 0xFFFFFFFF and then continues normal count operation.
1796          */
1797         for (i = 0; i < IGC_QUEUE_PAIRS_NUM; i++) {
1798                 union {
1799                         u64 ddword;
1800                         u32 dword[2];
1801                 } value;
1802                 u32 tmp;
1803
1804                 /*
1805                  * Read the register first, if the value is smaller than that
1806                  * previous read, that mean the register has been overflowed,
1807                  * then we add the high 4 bytes by 1 and replace the low 4
1808                  * bytes by the new value.
1809                  */
1810                 tmp = IGC_READ_REG(hw, IGC_PQGPRC(i));
1811                 value.ddword = queue_stats->pqgprc[i];
1812                 if (value.dword[U32_0_IN_U64] > tmp)
1813                         value.dword[U32_1_IN_U64]++;
1814                 value.dword[U32_0_IN_U64] = tmp;
1815                 queue_stats->pqgprc[i] = value.ddword;
1816
1817                 tmp = IGC_READ_REG(hw, IGC_PQGPTC(i));
1818                 value.ddword = queue_stats->pqgptc[i];
1819                 if (value.dword[U32_0_IN_U64] > tmp)
1820                         value.dword[U32_1_IN_U64]++;
1821                 value.dword[U32_0_IN_U64] = tmp;
1822                 queue_stats->pqgptc[i] = value.ddword;
1823
1824                 tmp = IGC_READ_REG(hw, IGC_PQGORC(i));
1825                 value.ddword = queue_stats->pqgorc[i];
1826                 if (value.dword[U32_0_IN_U64] > tmp)
1827                         value.dword[U32_1_IN_U64]++;
1828                 value.dword[U32_0_IN_U64] = tmp;
1829                 queue_stats->pqgorc[i] = value.ddword;
1830
1831                 tmp = IGC_READ_REG(hw, IGC_PQGOTC(i));
1832                 value.ddword = queue_stats->pqgotc[i];
1833                 if (value.dword[U32_0_IN_U64] > tmp)
1834                         value.dword[U32_1_IN_U64]++;
1835                 value.dword[U32_0_IN_U64] = tmp;
1836                 queue_stats->pqgotc[i] = value.ddword;
1837
1838                 tmp = IGC_READ_REG(hw, IGC_PQMPRC(i));
1839                 value.ddword = queue_stats->pqmprc[i];
1840                 if (value.dword[U32_0_IN_U64] > tmp)
1841                         value.dword[U32_1_IN_U64]++;
1842                 value.dword[U32_0_IN_U64] = tmp;
1843                 queue_stats->pqmprc[i] = value.ddword;
1844
1845                 tmp = IGC_READ_REG(hw, IGC_RQDPC(i));
1846                 value.ddword = queue_stats->rqdpc[i];
1847                 if (value.dword[U32_0_IN_U64] > tmp)
1848                         value.dword[U32_1_IN_U64]++;
1849                 value.dword[U32_0_IN_U64] = tmp;
1850                 queue_stats->rqdpc[i] = value.ddword;
1851
1852                 tmp = IGC_READ_REG(hw, IGC_TQDPC(i));
1853                 value.ddword = queue_stats->tqdpc[i];
1854                 if (value.dword[U32_0_IN_U64] > tmp)
1855                         value.dword[U32_1_IN_U64]++;
1856                 value.dword[U32_0_IN_U64] = tmp;
1857                 queue_stats->tqdpc[i] = value.ddword;
1858         }
1859 }
1860
1861 static int
1862 eth_igc_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *rte_stats)
1863 {
1864         struct igc_adapter *igc = IGC_DEV_PRIVATE(dev);
1865         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1866         struct igc_hw_stats *stats = IGC_DEV_PRIVATE_STATS(dev);
1867         struct igc_hw_queue_stats *queue_stats =
1868                         IGC_DEV_PRIVATE_QUEUE_STATS(dev);
1869         int i;
1870
1871         /*
1872          * Cancel status handler since it will read the queue status registers
1873          */
1874         rte_eal_alarm_cancel(igc_update_queue_stats_handler, dev);
1875
1876         /* Read status register */
1877         igc_read_queue_stats_register(dev);
1878         igc_read_stats_registers(hw, stats);
1879
1880         if (rte_stats == NULL) {
1881                 /* Restart queue status handler */
1882                 rte_eal_alarm_set(IGC_ALARM_INTERVAL,
1883                                 igc_update_queue_stats_handler, dev);
1884                 return -EINVAL;
1885         }
1886
1887         /* Rx Errors */
1888         rte_stats->imissed = stats->mpc;
1889         rte_stats->ierrors = stats->crcerrs + stats->rlec +
1890                         stats->rxerrc + stats->algnerrc;
1891
1892         /* Tx Errors */
1893         rte_stats->oerrors = stats->ecol + stats->latecol;
1894
1895         rte_stats->ipackets = stats->gprc;
1896         rte_stats->opackets = stats->gptc;
1897         rte_stats->ibytes   = stats->gorc;
1898         rte_stats->obytes   = stats->gotc;
1899
1900         /* Get per-queue statuses */
1901         for (i = 0; i < IGC_QUEUE_PAIRS_NUM; i++) {
1902                 /* GET TX queue statuses */
1903                 int map_id = igc->txq_stats_map[i];
1904                 if (map_id >= 0) {
1905                         rte_stats->q_opackets[map_id] += queue_stats->pqgptc[i];
1906                         rte_stats->q_obytes[map_id] += queue_stats->pqgotc[i];
1907                 }
1908                 /* Get RX queue statuses */
1909                 map_id = igc->rxq_stats_map[i];
1910                 if (map_id >= 0) {
1911                         rte_stats->q_ipackets[map_id] += queue_stats->pqgprc[i];
1912                         rte_stats->q_ibytes[map_id] += queue_stats->pqgorc[i];
1913                         rte_stats->q_errors[map_id] += queue_stats->rqdpc[i];
1914                 }
1915         }
1916
1917         /* Restart queue status handler */
1918         rte_eal_alarm_set(IGC_ALARM_INTERVAL,
1919                         igc_update_queue_stats_handler, dev);
1920         return 0;
1921 }
1922
1923 static int
1924 eth_igc_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
1925                    unsigned int n)
1926 {
1927         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1928         struct igc_hw_stats *hw_stats =
1929                         IGC_DEV_PRIVATE_STATS(dev);
1930         unsigned int i;
1931
1932         igc_read_stats_registers(hw, hw_stats);
1933
1934         if (n < IGC_NB_XSTATS)
1935                 return IGC_NB_XSTATS;
1936
1937         /* If this is a reset xstats is NULL, and we have cleared the
1938          * registers by reading them.
1939          */
1940         if (!xstats)
1941                 return 0;
1942
1943         /* Extended stats */
1944         for (i = 0; i < IGC_NB_XSTATS; i++) {
1945                 xstats[i].id = i;
1946                 xstats[i].value = *(uint64_t *)(((char *)hw_stats) +
1947                         rte_igc_stats_strings[i].offset);
1948         }
1949
1950         return IGC_NB_XSTATS;
1951 }
1952
1953 static int
1954 eth_igc_xstats_reset(struct rte_eth_dev *dev)
1955 {
1956         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
1957         struct igc_hw_stats *hw_stats = IGC_DEV_PRIVATE_STATS(dev);
1958         struct igc_hw_queue_stats *queue_stats =
1959                         IGC_DEV_PRIVATE_QUEUE_STATS(dev);
1960
1961         /* Cancel queue status handler for avoid conflict */
1962         rte_eal_alarm_cancel(igc_update_queue_stats_handler, dev);
1963
1964         /* HW registers are cleared on read */
1965         igc_reset_queue_stats_register(hw);
1966         igc_read_stats_registers(hw, hw_stats);
1967
1968         /* Reset software totals */
1969         memset(hw_stats, 0, sizeof(*hw_stats));
1970         memset(queue_stats, 0, sizeof(*queue_stats));
1971
1972         /* Restart the queue status handler */
1973         rte_eal_alarm_set(IGC_ALARM_INTERVAL, igc_update_queue_stats_handler,
1974                         dev);
1975
1976         return 0;
1977 }
1978
1979 static int
1980 eth_igc_xstats_get_names(__rte_unused struct rte_eth_dev *dev,
1981         struct rte_eth_xstat_name *xstats_names, unsigned int size)
1982 {
1983         unsigned int i;
1984
1985         if (xstats_names == NULL)
1986                 return IGC_NB_XSTATS;
1987
1988         if (size < IGC_NB_XSTATS) {
1989                 PMD_DRV_LOG(ERR, "not enough buffers!");
1990                 return IGC_NB_XSTATS;
1991         }
1992
1993         for (i = 0; i < IGC_NB_XSTATS; i++)
1994                 strlcpy(xstats_names[i].name, rte_igc_stats_strings[i].name,
1995                         sizeof(xstats_names[i].name));
1996
1997         return IGC_NB_XSTATS;
1998 }
1999
2000 static int
2001 eth_igc_xstats_get_names_by_id(struct rte_eth_dev *dev,
2002                 const uint64_t *ids, struct rte_eth_xstat_name *xstats_names,
2003                 unsigned int limit)
2004 {
2005         unsigned int i;
2006
2007         if (!ids)
2008                 return eth_igc_xstats_get_names(dev, xstats_names, limit);
2009
2010         for (i = 0; i < limit; i++) {
2011                 if (ids[i] >= IGC_NB_XSTATS) {
2012                         PMD_DRV_LOG(ERR, "id value isn't valid");
2013                         return -EINVAL;
2014                 }
2015                 strlcpy(xstats_names[i].name,
2016                         rte_igc_stats_strings[ids[i]].name,
2017                         sizeof(xstats_names[i].name));
2018         }
2019         return limit;
2020 }
2021
2022 static int
2023 eth_igc_xstats_get_by_id(struct rte_eth_dev *dev, const uint64_t *ids,
2024                 uint64_t *values, unsigned int n)
2025 {
2026         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2027         struct igc_hw_stats *hw_stats = IGC_DEV_PRIVATE_STATS(dev);
2028         unsigned int i;
2029
2030         igc_read_stats_registers(hw, hw_stats);
2031
2032         if (!ids) {
2033                 if (n < IGC_NB_XSTATS)
2034                         return IGC_NB_XSTATS;
2035
2036                 /* If this is a reset xstats is NULL, and we have cleared the
2037                  * registers by reading them.
2038                  */
2039                 if (!values)
2040                         return 0;
2041
2042                 /* Extended stats */
2043                 for (i = 0; i < IGC_NB_XSTATS; i++)
2044                         values[i] = *(uint64_t *)(((char *)hw_stats) +
2045                                         rte_igc_stats_strings[i].offset);
2046
2047                 return IGC_NB_XSTATS;
2048
2049         } else {
2050                 for (i = 0; i < n; i++) {
2051                         if (ids[i] >= IGC_NB_XSTATS) {
2052                                 PMD_DRV_LOG(ERR, "id value isn't valid");
2053                                 return -EINVAL;
2054                         }
2055                         values[i] = *(uint64_t *)(((char *)hw_stats) +
2056                                         rte_igc_stats_strings[ids[i]].offset);
2057                 }
2058                 return n;
2059         }
2060 }
2061
2062 static int
2063 eth_igc_queue_stats_mapping_set(struct rte_eth_dev *dev,
2064                 uint16_t queue_id, uint8_t stat_idx, uint8_t is_rx)
2065 {
2066         struct igc_adapter *igc = IGC_DEV_PRIVATE(dev);
2067
2068         /* check queue id is valid */
2069         if (queue_id >= IGC_QUEUE_PAIRS_NUM) {
2070                 PMD_DRV_LOG(ERR, "queue id(%u) error, max is %u",
2071                         queue_id, IGC_QUEUE_PAIRS_NUM - 1);
2072                 return -EINVAL;
2073         }
2074
2075         /* store the mapping status id */
2076         if (is_rx)
2077                 igc->rxq_stats_map[queue_id] = stat_idx;
2078         else
2079                 igc->txq_stats_map[queue_id] = stat_idx;
2080
2081         return 0;
2082 }
2083
2084 static int
2085 eth_igc_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
2086 {
2087         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2088         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2089         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2090         uint32_t vec = IGC_MISC_VEC_ID;
2091
2092         if (rte_intr_allow_others(intr_handle))
2093                 vec = IGC_RX_VEC_START;
2094
2095         uint32_t mask = 1u << (queue_id + vec);
2096
2097         IGC_WRITE_REG(hw, IGC_EIMC, mask);
2098         IGC_WRITE_FLUSH(hw);
2099
2100         return 0;
2101 }
2102
2103 static int
2104 eth_igc_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
2105 {
2106         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2107         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2108         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2109         uint32_t vec = IGC_MISC_VEC_ID;
2110
2111         if (rte_intr_allow_others(intr_handle))
2112                 vec = IGC_RX_VEC_START;
2113
2114         uint32_t mask = 1u << (queue_id + vec);
2115
2116         IGC_WRITE_REG(hw, IGC_EIMS, mask);
2117         IGC_WRITE_FLUSH(hw);
2118
2119         rte_intr_enable(intr_handle);
2120
2121         return 0;
2122 }
2123
2124 static int
2125 eth_igc_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2126 {
2127         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2128         uint32_t ctrl;
2129         int tx_pause;
2130         int rx_pause;
2131
2132         fc_conf->pause_time = hw->fc.pause_time;
2133         fc_conf->high_water = hw->fc.high_water;
2134         fc_conf->low_water = hw->fc.low_water;
2135         fc_conf->send_xon = hw->fc.send_xon;
2136         fc_conf->autoneg = hw->mac.autoneg;
2137
2138         /*
2139          * Return rx_pause and tx_pause status according to actual setting of
2140          * the TFCE and RFCE bits in the CTRL register.
2141          */
2142         ctrl = IGC_READ_REG(hw, IGC_CTRL);
2143         if (ctrl & IGC_CTRL_TFCE)
2144                 tx_pause = 1;
2145         else
2146                 tx_pause = 0;
2147
2148         if (ctrl & IGC_CTRL_RFCE)
2149                 rx_pause = 1;
2150         else
2151                 rx_pause = 0;
2152
2153         if (rx_pause && tx_pause)
2154                 fc_conf->mode = RTE_FC_FULL;
2155         else if (rx_pause)
2156                 fc_conf->mode = RTE_FC_RX_PAUSE;
2157         else if (tx_pause)
2158                 fc_conf->mode = RTE_FC_TX_PAUSE;
2159         else
2160                 fc_conf->mode = RTE_FC_NONE;
2161
2162         return 0;
2163 }
2164
2165 static int
2166 eth_igc_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2167 {
2168         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2169         uint32_t rx_buf_size;
2170         uint32_t max_high_water;
2171         uint32_t rctl;
2172         int err;
2173
2174         if (fc_conf->autoneg != hw->mac.autoneg)
2175                 return -ENOTSUP;
2176
2177         rx_buf_size = igc_get_rx_buffer_size(hw);
2178         PMD_DRV_LOG(DEBUG, "Rx packet buffer size = 0x%x", rx_buf_size);
2179
2180         /* At least reserve one Ethernet frame for watermark */
2181         max_high_water = rx_buf_size - RTE_ETHER_MAX_LEN;
2182         if (fc_conf->high_water > max_high_water ||
2183                 fc_conf->high_water < fc_conf->low_water) {
2184                 PMD_DRV_LOG(ERR,
2185                         "Incorrect high(%u)/low(%u) water value, max is %u",
2186                         fc_conf->high_water, fc_conf->low_water,
2187                         max_high_water);
2188                 return -EINVAL;
2189         }
2190
2191         switch (fc_conf->mode) {
2192         case RTE_FC_NONE:
2193                 hw->fc.requested_mode = igc_fc_none;
2194                 break;
2195         case RTE_FC_RX_PAUSE:
2196                 hw->fc.requested_mode = igc_fc_rx_pause;
2197                 break;
2198         case RTE_FC_TX_PAUSE:
2199                 hw->fc.requested_mode = igc_fc_tx_pause;
2200                 break;
2201         case RTE_FC_FULL:
2202                 hw->fc.requested_mode = igc_fc_full;
2203                 break;
2204         default:
2205                 PMD_DRV_LOG(ERR, "unsupported fc mode: %u", fc_conf->mode);
2206                 return -EINVAL;
2207         }
2208
2209         hw->fc.pause_time     = fc_conf->pause_time;
2210         hw->fc.high_water     = fc_conf->high_water;
2211         hw->fc.low_water      = fc_conf->low_water;
2212         hw->fc.send_xon       = fc_conf->send_xon;
2213
2214         err = igc_setup_link_generic(hw);
2215         if (err == IGC_SUCCESS) {
2216                 /**
2217                  * check if we want to forward MAC frames - driver doesn't have
2218                  * native capability to do that, so we'll write the registers
2219                  * ourselves
2220                  **/
2221                 rctl = IGC_READ_REG(hw, IGC_RCTL);
2222
2223                 /* set or clear MFLCN.PMCF bit depending on configuration */
2224                 if (fc_conf->mac_ctrl_frame_fwd != 0)
2225                         rctl |= IGC_RCTL_PMCF;
2226                 else
2227                         rctl &= ~IGC_RCTL_PMCF;
2228
2229                 IGC_WRITE_REG(hw, IGC_RCTL, rctl);
2230                 IGC_WRITE_FLUSH(hw);
2231
2232                 return 0;
2233         }
2234
2235         PMD_DRV_LOG(ERR, "igc_setup_link_generic = 0x%x", err);
2236         return -EIO;
2237 }
2238
2239 static int
2240 eth_igc_rss_reta_update(struct rte_eth_dev *dev,
2241                         struct rte_eth_rss_reta_entry64 *reta_conf,
2242                         uint16_t reta_size)
2243 {
2244         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2245         uint16_t i;
2246
2247         if (reta_size != ETH_RSS_RETA_SIZE_128) {
2248                 PMD_DRV_LOG(ERR,
2249                         "The size of RSS redirection table configured(%d) doesn't match the number hardware can supported(%d)",
2250                         reta_size, ETH_RSS_RETA_SIZE_128);
2251                 return -EINVAL;
2252         }
2253
2254         RTE_BUILD_BUG_ON(ETH_RSS_RETA_SIZE_128 % IGC_RSS_RDT_REG_SIZE);
2255
2256         /* set redirection table */
2257         for (i = 0; i < ETH_RSS_RETA_SIZE_128; i += IGC_RSS_RDT_REG_SIZE) {
2258                 union igc_rss_reta_reg reta, reg;
2259                 uint16_t idx, shift;
2260                 uint8_t j, mask;
2261
2262                 idx = i / RTE_RETA_GROUP_SIZE;
2263                 shift = i % RTE_RETA_GROUP_SIZE;
2264                 mask = (uint8_t)((reta_conf[idx].mask >> shift) &
2265                                 IGC_RSS_RDT_REG_SIZE_MASK);
2266
2267                 /* if no need to update the register */
2268                 if (!mask ||
2269                     shift > (RTE_RETA_GROUP_SIZE - IGC_RSS_RDT_REG_SIZE))
2270                         continue;
2271
2272                 /* check mask whether need to read the register value first */
2273                 if (mask == IGC_RSS_RDT_REG_SIZE_MASK)
2274                         reg.dword = 0;
2275                 else
2276                         reg.dword = IGC_READ_REG_LE_VALUE(hw,
2277                                         IGC_RETA(i / IGC_RSS_RDT_REG_SIZE));
2278
2279                 /* update the register */
2280                 RTE_BUILD_BUG_ON(sizeof(reta.bytes) != IGC_RSS_RDT_REG_SIZE);
2281                 for (j = 0; j < IGC_RSS_RDT_REG_SIZE; j++) {
2282                         if (mask & (1u << j))
2283                                 reta.bytes[j] =
2284                                         (uint8_t)reta_conf[idx].reta[shift + j];
2285                         else
2286                                 reta.bytes[j] = reg.bytes[j];
2287                 }
2288                 IGC_WRITE_REG_LE_VALUE(hw,
2289                         IGC_RETA(i / IGC_RSS_RDT_REG_SIZE), reta.dword);
2290         }
2291
2292         return 0;
2293 }
2294
2295 static int
2296 eth_igc_rss_reta_query(struct rte_eth_dev *dev,
2297                        struct rte_eth_rss_reta_entry64 *reta_conf,
2298                        uint16_t reta_size)
2299 {
2300         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2301         uint16_t i;
2302
2303         if (reta_size != ETH_RSS_RETA_SIZE_128) {
2304                 PMD_DRV_LOG(ERR,
2305                         "The size of RSS redirection table configured(%d) doesn't match the number hardware can supported(%d)",
2306                         reta_size, ETH_RSS_RETA_SIZE_128);
2307                 return -EINVAL;
2308         }
2309
2310         RTE_BUILD_BUG_ON(ETH_RSS_RETA_SIZE_128 % IGC_RSS_RDT_REG_SIZE);
2311
2312         /* read redirection table */
2313         for (i = 0; i < ETH_RSS_RETA_SIZE_128; i += IGC_RSS_RDT_REG_SIZE) {
2314                 union igc_rss_reta_reg reta;
2315                 uint16_t idx, shift;
2316                 uint8_t j, mask;
2317
2318                 idx = i / RTE_RETA_GROUP_SIZE;
2319                 shift = i % RTE_RETA_GROUP_SIZE;
2320                 mask = (uint8_t)((reta_conf[idx].mask >> shift) &
2321                                 IGC_RSS_RDT_REG_SIZE_MASK);
2322
2323                 /* if no need to read register */
2324                 if (!mask ||
2325                     shift > (RTE_RETA_GROUP_SIZE - IGC_RSS_RDT_REG_SIZE))
2326                         continue;
2327
2328                 /* read register and get the queue index */
2329                 RTE_BUILD_BUG_ON(sizeof(reta.bytes) != IGC_RSS_RDT_REG_SIZE);
2330                 reta.dword = IGC_READ_REG_LE_VALUE(hw,
2331                                 IGC_RETA(i / IGC_RSS_RDT_REG_SIZE));
2332                 for (j = 0; j < IGC_RSS_RDT_REG_SIZE; j++) {
2333                         if (mask & (1u << j))
2334                                 reta_conf[idx].reta[shift + j] = reta.bytes[j];
2335                 }
2336         }
2337
2338         return 0;
2339 }
2340
2341 static int
2342 eth_igc_rss_hash_update(struct rte_eth_dev *dev,
2343                         struct rte_eth_rss_conf *rss_conf)
2344 {
2345         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2346         igc_hw_rss_hash_set(hw, rss_conf);
2347         return 0;
2348 }
2349
2350 static int
2351 eth_igc_rss_hash_conf_get(struct rte_eth_dev *dev,
2352                         struct rte_eth_rss_conf *rss_conf)
2353 {
2354         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2355         uint32_t *hash_key = (uint32_t *)rss_conf->rss_key;
2356         uint32_t mrqc;
2357         uint64_t rss_hf;
2358
2359         if (hash_key != NULL) {
2360                 int i;
2361
2362                 /* if not enough space for store hash key */
2363                 if (rss_conf->rss_key_len != IGC_HKEY_SIZE) {
2364                         PMD_DRV_LOG(ERR,
2365                                 "RSS hash key size %u in parameter doesn't match the hardware hash key size %u",
2366                                 rss_conf->rss_key_len, IGC_HKEY_SIZE);
2367                         return -EINVAL;
2368                 }
2369
2370                 /* read RSS key from register */
2371                 for (i = 0; i < IGC_HKEY_MAX_INDEX; i++)
2372                         hash_key[i] = IGC_READ_REG_LE_VALUE(hw, IGC_RSSRK(i));
2373         }
2374
2375         /* get RSS functions configured in MRQC register */
2376         mrqc = IGC_READ_REG(hw, IGC_MRQC);
2377         if ((mrqc & IGC_MRQC_ENABLE_RSS_4Q) == 0)
2378                 return 0;
2379
2380         rss_hf = 0;
2381         if (mrqc & IGC_MRQC_RSS_FIELD_IPV4)
2382                 rss_hf |= ETH_RSS_IPV4;
2383         if (mrqc & IGC_MRQC_RSS_FIELD_IPV4_TCP)
2384                 rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
2385         if (mrqc & IGC_MRQC_RSS_FIELD_IPV6)
2386                 rss_hf |= ETH_RSS_IPV6;
2387         if (mrqc & IGC_MRQC_RSS_FIELD_IPV6_EX)
2388                 rss_hf |= ETH_RSS_IPV6_EX;
2389         if (mrqc & IGC_MRQC_RSS_FIELD_IPV6_TCP)
2390                 rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP;
2391         if (mrqc & IGC_MRQC_RSS_FIELD_IPV6_TCP_EX)
2392                 rss_hf |= ETH_RSS_IPV6_TCP_EX;
2393         if (mrqc & IGC_MRQC_RSS_FIELD_IPV4_UDP)
2394                 rss_hf |= ETH_RSS_NONFRAG_IPV4_UDP;
2395         if (mrqc & IGC_MRQC_RSS_FIELD_IPV6_UDP)
2396                 rss_hf |= ETH_RSS_NONFRAG_IPV6_UDP;
2397         if (mrqc & IGC_MRQC_RSS_FIELD_IPV6_UDP_EX)
2398                 rss_hf |= ETH_RSS_IPV6_UDP_EX;
2399
2400         rss_conf->rss_hf |= rss_hf;
2401         return 0;
2402 }
2403
2404 static int
2405 eth_igc_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
2406 {
2407         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2408         struct igc_vfta *shadow_vfta = IGC_DEV_PRIVATE_VFTA(dev);
2409         uint32_t vfta;
2410         uint32_t vid_idx;
2411         uint32_t vid_bit;
2412
2413         vid_idx = (vlan_id >> IGC_VFTA_ENTRY_SHIFT) & IGC_VFTA_ENTRY_MASK;
2414         vid_bit = 1u << (vlan_id & IGC_VFTA_ENTRY_BIT_SHIFT_MASK);
2415         vfta = shadow_vfta->vfta[vid_idx];
2416         if (on)
2417                 vfta |= vid_bit;
2418         else
2419                 vfta &= ~vid_bit;
2420         IGC_WRITE_REG_ARRAY(hw, IGC_VFTA, vid_idx, vfta);
2421
2422         /* update local VFTA copy */
2423         shadow_vfta->vfta[vid_idx] = vfta;
2424
2425         return 0;
2426 }
2427
2428 static void
2429 igc_vlan_hw_filter_disable(struct rte_eth_dev *dev)
2430 {
2431         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2432         igc_read_reg_check_clear_bits(hw, IGC_RCTL,
2433                         IGC_RCTL_CFIEN | IGC_RCTL_VFE);
2434 }
2435
2436 static void
2437 igc_vlan_hw_filter_enable(struct rte_eth_dev *dev)
2438 {
2439         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2440         struct igc_vfta *shadow_vfta = IGC_DEV_PRIVATE_VFTA(dev);
2441         uint32_t reg_val;
2442         int i;
2443
2444         /* Filter Table Enable, CFI not used for packet acceptance */
2445         reg_val = IGC_READ_REG(hw, IGC_RCTL);
2446         reg_val &= ~IGC_RCTL_CFIEN;
2447         reg_val |= IGC_RCTL_VFE;
2448         IGC_WRITE_REG(hw, IGC_RCTL, reg_val);
2449
2450         /* restore VFTA table */
2451         for (i = 0; i < IGC_VFTA_SIZE; i++)
2452                 IGC_WRITE_REG_ARRAY(hw, IGC_VFTA, i, shadow_vfta->vfta[i]);
2453 }
2454
2455 static void
2456 igc_vlan_hw_strip_disable(struct rte_eth_dev *dev)
2457 {
2458         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2459
2460         igc_read_reg_check_clear_bits(hw, IGC_CTRL, IGC_CTRL_VME);
2461 }
2462
2463 static void
2464 igc_vlan_hw_strip_enable(struct rte_eth_dev *dev)
2465 {
2466         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2467
2468         igc_read_reg_check_set_bits(hw, IGC_CTRL, IGC_CTRL_VME);
2469 }
2470
2471 static int
2472 igc_vlan_hw_extend_disable(struct rte_eth_dev *dev)
2473 {
2474         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2475         uint32_t frame_size = dev->data->mtu + IGC_ETH_OVERHEAD;
2476         uint32_t ctrl_ext;
2477
2478         ctrl_ext = IGC_READ_REG(hw, IGC_CTRL_EXT);
2479
2480         /* if extend vlan hasn't been enabled */
2481         if ((ctrl_ext & IGC_CTRL_EXT_EXT_VLAN) == 0)
2482                 return 0;
2483
2484         /* Update maximum packet length */
2485         if (frame_size < RTE_ETHER_MIN_MTU + VLAN_TAG_SIZE) {
2486                 PMD_DRV_LOG(ERR, "Maximum packet length %u error, min is %u",
2487                         frame_size, VLAN_TAG_SIZE + RTE_ETHER_MIN_MTU);
2488                 return -EINVAL;
2489         }
2490         IGC_WRITE_REG(hw, IGC_RLPML, frame_size - VLAN_TAG_SIZE);
2491
2492         IGC_WRITE_REG(hw, IGC_CTRL_EXT, ctrl_ext & ~IGC_CTRL_EXT_EXT_VLAN);
2493         return 0;
2494 }
2495
2496 static int
2497 igc_vlan_hw_extend_enable(struct rte_eth_dev *dev)
2498 {
2499         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2500         uint32_t frame_size = dev->data->mtu + IGC_ETH_OVERHEAD;
2501         uint32_t ctrl_ext;
2502
2503         ctrl_ext = IGC_READ_REG(hw, IGC_CTRL_EXT);
2504
2505         /* if extend vlan has been enabled */
2506         if (ctrl_ext & IGC_CTRL_EXT_EXT_VLAN)
2507                 return 0;
2508
2509         /* Update maximum packet length */
2510         if (frame_size > MAX_RX_JUMBO_FRAME_SIZE) {
2511                 PMD_DRV_LOG(ERR, "Maximum packet length %u error, max is %u",
2512                         frame_size, MAX_RX_JUMBO_FRAME_SIZE);
2513                 return -EINVAL;
2514         }
2515         IGC_WRITE_REG(hw, IGC_RLPML, frame_size);
2516
2517         IGC_WRITE_REG(hw, IGC_CTRL_EXT, ctrl_ext | IGC_CTRL_EXT_EXT_VLAN);
2518         return 0;
2519 }
2520
2521 static int
2522 eth_igc_vlan_offload_set(struct rte_eth_dev *dev, int mask)
2523 {
2524         struct rte_eth_rxmode *rxmode;
2525
2526         rxmode = &dev->data->dev_conf.rxmode;
2527         if (mask & ETH_VLAN_STRIP_MASK) {
2528                 if (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
2529                         igc_vlan_hw_strip_enable(dev);
2530                 else
2531                         igc_vlan_hw_strip_disable(dev);
2532         }
2533
2534         if (mask & ETH_VLAN_FILTER_MASK) {
2535                 if (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
2536                         igc_vlan_hw_filter_enable(dev);
2537                 else
2538                         igc_vlan_hw_filter_disable(dev);
2539         }
2540
2541         if (mask & ETH_VLAN_EXTEND_MASK) {
2542                 if (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_EXTEND)
2543                         return igc_vlan_hw_extend_enable(dev);
2544                 else
2545                         return igc_vlan_hw_extend_disable(dev);
2546         }
2547
2548         return 0;
2549 }
2550
2551 static int
2552 eth_igc_vlan_tpid_set(struct rte_eth_dev *dev,
2553                       enum rte_vlan_type vlan_type,
2554                       uint16_t tpid)
2555 {
2556         struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
2557         uint32_t reg_val;
2558
2559         /* only outer TPID of double VLAN can be configured*/
2560         if (vlan_type == ETH_VLAN_TYPE_OUTER) {
2561                 reg_val = IGC_READ_REG(hw, IGC_VET);
2562                 reg_val = (reg_val & (~IGC_VET_EXT)) |
2563                         ((uint32_t)tpid << IGC_VET_EXT_SHIFT);
2564                 IGC_WRITE_REG(hw, IGC_VET, reg_val);
2565
2566                 return 0;
2567         }
2568
2569         /* all other TPID values are read-only*/
2570         PMD_DRV_LOG(ERR, "Not supported");
2571         return -ENOTSUP;
2572 }
2573
2574 static int
2575 eth_igc_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2576         struct rte_pci_device *pci_dev)
2577 {
2578         PMD_INIT_FUNC_TRACE();
2579         return rte_eth_dev_pci_generic_probe(pci_dev,
2580                 sizeof(struct igc_adapter), eth_igc_dev_init);
2581 }
2582
2583 static int
2584 eth_igc_pci_remove(struct rte_pci_device *pci_dev)
2585 {
2586         PMD_INIT_FUNC_TRACE();
2587         return rte_eth_dev_pci_generic_remove(pci_dev, eth_igc_dev_uninit);
2588 }
2589
2590 static struct rte_pci_driver rte_igc_pmd = {
2591         .id_table = pci_id_igc_map,
2592         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
2593         .probe = eth_igc_pci_probe,
2594         .remove = eth_igc_pci_remove,
2595 };
2596
2597 RTE_PMD_REGISTER_PCI(net_igc, rte_igc_pmd);
2598 RTE_PMD_REGISTER_PCI_TABLE(net_igc, pci_id_igc_map);
2599 RTE_PMD_REGISTER_KMOD_DEP(net_igc, "* igb_uio | uio_pci_generic | vfio-pci");