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