net/txgbe: support ethertype filter add and delete
[dpdk.git] / drivers / net / txgbe / txgbe_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020
3  */
4
5 #include <stdio.h>
6 #include <errno.h>
7 #include <stdint.h>
8 #include <string.h>
9 #include <rte_common.h>
10 #include <rte_ethdev_pci.h>
11
12 #include <rte_interrupts.h>
13 #include <rte_log.h>
14 #include <rte_debug.h>
15 #include <rte_pci.h>
16 #include <rte_memory.h>
17 #include <rte_eal.h>
18 #include <rte_alarm.h>
19
20 #include "txgbe_logs.h"
21 #include "base/txgbe.h"
22 #include "txgbe_ethdev.h"
23 #include "txgbe_rxtx.h"
24 #include "txgbe_regs_group.h"
25
26 static const struct reg_info txgbe_regs_general[] = {
27         {TXGBE_RST, 1, 1, "TXGBE_RST"},
28         {TXGBE_STAT, 1, 1, "TXGBE_STAT"},
29         {TXGBE_PORTCTL, 1, 1, "TXGBE_PORTCTL"},
30         {TXGBE_SDP, 1, 1, "TXGBE_SDP"},
31         {TXGBE_SDPCTL, 1, 1, "TXGBE_SDPCTL"},
32         {TXGBE_LEDCTL, 1, 1, "TXGBE_LEDCTL"},
33         {0, 0, 0, ""}
34 };
35
36 static const struct reg_info txgbe_regs_nvm[] = {
37         {0, 0, 0, ""}
38 };
39
40 static const struct reg_info txgbe_regs_interrupt[] = {
41         {0, 0, 0, ""}
42 };
43
44 static const struct reg_info txgbe_regs_fctl_others[] = {
45         {0, 0, 0, ""}
46 };
47
48 static const struct reg_info txgbe_regs_rxdma[] = {
49         {0, 0, 0, ""}
50 };
51
52 static const struct reg_info txgbe_regs_rx[] = {
53         {0, 0, 0, ""}
54 };
55
56 static struct reg_info txgbe_regs_tx[] = {
57         {0, 0, 0, ""}
58 };
59
60 static const struct reg_info txgbe_regs_wakeup[] = {
61         {0, 0, 0, ""}
62 };
63
64 static const struct reg_info txgbe_regs_dcb[] = {
65         {0, 0, 0, ""}
66 };
67
68 static const struct reg_info txgbe_regs_mac[] = {
69         {0, 0, 0, ""}
70 };
71
72 static const struct reg_info txgbe_regs_diagnostic[] = {
73         {0, 0, 0, ""},
74 };
75
76 /* PF registers */
77 static const struct reg_info *txgbe_regs_others[] = {
78                                 txgbe_regs_general,
79                                 txgbe_regs_nvm,
80                                 txgbe_regs_interrupt,
81                                 txgbe_regs_fctl_others,
82                                 txgbe_regs_rxdma,
83                                 txgbe_regs_rx,
84                                 txgbe_regs_tx,
85                                 txgbe_regs_wakeup,
86                                 txgbe_regs_dcb,
87                                 txgbe_regs_mac,
88                                 txgbe_regs_diagnostic,
89                                 NULL};
90
91 static int  txgbe_dev_set_link_up(struct rte_eth_dev *dev);
92 static int  txgbe_dev_set_link_down(struct rte_eth_dev *dev);
93 static int txgbe_dev_close(struct rte_eth_dev *dev);
94 static int txgbe_dev_link_update(struct rte_eth_dev *dev,
95                                 int wait_to_complete);
96 static int txgbe_dev_stats_reset(struct rte_eth_dev *dev);
97 static void txgbe_vlan_hw_strip_enable(struct rte_eth_dev *dev, uint16_t queue);
98 static void txgbe_vlan_hw_strip_disable(struct rte_eth_dev *dev,
99                                         uint16_t queue);
100
101 static void txgbe_dev_link_status_print(struct rte_eth_dev *dev);
102 static int txgbe_dev_lsc_interrupt_setup(struct rte_eth_dev *dev, uint8_t on);
103 static int txgbe_dev_macsec_interrupt_setup(struct rte_eth_dev *dev);
104 static int txgbe_dev_rxq_interrupt_setup(struct rte_eth_dev *dev);
105 static int txgbe_dev_interrupt_get_status(struct rte_eth_dev *dev);
106 static int txgbe_dev_interrupt_action(struct rte_eth_dev *dev,
107                                       struct rte_intr_handle *handle);
108 static void txgbe_dev_interrupt_handler(void *param);
109 static void txgbe_dev_interrupt_delayed_handler(void *param);
110 static void txgbe_configure_msix(struct rte_eth_dev *dev);
111
112 static int txgbe_filter_restore(struct rte_eth_dev *dev);
113
114 #define TXGBE_SET_HWSTRIP(h, q) do {\
115                 uint32_t idx = (q) / (sizeof((h)->bitmap[0]) * NBBY); \
116                 uint32_t bit = (q) % (sizeof((h)->bitmap[0]) * NBBY); \
117                 (h)->bitmap[idx] |= 1 << bit;\
118         } while (0)
119
120 #define TXGBE_CLEAR_HWSTRIP(h, q) do {\
121                 uint32_t idx = (q) / (sizeof((h)->bitmap[0]) * NBBY); \
122                 uint32_t bit = (q) % (sizeof((h)->bitmap[0]) * NBBY); \
123                 (h)->bitmap[idx] &= ~(1 << bit);\
124         } while (0)
125
126 #define TXGBE_GET_HWSTRIP(h, q, r) do {\
127                 uint32_t idx = (q) / (sizeof((h)->bitmap[0]) * NBBY); \
128                 uint32_t bit = (q) % (sizeof((h)->bitmap[0]) * NBBY); \
129                 (r) = (h)->bitmap[idx] >> bit & 1;\
130         } while (0)
131
132 /*
133  * The set of PCI devices this driver supports
134  */
135 static const struct rte_pci_id pci_id_txgbe_map[] = {
136         { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, TXGBE_DEV_ID_RAPTOR_SFP) },
137         { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, TXGBE_DEV_ID_WX1820_SFP) },
138         { .vendor_id = 0, /* sentinel */ },
139 };
140
141 static const struct rte_eth_desc_lim rx_desc_lim = {
142         .nb_max = TXGBE_RING_DESC_MAX,
143         .nb_min = TXGBE_RING_DESC_MIN,
144         .nb_align = TXGBE_RXD_ALIGN,
145 };
146
147 static const struct rte_eth_desc_lim tx_desc_lim = {
148         .nb_max = TXGBE_RING_DESC_MAX,
149         .nb_min = TXGBE_RING_DESC_MIN,
150         .nb_align = TXGBE_TXD_ALIGN,
151         .nb_seg_max = TXGBE_TX_MAX_SEG,
152         .nb_mtu_seg_max = TXGBE_TX_MAX_SEG,
153 };
154
155 static const struct eth_dev_ops txgbe_eth_dev_ops;
156
157 #define HW_XSTAT(m) {#m, offsetof(struct txgbe_hw_stats, m)}
158 #define HW_XSTAT_NAME(m, n) {n, offsetof(struct txgbe_hw_stats, m)}
159 static const struct rte_txgbe_xstats_name_off rte_txgbe_stats_strings[] = {
160         /* MNG RxTx */
161         HW_XSTAT(mng_bmc2host_packets),
162         HW_XSTAT(mng_host2bmc_packets),
163         /* Basic RxTx */
164         HW_XSTAT(rx_packets),
165         HW_XSTAT(tx_packets),
166         HW_XSTAT(rx_bytes),
167         HW_XSTAT(tx_bytes),
168         HW_XSTAT(rx_total_bytes),
169         HW_XSTAT(rx_total_packets),
170         HW_XSTAT(tx_total_packets),
171         HW_XSTAT(rx_total_missed_packets),
172         HW_XSTAT(rx_broadcast_packets),
173         HW_XSTAT(rx_multicast_packets),
174         HW_XSTAT(rx_management_packets),
175         HW_XSTAT(tx_management_packets),
176         HW_XSTAT(rx_management_dropped),
177
178         /* Basic Error */
179         HW_XSTAT(rx_crc_errors),
180         HW_XSTAT(rx_illegal_byte_errors),
181         HW_XSTAT(rx_error_bytes),
182         HW_XSTAT(rx_mac_short_packet_dropped),
183         HW_XSTAT(rx_length_errors),
184         HW_XSTAT(rx_undersize_errors),
185         HW_XSTAT(rx_fragment_errors),
186         HW_XSTAT(rx_oversize_errors),
187         HW_XSTAT(rx_jabber_errors),
188         HW_XSTAT(rx_l3_l4_xsum_error),
189         HW_XSTAT(mac_local_errors),
190         HW_XSTAT(mac_remote_errors),
191
192         /* Flow Director */
193         HW_XSTAT(flow_director_added_filters),
194         HW_XSTAT(flow_director_removed_filters),
195         HW_XSTAT(flow_director_filter_add_errors),
196         HW_XSTAT(flow_director_filter_remove_errors),
197         HW_XSTAT(flow_director_matched_filters),
198         HW_XSTAT(flow_director_missed_filters),
199
200         /* FCoE */
201         HW_XSTAT(rx_fcoe_crc_errors),
202         HW_XSTAT(rx_fcoe_mbuf_allocation_errors),
203         HW_XSTAT(rx_fcoe_dropped),
204         HW_XSTAT(rx_fcoe_packets),
205         HW_XSTAT(tx_fcoe_packets),
206         HW_XSTAT(rx_fcoe_bytes),
207         HW_XSTAT(tx_fcoe_bytes),
208         HW_XSTAT(rx_fcoe_no_ddp),
209         HW_XSTAT(rx_fcoe_no_ddp_ext_buff),
210
211         /* MACSEC */
212         HW_XSTAT(tx_macsec_pkts_untagged),
213         HW_XSTAT(tx_macsec_pkts_encrypted),
214         HW_XSTAT(tx_macsec_pkts_protected),
215         HW_XSTAT(tx_macsec_octets_encrypted),
216         HW_XSTAT(tx_macsec_octets_protected),
217         HW_XSTAT(rx_macsec_pkts_untagged),
218         HW_XSTAT(rx_macsec_pkts_badtag),
219         HW_XSTAT(rx_macsec_pkts_nosci),
220         HW_XSTAT(rx_macsec_pkts_unknownsci),
221         HW_XSTAT(rx_macsec_octets_decrypted),
222         HW_XSTAT(rx_macsec_octets_validated),
223         HW_XSTAT(rx_macsec_sc_pkts_unchecked),
224         HW_XSTAT(rx_macsec_sc_pkts_delayed),
225         HW_XSTAT(rx_macsec_sc_pkts_late),
226         HW_XSTAT(rx_macsec_sa_pkts_ok),
227         HW_XSTAT(rx_macsec_sa_pkts_invalid),
228         HW_XSTAT(rx_macsec_sa_pkts_notvalid),
229         HW_XSTAT(rx_macsec_sa_pkts_unusedsa),
230         HW_XSTAT(rx_macsec_sa_pkts_notusingsa),
231
232         /* MAC RxTx */
233         HW_XSTAT(rx_size_64_packets),
234         HW_XSTAT(rx_size_65_to_127_packets),
235         HW_XSTAT(rx_size_128_to_255_packets),
236         HW_XSTAT(rx_size_256_to_511_packets),
237         HW_XSTAT(rx_size_512_to_1023_packets),
238         HW_XSTAT(rx_size_1024_to_max_packets),
239         HW_XSTAT(tx_size_64_packets),
240         HW_XSTAT(tx_size_65_to_127_packets),
241         HW_XSTAT(tx_size_128_to_255_packets),
242         HW_XSTAT(tx_size_256_to_511_packets),
243         HW_XSTAT(tx_size_512_to_1023_packets),
244         HW_XSTAT(tx_size_1024_to_max_packets),
245
246         /* Flow Control */
247         HW_XSTAT(tx_xon_packets),
248         HW_XSTAT(rx_xon_packets),
249         HW_XSTAT(tx_xoff_packets),
250         HW_XSTAT(rx_xoff_packets),
251
252         HW_XSTAT_NAME(tx_xon_packets, "tx_flow_control_xon_packets"),
253         HW_XSTAT_NAME(rx_xon_packets, "rx_flow_control_xon_packets"),
254         HW_XSTAT_NAME(tx_xoff_packets, "tx_flow_control_xoff_packets"),
255         HW_XSTAT_NAME(rx_xoff_packets, "rx_flow_control_xoff_packets"),
256 };
257
258 #define TXGBE_NB_HW_STATS (sizeof(rte_txgbe_stats_strings) / \
259                            sizeof(rte_txgbe_stats_strings[0]))
260
261 /* Per-priority statistics */
262 #define UP_XSTAT(m) {#m, offsetof(struct txgbe_hw_stats, up[0].m)}
263 static const struct rte_txgbe_xstats_name_off rte_txgbe_up_strings[] = {
264         UP_XSTAT(rx_up_packets),
265         UP_XSTAT(tx_up_packets),
266         UP_XSTAT(rx_up_bytes),
267         UP_XSTAT(tx_up_bytes),
268         UP_XSTAT(rx_up_drop_packets),
269
270         UP_XSTAT(tx_up_xon_packets),
271         UP_XSTAT(rx_up_xon_packets),
272         UP_XSTAT(tx_up_xoff_packets),
273         UP_XSTAT(rx_up_xoff_packets),
274         UP_XSTAT(rx_up_dropped),
275         UP_XSTAT(rx_up_mbuf_alloc_errors),
276         UP_XSTAT(tx_up_xon2off_packets),
277 };
278
279 #define TXGBE_NB_UP_STATS (sizeof(rte_txgbe_up_strings) / \
280                            sizeof(rte_txgbe_up_strings[0]))
281
282 /* Per-queue statistics */
283 #define QP_XSTAT(m) {#m, offsetof(struct txgbe_hw_stats, qp[0].m)}
284 static const struct rte_txgbe_xstats_name_off rte_txgbe_qp_strings[] = {
285         QP_XSTAT(rx_qp_packets),
286         QP_XSTAT(tx_qp_packets),
287         QP_XSTAT(rx_qp_bytes),
288         QP_XSTAT(tx_qp_bytes),
289         QP_XSTAT(rx_qp_mc_packets),
290 };
291
292 #define TXGBE_NB_QP_STATS (sizeof(rte_txgbe_qp_strings) / \
293                            sizeof(rte_txgbe_qp_strings[0]))
294
295 static inline int
296 txgbe_is_sfp(struct txgbe_hw *hw)
297 {
298         switch (hw->phy.type) {
299         case txgbe_phy_sfp_avago:
300         case txgbe_phy_sfp_ftl:
301         case txgbe_phy_sfp_intel:
302         case txgbe_phy_sfp_unknown:
303         case txgbe_phy_sfp_tyco_passive:
304         case txgbe_phy_sfp_unknown_passive:
305                 return 1;
306         default:
307                 return 0;
308         }
309 }
310
311 static inline int32_t
312 txgbe_pf_reset_hw(struct txgbe_hw *hw)
313 {
314         uint32_t ctrl_ext;
315         int32_t status;
316
317         status = hw->mac.reset_hw(hw);
318
319         ctrl_ext = rd32(hw, TXGBE_PORTCTL);
320         /* Set PF Reset Done bit so PF/VF Mail Ops can work */
321         ctrl_ext |= TXGBE_PORTCTL_RSTDONE;
322         wr32(hw, TXGBE_PORTCTL, ctrl_ext);
323         txgbe_flush(hw);
324
325         if (status == TXGBE_ERR_SFP_NOT_PRESENT)
326                 status = 0;
327         return status;
328 }
329
330 static inline void
331 txgbe_enable_intr(struct rte_eth_dev *dev)
332 {
333         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
334         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
335
336         wr32(hw, TXGBE_IENMISC, intr->mask_misc);
337         wr32(hw, TXGBE_IMC(0), TXGBE_IMC_MASK);
338         wr32(hw, TXGBE_IMC(1), TXGBE_IMC_MASK);
339         txgbe_flush(hw);
340 }
341
342 static void
343 txgbe_disable_intr(struct txgbe_hw *hw)
344 {
345         PMD_INIT_FUNC_TRACE();
346
347         wr32(hw, TXGBE_IENMISC, ~BIT_MASK32);
348         wr32(hw, TXGBE_IMS(0), TXGBE_IMC_MASK);
349         wr32(hw, TXGBE_IMS(1), TXGBE_IMC_MASK);
350         txgbe_flush(hw);
351 }
352
353 static int
354 txgbe_dev_queue_stats_mapping_set(struct rte_eth_dev *eth_dev,
355                                   uint16_t queue_id,
356                                   uint8_t stat_idx,
357                                   uint8_t is_rx)
358 {
359         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
360         struct txgbe_stat_mappings *stat_mappings =
361                 TXGBE_DEV_STAT_MAPPINGS(eth_dev);
362         uint32_t qsmr_mask = 0;
363         uint32_t clearing_mask = QMAP_FIELD_RESERVED_BITS_MASK;
364         uint32_t q_map;
365         uint8_t n, offset;
366
367         if (hw->mac.type != txgbe_mac_raptor)
368                 return -ENOSYS;
369
370         if (stat_idx & !QMAP_FIELD_RESERVED_BITS_MASK)
371                 return -EIO;
372
373         PMD_INIT_LOG(DEBUG, "Setting port %d, %s queue_id %d to stat index %d",
374                      (int)(eth_dev->data->port_id), is_rx ? "RX" : "TX",
375                      queue_id, stat_idx);
376
377         n = (uint8_t)(queue_id / NB_QMAP_FIELDS_PER_QSM_REG);
378         if (n >= TXGBE_NB_STAT_MAPPING) {
379                 PMD_INIT_LOG(ERR, "Nb of stat mapping registers exceeded");
380                 return -EIO;
381         }
382         offset = (uint8_t)(queue_id % NB_QMAP_FIELDS_PER_QSM_REG);
383
384         /* Now clear any previous stat_idx set */
385         clearing_mask <<= (QSM_REG_NB_BITS_PER_QMAP_FIELD * offset);
386         if (!is_rx)
387                 stat_mappings->tqsm[n] &= ~clearing_mask;
388         else
389                 stat_mappings->rqsm[n] &= ~clearing_mask;
390
391         q_map = (uint32_t)stat_idx;
392         q_map &= QMAP_FIELD_RESERVED_BITS_MASK;
393         qsmr_mask = q_map << (QSM_REG_NB_BITS_PER_QMAP_FIELD * offset);
394         if (!is_rx)
395                 stat_mappings->tqsm[n] |= qsmr_mask;
396         else
397                 stat_mappings->rqsm[n] |= qsmr_mask;
398
399         PMD_INIT_LOG(DEBUG, "Set port %d, %s queue_id %d to stat index %d",
400                      (int)(eth_dev->data->port_id), is_rx ? "RX" : "TX",
401                      queue_id, stat_idx);
402         PMD_INIT_LOG(DEBUG, "%s[%d] = 0x%08x", is_rx ? "RQSMR" : "TQSM", n,
403                      is_rx ? stat_mappings->rqsm[n] : stat_mappings->tqsm[n]);
404         return 0;
405 }
406
407 static void
408 txgbe_dcb_init(struct txgbe_hw *hw, struct txgbe_dcb_config *dcb_config)
409 {
410         int i;
411         u8 bwgp;
412         struct txgbe_dcb_tc_config *tc;
413
414         UNREFERENCED_PARAMETER(hw);
415
416         dcb_config->num_tcs.pg_tcs = TXGBE_DCB_TC_MAX;
417         dcb_config->num_tcs.pfc_tcs = TXGBE_DCB_TC_MAX;
418         bwgp = (u8)(100 / TXGBE_DCB_TC_MAX);
419         for (i = 0; i < TXGBE_DCB_TC_MAX; i++) {
420                 tc = &dcb_config->tc_config[i];
421                 tc->path[TXGBE_DCB_TX_CONFIG].bwg_id = i;
422                 tc->path[TXGBE_DCB_TX_CONFIG].bwg_percent = bwgp + (i & 1);
423                 tc->path[TXGBE_DCB_RX_CONFIG].bwg_id = i;
424                 tc->path[TXGBE_DCB_RX_CONFIG].bwg_percent = bwgp + (i & 1);
425                 tc->pfc = txgbe_dcb_pfc_disabled;
426         }
427
428         /* Initialize default user to priority mapping, UPx->TC0 */
429         tc = &dcb_config->tc_config[0];
430         tc->path[TXGBE_DCB_TX_CONFIG].up_to_tc_bitmap = 0xFF;
431         tc->path[TXGBE_DCB_RX_CONFIG].up_to_tc_bitmap = 0xFF;
432         for (i = 0; i < TXGBE_DCB_BWG_MAX; i++) {
433                 dcb_config->bw_percentage[i][TXGBE_DCB_TX_CONFIG] = 100;
434                 dcb_config->bw_percentage[i][TXGBE_DCB_RX_CONFIG] = 100;
435         }
436         dcb_config->rx_pba_cfg = txgbe_dcb_pba_equal;
437         dcb_config->pfc_mode_enable = false;
438         dcb_config->vt_mode = true;
439         dcb_config->round_robin_enable = false;
440         /* support all DCB capabilities */
441         dcb_config->support.capabilities = 0xFF;
442 }
443
444 /*
445  * Ensure that all locks are released before first NVM or PHY access
446  */
447 static void
448 txgbe_swfw_lock_reset(struct txgbe_hw *hw)
449 {
450         uint16_t mask;
451
452         /*
453          * These ones are more tricky since they are common to all ports; but
454          * swfw_sync retries last long enough (1s) to be almost sure that if
455          * lock can not be taken it is due to an improper lock of the
456          * semaphore.
457          */
458         mask = TXGBE_MNGSEM_SWPHY |
459                TXGBE_MNGSEM_SWMBX |
460                TXGBE_MNGSEM_SWFLASH;
461         if (hw->mac.acquire_swfw_sync(hw, mask) < 0)
462                 PMD_DRV_LOG(DEBUG, "SWFW common locks released");
463
464         hw->mac.release_swfw_sync(hw, mask);
465 }
466
467 static int
468 eth_txgbe_dev_init(struct rte_eth_dev *eth_dev, void *init_params __rte_unused)
469 {
470         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
471         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
472         struct txgbe_vfta *shadow_vfta = TXGBE_DEV_VFTA(eth_dev);
473         struct txgbe_hwstrip *hwstrip = TXGBE_DEV_HWSTRIP(eth_dev);
474         struct txgbe_dcb_config *dcb_config = TXGBE_DEV_DCB_CONFIG(eth_dev);
475         struct txgbe_filter_info *filter_info = TXGBE_DEV_FILTER(eth_dev);
476         struct txgbe_bw_conf *bw_conf = TXGBE_DEV_BW_CONF(eth_dev);
477         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
478         const struct rte_memzone *mz;
479         uint32_t ctrl_ext;
480         uint16_t csum;
481         int err, i, ret;
482
483         PMD_INIT_FUNC_TRACE();
484
485         eth_dev->dev_ops = &txgbe_eth_dev_ops;
486         eth_dev->rx_queue_count       = txgbe_dev_rx_queue_count;
487         eth_dev->rx_descriptor_status = txgbe_dev_rx_descriptor_status;
488         eth_dev->tx_descriptor_status = txgbe_dev_tx_descriptor_status;
489         eth_dev->rx_pkt_burst = &txgbe_recv_pkts;
490         eth_dev->tx_pkt_burst = &txgbe_xmit_pkts;
491         eth_dev->tx_pkt_prepare = &txgbe_prep_pkts;
492
493         /*
494          * For secondary processes, we don't initialise any further as primary
495          * has already done this work. Only check we don't need a different
496          * RX and TX function.
497          */
498         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
499                 struct txgbe_tx_queue *txq;
500                 /* TX queue function in primary, set by last queue initialized
501                  * Tx queue may not initialized by primary process
502                  */
503                 if (eth_dev->data->tx_queues) {
504                         uint16_t nb_tx_queues = eth_dev->data->nb_tx_queues;
505                         txq = eth_dev->data->tx_queues[nb_tx_queues - 1];
506                         txgbe_set_tx_function(eth_dev, txq);
507                 } else {
508                         /* Use default TX function if we get here */
509                         PMD_INIT_LOG(NOTICE, "No TX queues configured yet. "
510                                      "Using default TX function.");
511                 }
512
513                 txgbe_set_rx_function(eth_dev);
514
515                 return 0;
516         }
517
518         rte_eth_copy_pci_info(eth_dev, pci_dev);
519
520         /* Vendor and Device ID need to be set before init of shared code */
521         hw->device_id = pci_dev->id.device_id;
522         hw->vendor_id = pci_dev->id.vendor_id;
523         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
524         hw->allow_unsupported_sfp = 1;
525
526         /* Reserve memory for interrupt status block */
527         mz = rte_eth_dma_zone_reserve(eth_dev, "txgbe_driver", -1,
528                 16, TXGBE_ALIGN, SOCKET_ID_ANY);
529         if (mz == NULL)
530                 return -ENOMEM;
531
532         hw->isb_dma = TMZ_PADDR(mz);
533         hw->isb_mem = TMZ_VADDR(mz);
534
535         /* Initialize the shared code (base driver) */
536         err = txgbe_init_shared_code(hw);
537         if (err != 0) {
538                 PMD_INIT_LOG(ERR, "Shared code init failed: %d", err);
539                 return -EIO;
540         }
541
542         /* Unlock any pending hardware semaphore */
543         txgbe_swfw_lock_reset(hw);
544
545         /* Initialize DCB configuration*/
546         memset(dcb_config, 0, sizeof(struct txgbe_dcb_config));
547         txgbe_dcb_init(hw, dcb_config);
548
549         /* Get Hardware Flow Control setting */
550         hw->fc.requested_mode = txgbe_fc_full;
551         hw->fc.current_mode = txgbe_fc_full;
552         hw->fc.pause_time = TXGBE_FC_PAUSE_TIME;
553         for (i = 0; i < TXGBE_DCB_TC_MAX; i++) {
554                 hw->fc.low_water[i] = TXGBE_FC_XON_LOTH;
555                 hw->fc.high_water[i] = TXGBE_FC_XOFF_HITH;
556         }
557         hw->fc.send_xon = 1;
558
559         err = hw->rom.init_params(hw);
560         if (err != 0) {
561                 PMD_INIT_LOG(ERR, "The EEPROM init failed: %d", err);
562                 return -EIO;
563         }
564
565         /* Make sure we have a good EEPROM before we read from it */
566         err = hw->rom.validate_checksum(hw, &csum);
567         if (err != 0) {
568                 PMD_INIT_LOG(ERR, "The EEPROM checksum is not valid: %d", err);
569                 return -EIO;
570         }
571
572         err = hw->mac.init_hw(hw);
573
574         /*
575          * Devices with copper phys will fail to initialise if txgbe_init_hw()
576          * is called too soon after the kernel driver unbinding/binding occurs.
577          * The failure occurs in txgbe_identify_phy() for all devices,
578          * but for non-copper devies, txgbe_identify_sfp_module() is
579          * also called. See txgbe_identify_phy(). The reason for the
580          * failure is not known, and only occuts when virtualisation features
581          * are disabled in the bios. A delay of 200ms  was found to be enough by
582          * trial-and-error, and is doubled to be safe.
583          */
584         if (err && hw->phy.media_type == txgbe_media_type_copper) {
585                 rte_delay_ms(200);
586                 err = hw->mac.init_hw(hw);
587         }
588
589         if (err == TXGBE_ERR_SFP_NOT_PRESENT)
590                 err = 0;
591
592         if (err == TXGBE_ERR_EEPROM_VERSION) {
593                 PMD_INIT_LOG(ERR, "This device is a pre-production adapter/"
594                              "LOM.  Please be aware there may be issues associated "
595                              "with your hardware.");
596                 PMD_INIT_LOG(ERR, "If you are experiencing problems "
597                              "please contact your hardware representative "
598                              "who provided you with this hardware.");
599         } else if (err == TXGBE_ERR_SFP_NOT_SUPPORTED) {
600                 PMD_INIT_LOG(ERR, "Unsupported SFP+ Module");
601         }
602         if (err) {
603                 PMD_INIT_LOG(ERR, "Hardware Initialization Failure: %d", err);
604                 return -EIO;
605         }
606
607         /* Reset the hw statistics */
608         txgbe_dev_stats_reset(eth_dev);
609
610         /* disable interrupt */
611         txgbe_disable_intr(hw);
612
613         /* Allocate memory for storing MAC addresses */
614         eth_dev->data->mac_addrs = rte_zmalloc("txgbe", RTE_ETHER_ADDR_LEN *
615                                                hw->mac.num_rar_entries, 0);
616         if (eth_dev->data->mac_addrs == NULL) {
617                 PMD_INIT_LOG(ERR,
618                              "Failed to allocate %u bytes needed to store "
619                              "MAC addresses",
620                              RTE_ETHER_ADDR_LEN * hw->mac.num_rar_entries);
621                 return -ENOMEM;
622         }
623
624         /* Copy the permanent MAC address */
625         rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.perm_addr,
626                         &eth_dev->data->mac_addrs[0]);
627
628         /* Allocate memory for storing hash filter MAC addresses */
629         eth_dev->data->hash_mac_addrs = rte_zmalloc("txgbe",
630                         RTE_ETHER_ADDR_LEN * TXGBE_VMDQ_NUM_UC_MAC, 0);
631         if (eth_dev->data->hash_mac_addrs == NULL) {
632                 PMD_INIT_LOG(ERR,
633                              "Failed to allocate %d bytes needed to store MAC addresses",
634                              RTE_ETHER_ADDR_LEN * TXGBE_VMDQ_NUM_UC_MAC);
635                 return -ENOMEM;
636         }
637
638         /* initialize the vfta */
639         memset(shadow_vfta, 0, sizeof(*shadow_vfta));
640
641         /* initialize the hw strip bitmap*/
642         memset(hwstrip, 0, sizeof(*hwstrip));
643
644         /* initialize PF if max_vfs not zero */
645         ret = txgbe_pf_host_init(eth_dev);
646         if (ret) {
647                 rte_free(eth_dev->data->mac_addrs);
648                 eth_dev->data->mac_addrs = NULL;
649                 rte_free(eth_dev->data->hash_mac_addrs);
650                 eth_dev->data->hash_mac_addrs = NULL;
651                 return ret;
652         }
653
654         ctrl_ext = rd32(hw, TXGBE_PORTCTL);
655         /* let hardware know driver is loaded */
656         ctrl_ext |= TXGBE_PORTCTL_DRVLOAD;
657         /* Set PF Reset Done bit so PF/VF Mail Ops can work */
658         ctrl_ext |= TXGBE_PORTCTL_RSTDONE;
659         wr32(hw, TXGBE_PORTCTL, ctrl_ext);
660         txgbe_flush(hw);
661
662         if (txgbe_is_sfp(hw) && hw->phy.sfp_type != txgbe_sfp_type_not_present)
663                 PMD_INIT_LOG(DEBUG, "MAC: %d, PHY: %d, SFP+: %d",
664                              (int)hw->mac.type, (int)hw->phy.type,
665                              (int)hw->phy.sfp_type);
666         else
667                 PMD_INIT_LOG(DEBUG, "MAC: %d, PHY: %d",
668                              (int)hw->mac.type, (int)hw->phy.type);
669
670         PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
671                      eth_dev->data->port_id, pci_dev->id.vendor_id,
672                      pci_dev->id.device_id);
673
674         rte_intr_callback_register(intr_handle,
675                                    txgbe_dev_interrupt_handler, eth_dev);
676
677         /* enable uio/vfio intr/eventfd mapping */
678         rte_intr_enable(intr_handle);
679
680         /* enable support intr */
681         txgbe_enable_intr(eth_dev);
682
683         /* initialize filter info */
684         memset(filter_info, 0,
685                sizeof(struct txgbe_filter_info));
686
687         /* initialize 5tuple filter list */
688         TAILQ_INIT(&filter_info->fivetuple_list);
689
690         /* initialize bandwidth configuration info */
691         memset(bw_conf, 0, sizeof(struct txgbe_bw_conf));
692
693         return 0;
694 }
695
696 static int
697 eth_txgbe_dev_uninit(struct rte_eth_dev *eth_dev)
698 {
699         PMD_INIT_FUNC_TRACE();
700
701         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
702                 return 0;
703
704         txgbe_dev_close(eth_dev);
705
706         return 0;
707 }
708
709 static int txgbe_ntuple_filter_uninit(struct rte_eth_dev *eth_dev)
710 {
711         struct txgbe_filter_info *filter_info = TXGBE_DEV_FILTER(eth_dev);
712         struct txgbe_5tuple_filter *p_5tuple;
713
714         while ((p_5tuple = TAILQ_FIRST(&filter_info->fivetuple_list))) {
715                 TAILQ_REMOVE(&filter_info->fivetuple_list,
716                              p_5tuple,
717                              entries);
718                 rte_free(p_5tuple);
719         }
720         memset(filter_info->fivetuple_mask, 0,
721                sizeof(uint32_t) * TXGBE_5TUPLE_ARRAY_SIZE);
722
723         return 0;
724 }
725
726 static int
727 eth_txgbe_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
728                 struct rte_pci_device *pci_dev)
729 {
730         struct rte_eth_dev *pf_ethdev;
731         struct rte_eth_devargs eth_da;
732         int retval;
733
734         if (pci_dev->device.devargs) {
735                 retval = rte_eth_devargs_parse(pci_dev->device.devargs->args,
736                                 &eth_da);
737                 if (retval)
738                         return retval;
739         } else {
740                 memset(&eth_da, 0, sizeof(eth_da));
741         }
742
743         retval = rte_eth_dev_create(&pci_dev->device, pci_dev->device.name,
744                         sizeof(struct txgbe_adapter),
745                         eth_dev_pci_specific_init, pci_dev,
746                         eth_txgbe_dev_init, NULL);
747
748         if (retval || eth_da.nb_representor_ports < 1)
749                 return retval;
750
751         pf_ethdev = rte_eth_dev_allocated(pci_dev->device.name);
752         if (pf_ethdev == NULL)
753                 return -ENODEV;
754
755         return 0;
756 }
757
758 static int eth_txgbe_pci_remove(struct rte_pci_device *pci_dev)
759 {
760         struct rte_eth_dev *ethdev;
761
762         ethdev = rte_eth_dev_allocated(pci_dev->device.name);
763         if (!ethdev)
764                 return -ENODEV;
765
766         return rte_eth_dev_destroy(ethdev, eth_txgbe_dev_uninit);
767 }
768
769 static struct rte_pci_driver rte_txgbe_pmd = {
770         .id_table = pci_id_txgbe_map,
771         .drv_flags = RTE_PCI_DRV_NEED_MAPPING |
772                      RTE_PCI_DRV_INTR_LSC,
773         .probe = eth_txgbe_pci_probe,
774         .remove = eth_txgbe_pci_remove,
775 };
776
777 static int
778 txgbe_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
779 {
780         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
781         struct txgbe_vfta *shadow_vfta = TXGBE_DEV_VFTA(dev);
782         uint32_t vfta;
783         uint32_t vid_idx;
784         uint32_t vid_bit;
785
786         vid_idx = (uint32_t)((vlan_id >> 5) & 0x7F);
787         vid_bit = (uint32_t)(1 << (vlan_id & 0x1F));
788         vfta = rd32(hw, TXGBE_VLANTBL(vid_idx));
789         if (on)
790                 vfta |= vid_bit;
791         else
792                 vfta &= ~vid_bit;
793         wr32(hw, TXGBE_VLANTBL(vid_idx), vfta);
794
795         /* update local VFTA copy */
796         shadow_vfta->vfta[vid_idx] = vfta;
797
798         return 0;
799 }
800
801 static void
802 txgbe_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
803 {
804         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
805         struct txgbe_rx_queue *rxq;
806         bool restart;
807         uint32_t rxcfg, rxbal, rxbah;
808
809         if (on)
810                 txgbe_vlan_hw_strip_enable(dev, queue);
811         else
812                 txgbe_vlan_hw_strip_disable(dev, queue);
813
814         rxq = dev->data->rx_queues[queue];
815         rxbal = rd32(hw, TXGBE_RXBAL(rxq->reg_idx));
816         rxbah = rd32(hw, TXGBE_RXBAH(rxq->reg_idx));
817         rxcfg = rd32(hw, TXGBE_RXCFG(rxq->reg_idx));
818         if (rxq->offloads & DEV_RX_OFFLOAD_VLAN_STRIP) {
819                 restart = (rxcfg & TXGBE_RXCFG_ENA) &&
820                         !(rxcfg & TXGBE_RXCFG_VLAN);
821                 rxcfg |= TXGBE_RXCFG_VLAN;
822         } else {
823                 restart = (rxcfg & TXGBE_RXCFG_ENA) &&
824                         (rxcfg & TXGBE_RXCFG_VLAN);
825                 rxcfg &= ~TXGBE_RXCFG_VLAN;
826         }
827         rxcfg &= ~TXGBE_RXCFG_ENA;
828
829         if (restart) {
830                 /* set vlan strip for ring */
831                 txgbe_dev_rx_queue_stop(dev, queue);
832                 wr32(hw, TXGBE_RXBAL(rxq->reg_idx), rxbal);
833                 wr32(hw, TXGBE_RXBAH(rxq->reg_idx), rxbah);
834                 wr32(hw, TXGBE_RXCFG(rxq->reg_idx), rxcfg);
835                 txgbe_dev_rx_queue_start(dev, queue);
836         }
837 }
838
839 static int
840 txgbe_vlan_tpid_set(struct rte_eth_dev *dev,
841                     enum rte_vlan_type vlan_type,
842                     uint16_t tpid)
843 {
844         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
845         int ret = 0;
846         uint32_t portctrl, vlan_ext, qinq;
847
848         portctrl = rd32(hw, TXGBE_PORTCTL);
849
850         vlan_ext = (portctrl & TXGBE_PORTCTL_VLANEXT);
851         qinq = vlan_ext && (portctrl & TXGBE_PORTCTL_QINQ);
852         switch (vlan_type) {
853         case ETH_VLAN_TYPE_INNER:
854                 if (vlan_ext) {
855                         wr32m(hw, TXGBE_VLANCTL,
856                                 TXGBE_VLANCTL_TPID_MASK,
857                                 TXGBE_VLANCTL_TPID(tpid));
858                         wr32m(hw, TXGBE_DMATXCTRL,
859                                 TXGBE_DMATXCTRL_TPID_MASK,
860                                 TXGBE_DMATXCTRL_TPID(tpid));
861                 } else {
862                         ret = -ENOTSUP;
863                         PMD_DRV_LOG(ERR, "Inner type is not supported"
864                                     " by single VLAN");
865                 }
866
867                 if (qinq) {
868                         wr32m(hw, TXGBE_TAGTPID(0),
869                                 TXGBE_TAGTPID_LSB_MASK,
870                                 TXGBE_TAGTPID_LSB(tpid));
871                 }
872                 break;
873         case ETH_VLAN_TYPE_OUTER:
874                 if (vlan_ext) {
875                         /* Only the high 16-bits is valid */
876                         wr32m(hw, TXGBE_EXTAG,
877                                 TXGBE_EXTAG_VLAN_MASK,
878                                 TXGBE_EXTAG_VLAN(tpid));
879                 } else {
880                         wr32m(hw, TXGBE_VLANCTL,
881                                 TXGBE_VLANCTL_TPID_MASK,
882                                 TXGBE_VLANCTL_TPID(tpid));
883                         wr32m(hw, TXGBE_DMATXCTRL,
884                                 TXGBE_DMATXCTRL_TPID_MASK,
885                                 TXGBE_DMATXCTRL_TPID(tpid));
886                 }
887
888                 if (qinq) {
889                         wr32m(hw, TXGBE_TAGTPID(0),
890                                 TXGBE_TAGTPID_MSB_MASK,
891                                 TXGBE_TAGTPID_MSB(tpid));
892                 }
893                 break;
894         default:
895                 PMD_DRV_LOG(ERR, "Unsupported VLAN type %d", vlan_type);
896                 return -EINVAL;
897         }
898
899         return ret;
900 }
901
902 void
903 txgbe_vlan_hw_filter_disable(struct rte_eth_dev *dev)
904 {
905         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
906         uint32_t vlnctrl;
907
908         PMD_INIT_FUNC_TRACE();
909
910         /* Filter Table Disable */
911         vlnctrl = rd32(hw, TXGBE_VLANCTL);
912         vlnctrl &= ~TXGBE_VLANCTL_VFE;
913         wr32(hw, TXGBE_VLANCTL, vlnctrl);
914 }
915
916 void
917 txgbe_vlan_hw_filter_enable(struct rte_eth_dev *dev)
918 {
919         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
920         struct txgbe_vfta *shadow_vfta = TXGBE_DEV_VFTA(dev);
921         uint32_t vlnctrl;
922         uint16_t i;
923
924         PMD_INIT_FUNC_TRACE();
925
926         /* Filter Table Enable */
927         vlnctrl = rd32(hw, TXGBE_VLANCTL);
928         vlnctrl &= ~TXGBE_VLANCTL_CFIENA;
929         vlnctrl |= TXGBE_VLANCTL_VFE;
930         wr32(hw, TXGBE_VLANCTL, vlnctrl);
931
932         /* write whatever is in local vfta copy */
933         for (i = 0; i < TXGBE_VFTA_SIZE; i++)
934                 wr32(hw, TXGBE_VLANTBL(i), shadow_vfta->vfta[i]);
935 }
936
937 void
938 txgbe_vlan_hw_strip_bitmap_set(struct rte_eth_dev *dev, uint16_t queue, bool on)
939 {
940         struct txgbe_hwstrip *hwstrip = TXGBE_DEV_HWSTRIP(dev);
941         struct txgbe_rx_queue *rxq;
942
943         if (queue >= TXGBE_MAX_RX_QUEUE_NUM)
944                 return;
945
946         if (on)
947                 TXGBE_SET_HWSTRIP(hwstrip, queue);
948         else
949                 TXGBE_CLEAR_HWSTRIP(hwstrip, queue);
950
951         if (queue >= dev->data->nb_rx_queues)
952                 return;
953
954         rxq = dev->data->rx_queues[queue];
955
956         if (on) {
957                 rxq->vlan_flags = PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
958                 rxq->offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
959         } else {
960                 rxq->vlan_flags = PKT_RX_VLAN;
961                 rxq->offloads &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
962         }
963 }
964
965 static void
966 txgbe_vlan_hw_strip_disable(struct rte_eth_dev *dev, uint16_t queue)
967 {
968         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
969         uint32_t ctrl;
970
971         PMD_INIT_FUNC_TRACE();
972
973         ctrl = rd32(hw, TXGBE_RXCFG(queue));
974         ctrl &= ~TXGBE_RXCFG_VLAN;
975         wr32(hw, TXGBE_RXCFG(queue), ctrl);
976
977         /* record those setting for HW strip per queue */
978         txgbe_vlan_hw_strip_bitmap_set(dev, queue, 0);
979 }
980
981 static void
982 txgbe_vlan_hw_strip_enable(struct rte_eth_dev *dev, uint16_t queue)
983 {
984         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
985         uint32_t ctrl;
986
987         PMD_INIT_FUNC_TRACE();
988
989         ctrl = rd32(hw, TXGBE_RXCFG(queue));
990         ctrl |= TXGBE_RXCFG_VLAN;
991         wr32(hw, TXGBE_RXCFG(queue), ctrl);
992
993         /* record those setting for HW strip per queue */
994         txgbe_vlan_hw_strip_bitmap_set(dev, queue, 1);
995 }
996
997 static void
998 txgbe_vlan_hw_extend_disable(struct rte_eth_dev *dev)
999 {
1000         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1001         uint32_t ctrl;
1002
1003         PMD_INIT_FUNC_TRACE();
1004
1005         ctrl = rd32(hw, TXGBE_PORTCTL);
1006         ctrl &= ~TXGBE_PORTCTL_VLANEXT;
1007         ctrl &= ~TXGBE_PORTCTL_QINQ;
1008         wr32(hw, TXGBE_PORTCTL, ctrl);
1009 }
1010
1011 static void
1012 txgbe_vlan_hw_extend_enable(struct rte_eth_dev *dev)
1013 {
1014         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1015         struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1016         struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
1017         uint32_t ctrl;
1018
1019         PMD_INIT_FUNC_TRACE();
1020
1021         ctrl  = rd32(hw, TXGBE_PORTCTL);
1022         ctrl |= TXGBE_PORTCTL_VLANEXT;
1023         if (rxmode->offloads & DEV_RX_OFFLOAD_QINQ_STRIP ||
1024             txmode->offloads & DEV_TX_OFFLOAD_QINQ_INSERT)
1025                 ctrl |= TXGBE_PORTCTL_QINQ;
1026         wr32(hw, TXGBE_PORTCTL, ctrl);
1027 }
1028
1029 void
1030 txgbe_vlan_hw_strip_config(struct rte_eth_dev *dev)
1031 {
1032         struct txgbe_rx_queue *rxq;
1033         uint16_t i;
1034
1035         PMD_INIT_FUNC_TRACE();
1036
1037         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1038                 rxq = dev->data->rx_queues[i];
1039
1040                 if (rxq->offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
1041                         txgbe_vlan_strip_queue_set(dev, i, 1);
1042                 else
1043                         txgbe_vlan_strip_queue_set(dev, i, 0);
1044         }
1045 }
1046
1047 void
1048 txgbe_config_vlan_strip_on_all_queues(struct rte_eth_dev *dev, int mask)
1049 {
1050         uint16_t i;
1051         struct rte_eth_rxmode *rxmode;
1052         struct txgbe_rx_queue *rxq;
1053
1054         if (mask & ETH_VLAN_STRIP_MASK) {
1055                 rxmode = &dev->data->dev_conf.rxmode;
1056                 if (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
1057                         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1058                                 rxq = dev->data->rx_queues[i];
1059                                 rxq->offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
1060                         }
1061                 else
1062                         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1063                                 rxq = dev->data->rx_queues[i];
1064                                 rxq->offloads &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
1065                         }
1066         }
1067 }
1068
1069 static int
1070 txgbe_vlan_offload_config(struct rte_eth_dev *dev, int mask)
1071 {
1072         struct rte_eth_rxmode *rxmode;
1073         rxmode = &dev->data->dev_conf.rxmode;
1074
1075         if (mask & ETH_VLAN_STRIP_MASK)
1076                 txgbe_vlan_hw_strip_config(dev);
1077
1078         if (mask & ETH_VLAN_FILTER_MASK) {
1079                 if (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
1080                         txgbe_vlan_hw_filter_enable(dev);
1081                 else
1082                         txgbe_vlan_hw_filter_disable(dev);
1083         }
1084
1085         if (mask & ETH_VLAN_EXTEND_MASK) {
1086                 if (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_EXTEND)
1087                         txgbe_vlan_hw_extend_enable(dev);
1088                 else
1089                         txgbe_vlan_hw_extend_disable(dev);
1090         }
1091
1092         return 0;
1093 }
1094
1095 static int
1096 txgbe_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1097 {
1098         txgbe_config_vlan_strip_on_all_queues(dev, mask);
1099
1100         txgbe_vlan_offload_config(dev, mask);
1101
1102         return 0;
1103 }
1104
1105 static void
1106 txgbe_vmdq_vlan_hw_filter_enable(struct rte_eth_dev *dev)
1107 {
1108         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1109         /* VLNCTL: enable vlan filtering and allow all vlan tags through */
1110         uint32_t vlanctrl = rd32(hw, TXGBE_VLANCTL);
1111
1112         vlanctrl |= TXGBE_VLANCTL_VFE; /* enable vlan filters */
1113         wr32(hw, TXGBE_VLANCTL, vlanctrl);
1114 }
1115
1116 static int
1117 txgbe_check_vf_rss_rxq_num(struct rte_eth_dev *dev, uint16_t nb_rx_q)
1118 {
1119         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1120
1121         switch (nb_rx_q) {
1122         case 1:
1123         case 2:
1124                 RTE_ETH_DEV_SRIOV(dev).active = ETH_64_POOLS;
1125                 break;
1126         case 4:
1127                 RTE_ETH_DEV_SRIOV(dev).active = ETH_32_POOLS;
1128                 break;
1129         default:
1130                 return -EINVAL;
1131         }
1132
1133         RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool =
1134                 TXGBE_MAX_RX_QUEUE_NUM / RTE_ETH_DEV_SRIOV(dev).active;
1135         RTE_ETH_DEV_SRIOV(dev).def_pool_q_idx =
1136                 pci_dev->max_vfs * RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
1137         return 0;
1138 }
1139
1140 static int
1141 txgbe_check_mq_mode(struct rte_eth_dev *dev)
1142 {
1143         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
1144         uint16_t nb_rx_q = dev->data->nb_rx_queues;
1145         uint16_t nb_tx_q = dev->data->nb_tx_queues;
1146
1147         if (RTE_ETH_DEV_SRIOV(dev).active != 0) {
1148                 /* check multi-queue mode */
1149                 switch (dev_conf->rxmode.mq_mode) {
1150                 case ETH_MQ_RX_VMDQ_DCB:
1151                         PMD_INIT_LOG(INFO, "ETH_MQ_RX_VMDQ_DCB mode supported in SRIOV");
1152                         break;
1153                 case ETH_MQ_RX_VMDQ_DCB_RSS:
1154                         /* DCB/RSS VMDQ in SRIOV mode, not implement yet */
1155                         PMD_INIT_LOG(ERR, "SRIOV active,"
1156                                         " unsupported mq_mode rx %d.",
1157                                         dev_conf->rxmode.mq_mode);
1158                         return -EINVAL;
1159                 case ETH_MQ_RX_RSS:
1160                 case ETH_MQ_RX_VMDQ_RSS:
1161                         dev->data->dev_conf.rxmode.mq_mode = ETH_MQ_RX_VMDQ_RSS;
1162                         if (nb_rx_q <= RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool)
1163                                 if (txgbe_check_vf_rss_rxq_num(dev, nb_rx_q)) {
1164                                         PMD_INIT_LOG(ERR, "SRIOV is active,"
1165                                                 " invalid queue number"
1166                                                 " for VMDQ RSS, allowed"
1167                                                 " value are 1, 2 or 4.");
1168                                         return -EINVAL;
1169                                 }
1170                         break;
1171                 case ETH_MQ_RX_VMDQ_ONLY:
1172                 case ETH_MQ_RX_NONE:
1173                         /* if nothing mq mode configure, use default scheme */
1174                         dev->data->dev_conf.rxmode.mq_mode =
1175                                 ETH_MQ_RX_VMDQ_ONLY;
1176                         break;
1177                 default: /* ETH_MQ_RX_DCB, ETH_MQ_RX_DCB_RSS or ETH_MQ_TX_DCB*/
1178                         /* SRIOV only works in VMDq enable mode */
1179                         PMD_INIT_LOG(ERR, "SRIOV is active,"
1180                                         " wrong mq_mode rx %d.",
1181                                         dev_conf->rxmode.mq_mode);
1182                         return -EINVAL;
1183                 }
1184
1185                 switch (dev_conf->txmode.mq_mode) {
1186                 case ETH_MQ_TX_VMDQ_DCB:
1187                         PMD_INIT_LOG(INFO, "ETH_MQ_TX_VMDQ_DCB mode supported in SRIOV");
1188                         dev->data->dev_conf.txmode.mq_mode = ETH_MQ_TX_VMDQ_DCB;
1189                         break;
1190                 default: /* ETH_MQ_TX_VMDQ_ONLY or ETH_MQ_TX_NONE */
1191                         dev->data->dev_conf.txmode.mq_mode =
1192                                 ETH_MQ_TX_VMDQ_ONLY;
1193                         break;
1194                 }
1195
1196                 /* check valid queue number */
1197                 if ((nb_rx_q > RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool) ||
1198                     (nb_tx_q > RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool)) {
1199                         PMD_INIT_LOG(ERR, "SRIOV is active,"
1200                                         " nb_rx_q=%d nb_tx_q=%d queue number"
1201                                         " must be less than or equal to %d.",
1202                                         nb_rx_q, nb_tx_q,
1203                                         RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool);
1204                         return -EINVAL;
1205                 }
1206         } else {
1207                 if (dev_conf->rxmode.mq_mode == ETH_MQ_RX_VMDQ_DCB_RSS) {
1208                         PMD_INIT_LOG(ERR, "VMDQ+DCB+RSS mq_mode is"
1209                                           " not supported.");
1210                         return -EINVAL;
1211                 }
1212                 /* check configuration for vmdb+dcb mode */
1213                 if (dev_conf->rxmode.mq_mode == ETH_MQ_RX_VMDQ_DCB) {
1214                         const struct rte_eth_vmdq_dcb_conf *conf;
1215
1216                         if (nb_rx_q != TXGBE_VMDQ_DCB_NB_QUEUES) {
1217                                 PMD_INIT_LOG(ERR, "VMDQ+DCB, nb_rx_q != %d.",
1218                                                 TXGBE_VMDQ_DCB_NB_QUEUES);
1219                                 return -EINVAL;
1220                         }
1221                         conf = &dev_conf->rx_adv_conf.vmdq_dcb_conf;
1222                         if (!(conf->nb_queue_pools == ETH_16_POOLS ||
1223                                conf->nb_queue_pools == ETH_32_POOLS)) {
1224                                 PMD_INIT_LOG(ERR, "VMDQ+DCB selected,"
1225                                                 " nb_queue_pools must be %d or %d.",
1226                                                 ETH_16_POOLS, ETH_32_POOLS);
1227                                 return -EINVAL;
1228                         }
1229                 }
1230                 if (dev_conf->txmode.mq_mode == ETH_MQ_TX_VMDQ_DCB) {
1231                         const struct rte_eth_vmdq_dcb_tx_conf *conf;
1232
1233                         if (nb_tx_q != TXGBE_VMDQ_DCB_NB_QUEUES) {
1234                                 PMD_INIT_LOG(ERR, "VMDQ+DCB, nb_tx_q != %d",
1235                                                  TXGBE_VMDQ_DCB_NB_QUEUES);
1236                                 return -EINVAL;
1237                         }
1238                         conf = &dev_conf->tx_adv_conf.vmdq_dcb_tx_conf;
1239                         if (!(conf->nb_queue_pools == ETH_16_POOLS ||
1240                                conf->nb_queue_pools == ETH_32_POOLS)) {
1241                                 PMD_INIT_LOG(ERR, "VMDQ+DCB selected,"
1242                                                 " nb_queue_pools != %d and"
1243                                                 " nb_queue_pools != %d.",
1244                                                 ETH_16_POOLS, ETH_32_POOLS);
1245                                 return -EINVAL;
1246                         }
1247                 }
1248
1249                 /* For DCB mode check our configuration before we go further */
1250                 if (dev_conf->rxmode.mq_mode == ETH_MQ_RX_DCB) {
1251                         const struct rte_eth_dcb_rx_conf *conf;
1252
1253                         conf = &dev_conf->rx_adv_conf.dcb_rx_conf;
1254                         if (!(conf->nb_tcs == ETH_4_TCS ||
1255                                conf->nb_tcs == ETH_8_TCS)) {
1256                                 PMD_INIT_LOG(ERR, "DCB selected, nb_tcs != %d"
1257                                                 " and nb_tcs != %d.",
1258                                                 ETH_4_TCS, ETH_8_TCS);
1259                                 return -EINVAL;
1260                         }
1261                 }
1262
1263                 if (dev_conf->txmode.mq_mode == ETH_MQ_TX_DCB) {
1264                         const struct rte_eth_dcb_tx_conf *conf;
1265
1266                         conf = &dev_conf->tx_adv_conf.dcb_tx_conf;
1267                         if (!(conf->nb_tcs == ETH_4_TCS ||
1268                                conf->nb_tcs == ETH_8_TCS)) {
1269                                 PMD_INIT_LOG(ERR, "DCB selected, nb_tcs != %d"
1270                                                 " and nb_tcs != %d.",
1271                                                 ETH_4_TCS, ETH_8_TCS);
1272                                 return -EINVAL;
1273                         }
1274                 }
1275         }
1276         return 0;
1277 }
1278
1279 static int
1280 txgbe_dev_configure(struct rte_eth_dev *dev)
1281 {
1282         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
1283         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
1284         int ret;
1285
1286         PMD_INIT_FUNC_TRACE();
1287
1288         if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
1289                 dev->data->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
1290
1291         /* multiple queue mode checking */
1292         ret  = txgbe_check_mq_mode(dev);
1293         if (ret != 0) {
1294                 PMD_DRV_LOG(ERR, "txgbe_check_mq_mode fails with %d.",
1295                             ret);
1296                 return ret;
1297         }
1298
1299         /* set flag to update link status after init */
1300         intr->flags |= TXGBE_FLAG_NEED_LINK_UPDATE;
1301
1302         /*
1303          * Initialize to TRUE. If any of Rx queues doesn't meet the bulk
1304          * allocation Rx preconditions we will reset it.
1305          */
1306         adapter->rx_bulk_alloc_allowed = true;
1307
1308         return 0;
1309 }
1310
1311 static void
1312 txgbe_dev_phy_intr_setup(struct rte_eth_dev *dev)
1313 {
1314         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1315         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
1316         uint32_t gpie;
1317
1318         gpie = rd32(hw, TXGBE_GPIOINTEN);
1319         gpie |= TXGBE_GPIOBIT_6;
1320         wr32(hw, TXGBE_GPIOINTEN, gpie);
1321         intr->mask_misc |= TXGBE_ICRMISC_GPIO;
1322 }
1323
1324 int
1325 txgbe_set_vf_rate_limit(struct rte_eth_dev *dev, uint16_t vf,
1326                         uint16_t tx_rate, uint64_t q_msk)
1327 {
1328         struct txgbe_hw *hw;
1329         struct txgbe_vf_info *vfinfo;
1330         struct rte_eth_link link;
1331         uint8_t  nb_q_per_pool;
1332         uint32_t queue_stride;
1333         uint32_t queue_idx, idx = 0, vf_idx;
1334         uint32_t queue_end;
1335         uint16_t total_rate = 0;
1336         struct rte_pci_device *pci_dev;
1337         int ret;
1338
1339         pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1340         ret = rte_eth_link_get_nowait(dev->data->port_id, &link);
1341         if (ret < 0)
1342                 return ret;
1343
1344         if (vf >= pci_dev->max_vfs)
1345                 return -EINVAL;
1346
1347         if (tx_rate > link.link_speed)
1348                 return -EINVAL;
1349
1350         if (q_msk == 0)
1351                 return 0;
1352
1353         hw = TXGBE_DEV_HW(dev);
1354         vfinfo = *(TXGBE_DEV_VFDATA(dev));
1355         nb_q_per_pool = RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
1356         queue_stride = TXGBE_MAX_RX_QUEUE_NUM / RTE_ETH_DEV_SRIOV(dev).active;
1357         queue_idx = vf * queue_stride;
1358         queue_end = queue_idx + nb_q_per_pool - 1;
1359         if (queue_end >= hw->mac.max_tx_queues)
1360                 return -EINVAL;
1361
1362         if (vfinfo) {
1363                 for (vf_idx = 0; vf_idx < pci_dev->max_vfs; vf_idx++) {
1364                         if (vf_idx == vf)
1365                                 continue;
1366                         for (idx = 0; idx < RTE_DIM(vfinfo[vf_idx].tx_rate);
1367                                 idx++)
1368                                 total_rate += vfinfo[vf_idx].tx_rate[idx];
1369                 }
1370         } else {
1371                 return -EINVAL;
1372         }
1373
1374         /* Store tx_rate for this vf. */
1375         for (idx = 0; idx < nb_q_per_pool; idx++) {
1376                 if (((uint64_t)0x1 << idx) & q_msk) {
1377                         if (vfinfo[vf].tx_rate[idx] != tx_rate)
1378                                 vfinfo[vf].tx_rate[idx] = tx_rate;
1379                         total_rate += tx_rate;
1380                 }
1381         }
1382
1383         if (total_rate > dev->data->dev_link.link_speed) {
1384                 /* Reset stored TX rate of the VF if it causes exceed
1385                  * link speed.
1386                  */
1387                 memset(vfinfo[vf].tx_rate, 0, sizeof(vfinfo[vf].tx_rate));
1388                 return -EINVAL;
1389         }
1390
1391         /* Set ARBTXRATE of each queue/pool for vf X  */
1392         for (; queue_idx <= queue_end; queue_idx++) {
1393                 if (0x1 & q_msk)
1394                         txgbe_set_queue_rate_limit(dev, queue_idx, tx_rate);
1395                 q_msk = q_msk >> 1;
1396         }
1397
1398         return 0;
1399 }
1400
1401 /*
1402  * Configure device link speed and setup link.
1403  * It returns 0 on success.
1404  */
1405 static int
1406 txgbe_dev_start(struct rte_eth_dev *dev)
1407 {
1408         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1409         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
1410         struct txgbe_vf_info *vfinfo = *TXGBE_DEV_VFDATA(dev);
1411         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1412         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1413         uint32_t intr_vector = 0;
1414         int err;
1415         bool link_up = false, negotiate = 0;
1416         uint32_t speed = 0;
1417         uint32_t allowed_speeds = 0;
1418         int mask = 0;
1419         int status;
1420         uint16_t vf, idx;
1421         uint32_t *link_speeds;
1422
1423         PMD_INIT_FUNC_TRACE();
1424
1425         /* TXGBE devices don't support:
1426          *    - half duplex (checked afterwards for valid speeds)
1427          *    - fixed speed: TODO implement
1428          */
1429         if (dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED) {
1430                 PMD_INIT_LOG(ERR,
1431                 "Invalid link_speeds for port %u, fix speed not supported",
1432                                 dev->data->port_id);
1433                 return -EINVAL;
1434         }
1435
1436         /* Stop the link setup handler before resetting the HW. */
1437         rte_eal_alarm_cancel(txgbe_dev_setup_link_alarm_handler, dev);
1438
1439         /* disable uio/vfio intr/eventfd mapping */
1440         rte_intr_disable(intr_handle);
1441
1442         /* stop adapter */
1443         hw->adapter_stopped = 0;
1444         txgbe_stop_hw(hw);
1445
1446         /* reinitialize adapter
1447          * this calls reset and start
1448          */
1449         hw->nb_rx_queues = dev->data->nb_rx_queues;
1450         hw->nb_tx_queues = dev->data->nb_tx_queues;
1451         status = txgbe_pf_reset_hw(hw);
1452         if (status != 0)
1453                 return -1;
1454         hw->mac.start_hw(hw);
1455         hw->mac.get_link_status = true;
1456
1457         /* configure PF module if SRIOV enabled */
1458         txgbe_pf_host_configure(dev);
1459
1460         txgbe_dev_phy_intr_setup(dev);
1461
1462         /* check and configure queue intr-vector mapping */
1463         if ((rte_intr_cap_multiple(intr_handle) ||
1464              !RTE_ETH_DEV_SRIOV(dev).active) &&
1465             dev->data->dev_conf.intr_conf.rxq != 0) {
1466                 intr_vector = dev->data->nb_rx_queues;
1467                 if (rte_intr_efd_enable(intr_handle, intr_vector))
1468                         return -1;
1469         }
1470
1471         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
1472                 intr_handle->intr_vec =
1473                         rte_zmalloc("intr_vec",
1474                                     dev->data->nb_rx_queues * sizeof(int), 0);
1475                 if (intr_handle->intr_vec == NULL) {
1476                         PMD_INIT_LOG(ERR, "Failed to allocate %d rx_queues"
1477                                      " intr_vec", dev->data->nb_rx_queues);
1478                         return -ENOMEM;
1479                 }
1480         }
1481
1482         /* confiugre msix for sleep until rx interrupt */
1483         txgbe_configure_msix(dev);
1484
1485         /* initialize transmission unit */
1486         txgbe_dev_tx_init(dev);
1487
1488         /* This can fail when allocating mbufs for descriptor rings */
1489         err = txgbe_dev_rx_init(dev);
1490         if (err) {
1491                 PMD_INIT_LOG(ERR, "Unable to initialize RX hardware");
1492                 goto error;
1493         }
1494
1495         mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK |
1496                 ETH_VLAN_EXTEND_MASK;
1497         err = txgbe_vlan_offload_config(dev, mask);
1498         if (err) {
1499                 PMD_INIT_LOG(ERR, "Unable to set VLAN offload");
1500                 goto error;
1501         }
1502
1503         if (dev->data->dev_conf.rxmode.mq_mode == ETH_MQ_RX_VMDQ_ONLY) {
1504                 /* Enable vlan filtering for VMDq */
1505                 txgbe_vmdq_vlan_hw_filter_enable(dev);
1506         }
1507
1508         /* Configure DCB hw */
1509         txgbe_configure_pb(dev);
1510         txgbe_configure_port(dev);
1511         txgbe_configure_dcb(dev);
1512
1513         /* Restore vf rate limit */
1514         if (vfinfo != NULL) {
1515                 for (vf = 0; vf < pci_dev->max_vfs; vf++)
1516                         for (idx = 0; idx < TXGBE_MAX_QUEUE_NUM_PER_VF; idx++)
1517                                 if (vfinfo[vf].tx_rate[idx] != 0)
1518                                         txgbe_set_vf_rate_limit(dev, vf,
1519                                                 vfinfo[vf].tx_rate[idx],
1520                                                 1 << idx);
1521         }
1522
1523         err = txgbe_dev_rxtx_start(dev);
1524         if (err < 0) {
1525                 PMD_INIT_LOG(ERR, "Unable to start rxtx queues");
1526                 goto error;
1527         }
1528
1529         /* Skip link setup if loopback mode is enabled. */
1530         if (hw->mac.type == txgbe_mac_raptor &&
1531             dev->data->dev_conf.lpbk_mode)
1532                 goto skip_link_setup;
1533
1534         if (txgbe_is_sfp(hw) && hw->phy.multispeed_fiber) {
1535                 err = hw->mac.setup_sfp(hw);
1536                 if (err)
1537                         goto error;
1538         }
1539
1540         if (hw->phy.media_type == txgbe_media_type_copper) {
1541                 /* Turn on the copper */
1542                 hw->phy.set_phy_power(hw, true);
1543         } else {
1544                 /* Turn on the laser */
1545                 hw->mac.enable_tx_laser(hw);
1546         }
1547
1548         err = hw->mac.check_link(hw, &speed, &link_up, 0);
1549         if (err)
1550                 goto error;
1551         dev->data->dev_link.link_status = link_up;
1552
1553         err = hw->mac.get_link_capabilities(hw, &speed, &negotiate);
1554         if (err)
1555                 goto error;
1556
1557         allowed_speeds = ETH_LINK_SPEED_100M | ETH_LINK_SPEED_1G |
1558                         ETH_LINK_SPEED_10G;
1559
1560         link_speeds = &dev->data->dev_conf.link_speeds;
1561         if (*link_speeds & ~allowed_speeds) {
1562                 PMD_INIT_LOG(ERR, "Invalid link setting");
1563                 goto error;
1564         }
1565
1566         speed = 0x0;
1567         if (*link_speeds == ETH_LINK_SPEED_AUTONEG) {
1568                 speed = (TXGBE_LINK_SPEED_100M_FULL |
1569                          TXGBE_LINK_SPEED_1GB_FULL |
1570                          TXGBE_LINK_SPEED_10GB_FULL);
1571         } else {
1572                 if (*link_speeds & ETH_LINK_SPEED_10G)
1573                         speed |= TXGBE_LINK_SPEED_10GB_FULL;
1574                 if (*link_speeds & ETH_LINK_SPEED_5G)
1575                         speed |= TXGBE_LINK_SPEED_5GB_FULL;
1576                 if (*link_speeds & ETH_LINK_SPEED_2_5G)
1577                         speed |= TXGBE_LINK_SPEED_2_5GB_FULL;
1578                 if (*link_speeds & ETH_LINK_SPEED_1G)
1579                         speed |= TXGBE_LINK_SPEED_1GB_FULL;
1580                 if (*link_speeds & ETH_LINK_SPEED_100M)
1581                         speed |= TXGBE_LINK_SPEED_100M_FULL;
1582         }
1583
1584         err = hw->mac.setup_link(hw, speed, link_up);
1585         if (err)
1586                 goto error;
1587
1588 skip_link_setup:
1589
1590         if (rte_intr_allow_others(intr_handle)) {
1591                 /* check if lsc interrupt is enabled */
1592                 if (dev->data->dev_conf.intr_conf.lsc != 0)
1593                         txgbe_dev_lsc_interrupt_setup(dev, TRUE);
1594                 else
1595                         txgbe_dev_lsc_interrupt_setup(dev, FALSE);
1596                 txgbe_dev_macsec_interrupt_setup(dev);
1597                 txgbe_set_ivar_map(hw, -1, 1, TXGBE_MISC_VEC_ID);
1598         } else {
1599                 rte_intr_callback_unregister(intr_handle,
1600                                              txgbe_dev_interrupt_handler, dev);
1601                 if (dev->data->dev_conf.intr_conf.lsc != 0)
1602                         PMD_INIT_LOG(INFO, "lsc won't enable because of"
1603                                      " no intr multiplex");
1604         }
1605
1606         /* check if rxq interrupt is enabled */
1607         if (dev->data->dev_conf.intr_conf.rxq != 0 &&
1608             rte_intr_dp_is_en(intr_handle))
1609                 txgbe_dev_rxq_interrupt_setup(dev);
1610
1611         /* enable uio/vfio intr/eventfd mapping */
1612         rte_intr_enable(intr_handle);
1613
1614         /* resume enabled intr since hw reset */
1615         txgbe_enable_intr(dev);
1616         txgbe_filter_restore(dev);
1617
1618         /*
1619          * Update link status right before return, because it may
1620          * start link configuration process in a separate thread.
1621          */
1622         txgbe_dev_link_update(dev, 0);
1623
1624         wr32m(hw, TXGBE_LEDCTL, 0xFFFFFFFF, TXGBE_LEDCTL_ORD_MASK);
1625
1626         txgbe_read_stats_registers(hw, hw_stats);
1627         hw->offset_loaded = 1;
1628
1629         return 0;
1630
1631 error:
1632         PMD_INIT_LOG(ERR, "failure in dev start: %d", err);
1633         txgbe_dev_clear_queues(dev);
1634         return -EIO;
1635 }
1636
1637 /*
1638  * Stop device: disable rx and tx functions to allow for reconfiguring.
1639  */
1640 static int
1641 txgbe_dev_stop(struct rte_eth_dev *dev)
1642 {
1643         struct rte_eth_link link;
1644         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
1645         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1646         struct txgbe_vf_info *vfinfo = *TXGBE_DEV_VFDATA(dev);
1647         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1648         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1649         int vf;
1650
1651         if (hw->adapter_stopped)
1652                 return 0;
1653
1654         PMD_INIT_FUNC_TRACE();
1655
1656         rte_eal_alarm_cancel(txgbe_dev_setup_link_alarm_handler, dev);
1657
1658         /* disable interrupts */
1659         txgbe_disable_intr(hw);
1660
1661         /* reset the NIC */
1662         txgbe_pf_reset_hw(hw);
1663         hw->adapter_stopped = 0;
1664
1665         /* stop adapter */
1666         txgbe_stop_hw(hw);
1667
1668         for (vf = 0; vfinfo != NULL && vf < pci_dev->max_vfs; vf++)
1669                 vfinfo[vf].clear_to_send = false;
1670
1671         if (hw->phy.media_type == txgbe_media_type_copper) {
1672                 /* Turn off the copper */
1673                 hw->phy.set_phy_power(hw, false);
1674         } else {
1675                 /* Turn off the laser */
1676                 hw->mac.disable_tx_laser(hw);
1677         }
1678
1679         txgbe_dev_clear_queues(dev);
1680
1681         /* Clear stored conf */
1682         dev->data->scattered_rx = 0;
1683         dev->data->lro = 0;
1684
1685         /* Clear recorded link status */
1686         memset(&link, 0, sizeof(link));
1687         rte_eth_linkstatus_set(dev, &link);
1688
1689         if (!rte_intr_allow_others(intr_handle))
1690                 /* resume to the default handler */
1691                 rte_intr_callback_register(intr_handle,
1692                                            txgbe_dev_interrupt_handler,
1693                                            (void *)dev);
1694
1695         /* Clean datapath event and queue/vec mapping */
1696         rte_intr_efd_disable(intr_handle);
1697         if (intr_handle->intr_vec != NULL) {
1698                 rte_free(intr_handle->intr_vec);
1699                 intr_handle->intr_vec = NULL;
1700         }
1701
1702         adapter->rss_reta_updated = 0;
1703         wr32m(hw, TXGBE_LEDCTL, 0xFFFFFFFF, TXGBE_LEDCTL_SEL_MASK);
1704
1705         hw->adapter_stopped = true;
1706         dev->data->dev_started = 0;
1707
1708         return 0;
1709 }
1710
1711 /*
1712  * Set device link up: enable tx.
1713  */
1714 static int
1715 txgbe_dev_set_link_up(struct rte_eth_dev *dev)
1716 {
1717         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1718
1719         if (hw->phy.media_type == txgbe_media_type_copper) {
1720                 /* Turn on the copper */
1721                 hw->phy.set_phy_power(hw, true);
1722         } else {
1723                 /* Turn on the laser */
1724                 hw->mac.enable_tx_laser(hw);
1725                 txgbe_dev_link_update(dev, 0);
1726         }
1727
1728         return 0;
1729 }
1730
1731 /*
1732  * Set device link down: disable tx.
1733  */
1734 static int
1735 txgbe_dev_set_link_down(struct rte_eth_dev *dev)
1736 {
1737         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1738
1739         if (hw->phy.media_type == txgbe_media_type_copper) {
1740                 /* Turn off the copper */
1741                 hw->phy.set_phy_power(hw, false);
1742         } else {
1743                 /* Turn off the laser */
1744                 hw->mac.disable_tx_laser(hw);
1745                 txgbe_dev_link_update(dev, 0);
1746         }
1747
1748         return 0;
1749 }
1750
1751 /*
1752  * Reset and stop device.
1753  */
1754 static int
1755 txgbe_dev_close(struct rte_eth_dev *dev)
1756 {
1757         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1758         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1759         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1760         int retries = 0;
1761         int ret;
1762
1763         PMD_INIT_FUNC_TRACE();
1764
1765         txgbe_pf_reset_hw(hw);
1766
1767         ret = txgbe_dev_stop(dev);
1768
1769         txgbe_dev_free_queues(dev);
1770
1771         /* reprogram the RAR[0] in case user changed it. */
1772         txgbe_set_rar(hw, 0, hw->mac.addr, 0, true);
1773
1774         /* Unlock any pending hardware semaphore */
1775         txgbe_swfw_lock_reset(hw);
1776
1777         /* disable uio intr before callback unregister */
1778         rte_intr_disable(intr_handle);
1779
1780         do {
1781                 ret = rte_intr_callback_unregister(intr_handle,
1782                                 txgbe_dev_interrupt_handler, dev);
1783                 if (ret >= 0 || ret == -ENOENT) {
1784                         break;
1785                 } else if (ret != -EAGAIN) {
1786                         PMD_INIT_LOG(ERR,
1787                                 "intr callback unregister failed: %d",
1788                                 ret);
1789                 }
1790                 rte_delay_ms(100);
1791         } while (retries++ < (10 + TXGBE_LINK_UP_TIME));
1792
1793         /* cancel the delay handler before remove dev */
1794         rte_eal_alarm_cancel(txgbe_dev_interrupt_delayed_handler, dev);
1795
1796         /* uninitialize PF if max_vfs not zero */
1797         txgbe_pf_host_uninit(dev);
1798
1799         rte_free(dev->data->mac_addrs);
1800         dev->data->mac_addrs = NULL;
1801
1802         rte_free(dev->data->hash_mac_addrs);
1803         dev->data->hash_mac_addrs = NULL;
1804
1805         /* Remove all ntuple filters of the device */
1806         txgbe_ntuple_filter_uninit(dev);
1807
1808         return ret;
1809 }
1810
1811 /*
1812  * Reset PF device.
1813  */
1814 static int
1815 txgbe_dev_reset(struct rte_eth_dev *dev)
1816 {
1817         int ret;
1818
1819         /* When a DPDK PMD PF begin to reset PF port, it should notify all
1820          * its VF to make them align with it. The detailed notification
1821          * mechanism is PMD specific. As to txgbe PF, it is rather complex.
1822          * To avoid unexpected behavior in VF, currently reset of PF with
1823          * SR-IOV activation is not supported. It might be supported later.
1824          */
1825         if (dev->data->sriov.active)
1826                 return -ENOTSUP;
1827
1828         ret = eth_txgbe_dev_uninit(dev);
1829         if (ret)
1830                 return ret;
1831
1832         ret = eth_txgbe_dev_init(dev, NULL);
1833
1834         return ret;
1835 }
1836
1837 #define UPDATE_QP_COUNTER_32bit(reg, last_counter, counter)     \
1838         {                                                       \
1839                 uint32_t current_counter = rd32(hw, reg);       \
1840                 if (current_counter < last_counter)             \
1841                         current_counter += 0x100000000LL;       \
1842                 if (!hw->offset_loaded)                         \
1843                         last_counter = current_counter;         \
1844                 counter = current_counter - last_counter;       \
1845                 counter &= 0xFFFFFFFFLL;                        \
1846         }
1847
1848 #define UPDATE_QP_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
1849         {                                                                \
1850                 uint64_t current_counter_lsb = rd32(hw, reg_lsb);        \
1851                 uint64_t current_counter_msb = rd32(hw, reg_msb);        \
1852                 uint64_t current_counter = (current_counter_msb << 32) | \
1853                         current_counter_lsb;                             \
1854                 if (current_counter < last_counter)                      \
1855                         current_counter += 0x1000000000LL;               \
1856                 if (!hw->offset_loaded)                                  \
1857                         last_counter = current_counter;                  \
1858                 counter = current_counter - last_counter;                \
1859                 counter &= 0xFFFFFFFFFLL;                                \
1860         }
1861
1862 void
1863 txgbe_read_stats_registers(struct txgbe_hw *hw,
1864                            struct txgbe_hw_stats *hw_stats)
1865 {
1866         unsigned int i;
1867
1868         /* QP Stats */
1869         for (i = 0; i < hw->nb_rx_queues; i++) {
1870                 UPDATE_QP_COUNTER_32bit(TXGBE_QPRXPKT(i),
1871                         hw->qp_last[i].rx_qp_packets,
1872                         hw_stats->qp[i].rx_qp_packets);
1873                 UPDATE_QP_COUNTER_36bit(TXGBE_QPRXOCTL(i), TXGBE_QPRXOCTH(i),
1874                         hw->qp_last[i].rx_qp_bytes,
1875                         hw_stats->qp[i].rx_qp_bytes);
1876                 UPDATE_QP_COUNTER_32bit(TXGBE_QPRXMPKT(i),
1877                         hw->qp_last[i].rx_qp_mc_packets,
1878                         hw_stats->qp[i].rx_qp_mc_packets);
1879         }
1880
1881         for (i = 0; i < hw->nb_tx_queues; i++) {
1882                 UPDATE_QP_COUNTER_32bit(TXGBE_QPTXPKT(i),
1883                         hw->qp_last[i].tx_qp_packets,
1884                         hw_stats->qp[i].tx_qp_packets);
1885                 UPDATE_QP_COUNTER_36bit(TXGBE_QPTXOCTL(i), TXGBE_QPTXOCTH(i),
1886                         hw->qp_last[i].tx_qp_bytes,
1887                         hw_stats->qp[i].tx_qp_bytes);
1888         }
1889         /* PB Stats */
1890         for (i = 0; i < TXGBE_MAX_UP; i++) {
1891                 hw_stats->up[i].rx_up_xon_packets +=
1892                                 rd32(hw, TXGBE_PBRXUPXON(i));
1893                 hw_stats->up[i].rx_up_xoff_packets +=
1894                                 rd32(hw, TXGBE_PBRXUPXOFF(i));
1895                 hw_stats->up[i].tx_up_xon_packets +=
1896                                 rd32(hw, TXGBE_PBTXUPXON(i));
1897                 hw_stats->up[i].tx_up_xoff_packets +=
1898                                 rd32(hw, TXGBE_PBTXUPXOFF(i));
1899                 hw_stats->up[i].tx_up_xon2off_packets +=
1900                                 rd32(hw, TXGBE_PBTXUPOFF(i));
1901                 hw_stats->up[i].rx_up_dropped +=
1902                                 rd32(hw, TXGBE_PBRXMISS(i));
1903         }
1904         hw_stats->rx_xon_packets += rd32(hw, TXGBE_PBRXLNKXON);
1905         hw_stats->rx_xoff_packets += rd32(hw, TXGBE_PBRXLNKXOFF);
1906         hw_stats->tx_xon_packets += rd32(hw, TXGBE_PBTXLNKXON);
1907         hw_stats->tx_xoff_packets += rd32(hw, TXGBE_PBTXLNKXOFF);
1908
1909         /* DMA Stats */
1910         hw_stats->rx_packets += rd32(hw, TXGBE_DMARXPKT);
1911         hw_stats->tx_packets += rd32(hw, TXGBE_DMATXPKT);
1912
1913         hw_stats->rx_bytes += rd64(hw, TXGBE_DMARXOCTL);
1914         hw_stats->tx_bytes += rd64(hw, TXGBE_DMATXOCTL);
1915         hw_stats->rx_drop_packets += rd32(hw, TXGBE_PBRXDROP);
1916
1917         /* MAC Stats */
1918         hw_stats->rx_crc_errors += rd64(hw, TXGBE_MACRXERRCRCL);
1919         hw_stats->rx_multicast_packets += rd64(hw, TXGBE_MACRXMPKTL);
1920         hw_stats->tx_multicast_packets += rd64(hw, TXGBE_MACTXMPKTL);
1921
1922         hw_stats->rx_total_packets += rd64(hw, TXGBE_MACRXPKTL);
1923         hw_stats->tx_total_packets += rd64(hw, TXGBE_MACTXPKTL);
1924         hw_stats->rx_total_bytes += rd64(hw, TXGBE_MACRXGBOCTL);
1925
1926         hw_stats->rx_broadcast_packets += rd64(hw, TXGBE_MACRXOCTL);
1927         hw_stats->tx_broadcast_packets += rd32(hw, TXGBE_MACTXOCTL);
1928
1929         hw_stats->rx_size_64_packets += rd64(hw, TXGBE_MACRX1TO64L);
1930         hw_stats->rx_size_65_to_127_packets += rd64(hw, TXGBE_MACRX65TO127L);
1931         hw_stats->rx_size_128_to_255_packets += rd64(hw, TXGBE_MACRX128TO255L);
1932         hw_stats->rx_size_256_to_511_packets += rd64(hw, TXGBE_MACRX256TO511L);
1933         hw_stats->rx_size_512_to_1023_packets +=
1934                         rd64(hw, TXGBE_MACRX512TO1023L);
1935         hw_stats->rx_size_1024_to_max_packets +=
1936                         rd64(hw, TXGBE_MACRX1024TOMAXL);
1937         hw_stats->tx_size_64_packets += rd64(hw, TXGBE_MACTX1TO64L);
1938         hw_stats->tx_size_65_to_127_packets += rd64(hw, TXGBE_MACTX65TO127L);
1939         hw_stats->tx_size_128_to_255_packets += rd64(hw, TXGBE_MACTX128TO255L);
1940         hw_stats->tx_size_256_to_511_packets += rd64(hw, TXGBE_MACTX256TO511L);
1941         hw_stats->tx_size_512_to_1023_packets +=
1942                         rd64(hw, TXGBE_MACTX512TO1023L);
1943         hw_stats->tx_size_1024_to_max_packets +=
1944                         rd64(hw, TXGBE_MACTX1024TOMAXL);
1945
1946         hw_stats->rx_undersize_errors += rd64(hw, TXGBE_MACRXERRLENL);
1947         hw_stats->rx_oversize_errors += rd32(hw, TXGBE_MACRXOVERSIZE);
1948         hw_stats->rx_jabber_errors += rd32(hw, TXGBE_MACRXJABBER);
1949
1950         /* MNG Stats */
1951         hw_stats->mng_bmc2host_packets = rd32(hw, TXGBE_MNGBMC2OS);
1952         hw_stats->mng_host2bmc_packets = rd32(hw, TXGBE_MNGOS2BMC);
1953         hw_stats->rx_management_packets = rd32(hw, TXGBE_DMARXMNG);
1954         hw_stats->tx_management_packets = rd32(hw, TXGBE_DMATXMNG);
1955
1956         /* FCoE Stats */
1957         hw_stats->rx_fcoe_crc_errors += rd32(hw, TXGBE_FCOECRC);
1958         hw_stats->rx_fcoe_mbuf_allocation_errors += rd32(hw, TXGBE_FCOELAST);
1959         hw_stats->rx_fcoe_dropped += rd32(hw, TXGBE_FCOERPDC);
1960         hw_stats->rx_fcoe_packets += rd32(hw, TXGBE_FCOEPRC);
1961         hw_stats->tx_fcoe_packets += rd32(hw, TXGBE_FCOEPTC);
1962         hw_stats->rx_fcoe_bytes += rd32(hw, TXGBE_FCOEDWRC);
1963         hw_stats->tx_fcoe_bytes += rd32(hw, TXGBE_FCOEDWTC);
1964
1965         /* Flow Director Stats */
1966         hw_stats->flow_director_matched_filters += rd32(hw, TXGBE_FDIRMATCH);
1967         hw_stats->flow_director_missed_filters += rd32(hw, TXGBE_FDIRMISS);
1968         hw_stats->flow_director_added_filters +=
1969                 TXGBE_FDIRUSED_ADD(rd32(hw, TXGBE_FDIRUSED));
1970         hw_stats->flow_director_removed_filters +=
1971                 TXGBE_FDIRUSED_REM(rd32(hw, TXGBE_FDIRUSED));
1972         hw_stats->flow_director_filter_add_errors +=
1973                 TXGBE_FDIRFAIL_ADD(rd32(hw, TXGBE_FDIRFAIL));
1974         hw_stats->flow_director_filter_remove_errors +=
1975                 TXGBE_FDIRFAIL_REM(rd32(hw, TXGBE_FDIRFAIL));
1976
1977         /* MACsec Stats */
1978         hw_stats->tx_macsec_pkts_untagged += rd32(hw, TXGBE_LSECTX_UTPKT);
1979         hw_stats->tx_macsec_pkts_encrypted +=
1980                         rd32(hw, TXGBE_LSECTX_ENCPKT);
1981         hw_stats->tx_macsec_pkts_protected +=
1982                         rd32(hw, TXGBE_LSECTX_PROTPKT);
1983         hw_stats->tx_macsec_octets_encrypted +=
1984                         rd32(hw, TXGBE_LSECTX_ENCOCT);
1985         hw_stats->tx_macsec_octets_protected +=
1986                         rd32(hw, TXGBE_LSECTX_PROTOCT);
1987         hw_stats->rx_macsec_pkts_untagged += rd32(hw, TXGBE_LSECRX_UTPKT);
1988         hw_stats->rx_macsec_pkts_badtag += rd32(hw, TXGBE_LSECRX_BTPKT);
1989         hw_stats->rx_macsec_pkts_nosci += rd32(hw, TXGBE_LSECRX_NOSCIPKT);
1990         hw_stats->rx_macsec_pkts_unknownsci += rd32(hw, TXGBE_LSECRX_UNSCIPKT);
1991         hw_stats->rx_macsec_octets_decrypted += rd32(hw, TXGBE_LSECRX_DECOCT);
1992         hw_stats->rx_macsec_octets_validated += rd32(hw, TXGBE_LSECRX_VLDOCT);
1993         hw_stats->rx_macsec_sc_pkts_unchecked +=
1994                         rd32(hw, TXGBE_LSECRX_UNCHKPKT);
1995         hw_stats->rx_macsec_sc_pkts_delayed += rd32(hw, TXGBE_LSECRX_DLYPKT);
1996         hw_stats->rx_macsec_sc_pkts_late += rd32(hw, TXGBE_LSECRX_LATEPKT);
1997         for (i = 0; i < 2; i++) {
1998                 hw_stats->rx_macsec_sa_pkts_ok +=
1999                         rd32(hw, TXGBE_LSECRX_OKPKT(i));
2000                 hw_stats->rx_macsec_sa_pkts_invalid +=
2001                         rd32(hw, TXGBE_LSECRX_INVPKT(i));
2002                 hw_stats->rx_macsec_sa_pkts_notvalid +=
2003                         rd32(hw, TXGBE_LSECRX_BADPKT(i));
2004         }
2005         hw_stats->rx_macsec_sa_pkts_unusedsa +=
2006                         rd32(hw, TXGBE_LSECRX_INVSAPKT);
2007         hw_stats->rx_macsec_sa_pkts_notusingsa +=
2008                         rd32(hw, TXGBE_LSECRX_BADSAPKT);
2009
2010         hw_stats->rx_total_missed_packets = 0;
2011         for (i = 0; i < TXGBE_MAX_UP; i++) {
2012                 hw_stats->rx_total_missed_packets +=
2013                         hw_stats->up[i].rx_up_dropped;
2014         }
2015 }
2016
2017 static int
2018 txgbe_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
2019 {
2020         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2021         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
2022         struct txgbe_stat_mappings *stat_mappings =
2023                         TXGBE_DEV_STAT_MAPPINGS(dev);
2024         uint32_t i, j;
2025
2026         txgbe_read_stats_registers(hw, hw_stats);
2027
2028         if (stats == NULL)
2029                 return -EINVAL;
2030
2031         /* Fill out the rte_eth_stats statistics structure */
2032         stats->ipackets = hw_stats->rx_packets;
2033         stats->ibytes = hw_stats->rx_bytes;
2034         stats->opackets = hw_stats->tx_packets;
2035         stats->obytes = hw_stats->tx_bytes;
2036
2037         memset(&stats->q_ipackets, 0, sizeof(stats->q_ipackets));
2038         memset(&stats->q_opackets, 0, sizeof(stats->q_opackets));
2039         memset(&stats->q_ibytes, 0, sizeof(stats->q_ibytes));
2040         memset(&stats->q_obytes, 0, sizeof(stats->q_obytes));
2041         memset(&stats->q_errors, 0, sizeof(stats->q_errors));
2042         for (i = 0; i < TXGBE_MAX_QP; i++) {
2043                 uint32_t n = i / NB_QMAP_FIELDS_PER_QSM_REG;
2044                 uint32_t offset = (i % NB_QMAP_FIELDS_PER_QSM_REG) * 8;
2045                 uint32_t q_map;
2046
2047                 q_map = (stat_mappings->rqsm[n] >> offset)
2048                                 & QMAP_FIELD_RESERVED_BITS_MASK;
2049                 j = (q_map < RTE_ETHDEV_QUEUE_STAT_CNTRS
2050                      ? q_map : q_map % RTE_ETHDEV_QUEUE_STAT_CNTRS);
2051                 stats->q_ipackets[j] += hw_stats->qp[i].rx_qp_packets;
2052                 stats->q_ibytes[j] += hw_stats->qp[i].rx_qp_bytes;
2053
2054                 q_map = (stat_mappings->tqsm[n] >> offset)
2055                                 & QMAP_FIELD_RESERVED_BITS_MASK;
2056                 j = (q_map < RTE_ETHDEV_QUEUE_STAT_CNTRS
2057                      ? q_map : q_map % RTE_ETHDEV_QUEUE_STAT_CNTRS);
2058                 stats->q_opackets[j] += hw_stats->qp[i].tx_qp_packets;
2059                 stats->q_obytes[j] += hw_stats->qp[i].tx_qp_bytes;
2060         }
2061
2062         /* Rx Errors */
2063         stats->imissed  = hw_stats->rx_total_missed_packets;
2064         stats->ierrors  = hw_stats->rx_crc_errors +
2065                           hw_stats->rx_mac_short_packet_dropped +
2066                           hw_stats->rx_length_errors +
2067                           hw_stats->rx_undersize_errors +
2068                           hw_stats->rx_oversize_errors +
2069                           hw_stats->rx_drop_packets +
2070                           hw_stats->rx_illegal_byte_errors +
2071                           hw_stats->rx_error_bytes +
2072                           hw_stats->rx_fragment_errors +
2073                           hw_stats->rx_fcoe_crc_errors +
2074                           hw_stats->rx_fcoe_mbuf_allocation_errors;
2075
2076         /* Tx Errors */
2077         stats->oerrors  = 0;
2078         return 0;
2079 }
2080
2081 static int
2082 txgbe_dev_stats_reset(struct rte_eth_dev *dev)
2083 {
2084         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2085         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
2086
2087         /* HW registers are cleared on read */
2088         hw->offset_loaded = 0;
2089         txgbe_dev_stats_get(dev, NULL);
2090         hw->offset_loaded = 1;
2091
2092         /* Reset software totals */
2093         memset(hw_stats, 0, sizeof(*hw_stats));
2094
2095         return 0;
2096 }
2097
2098 /* This function calculates the number of xstats based on the current config */
2099 static unsigned
2100 txgbe_xstats_calc_num(struct rte_eth_dev *dev)
2101 {
2102         int nb_queues = max(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
2103         return TXGBE_NB_HW_STATS +
2104                TXGBE_NB_UP_STATS * TXGBE_MAX_UP +
2105                TXGBE_NB_QP_STATS * nb_queues;
2106 }
2107
2108 static inline int
2109 txgbe_get_name_by_id(uint32_t id, char *name, uint32_t size)
2110 {
2111         int nb, st;
2112
2113         /* Extended stats from txgbe_hw_stats */
2114         if (id < TXGBE_NB_HW_STATS) {
2115                 snprintf(name, size, "[hw]%s",
2116                         rte_txgbe_stats_strings[id].name);
2117                 return 0;
2118         }
2119         id -= TXGBE_NB_HW_STATS;
2120
2121         /* Priority Stats */
2122         if (id < TXGBE_NB_UP_STATS * TXGBE_MAX_UP) {
2123                 nb = id / TXGBE_NB_UP_STATS;
2124                 st = id % TXGBE_NB_UP_STATS;
2125                 snprintf(name, size, "[p%u]%s", nb,
2126                         rte_txgbe_up_strings[st].name);
2127                 return 0;
2128         }
2129         id -= TXGBE_NB_UP_STATS * TXGBE_MAX_UP;
2130
2131         /* Queue Stats */
2132         if (id < TXGBE_NB_QP_STATS * TXGBE_MAX_QP) {
2133                 nb = id / TXGBE_NB_QP_STATS;
2134                 st = id % TXGBE_NB_QP_STATS;
2135                 snprintf(name, size, "[q%u]%s", nb,
2136                         rte_txgbe_qp_strings[st].name);
2137                 return 0;
2138         }
2139         id -= TXGBE_NB_QP_STATS * TXGBE_MAX_QP;
2140
2141         return -(int)(id + 1);
2142 }
2143
2144 static inline int
2145 txgbe_get_offset_by_id(uint32_t id, uint32_t *offset)
2146 {
2147         int nb, st;
2148
2149         /* Extended stats from txgbe_hw_stats */
2150         if (id < TXGBE_NB_HW_STATS) {
2151                 *offset = rte_txgbe_stats_strings[id].offset;
2152                 return 0;
2153         }
2154         id -= TXGBE_NB_HW_STATS;
2155
2156         /* Priority Stats */
2157         if (id < TXGBE_NB_UP_STATS * TXGBE_MAX_UP) {
2158                 nb = id / TXGBE_NB_UP_STATS;
2159                 st = id % TXGBE_NB_UP_STATS;
2160                 *offset = rte_txgbe_up_strings[st].offset +
2161                         nb * (TXGBE_NB_UP_STATS * sizeof(uint64_t));
2162                 return 0;
2163         }
2164         id -= TXGBE_NB_UP_STATS * TXGBE_MAX_UP;
2165
2166         /* Queue Stats */
2167         if (id < TXGBE_NB_QP_STATS * TXGBE_MAX_QP) {
2168                 nb = id / TXGBE_NB_QP_STATS;
2169                 st = id % TXGBE_NB_QP_STATS;
2170                 *offset = rte_txgbe_qp_strings[st].offset +
2171                         nb * (TXGBE_NB_QP_STATS * sizeof(uint64_t));
2172                 return 0;
2173         }
2174
2175         return -1;
2176 }
2177
2178 static int txgbe_dev_xstats_get_names(struct rte_eth_dev *dev,
2179         struct rte_eth_xstat_name *xstats_names, unsigned int limit)
2180 {
2181         unsigned int i, count;
2182
2183         count = txgbe_xstats_calc_num(dev);
2184         if (xstats_names == NULL)
2185                 return count;
2186
2187         /* Note: limit >= cnt_stats checked upstream
2188          * in rte_eth_xstats_names()
2189          */
2190         limit = min(limit, count);
2191
2192         /* Extended stats from txgbe_hw_stats */
2193         for (i = 0; i < limit; i++) {
2194                 if (txgbe_get_name_by_id(i, xstats_names[i].name,
2195                         sizeof(xstats_names[i].name))) {
2196                         PMD_INIT_LOG(WARNING, "id value %d isn't valid", i);
2197                         break;
2198                 }
2199         }
2200
2201         return i;
2202 }
2203
2204 static int txgbe_dev_xstats_get_names_by_id(struct rte_eth_dev *dev,
2205         struct rte_eth_xstat_name *xstats_names,
2206         const uint64_t *ids,
2207         unsigned int limit)
2208 {
2209         unsigned int i;
2210
2211         if (ids == NULL)
2212                 return txgbe_dev_xstats_get_names(dev, xstats_names, limit);
2213
2214         for (i = 0; i < limit; i++) {
2215                 if (txgbe_get_name_by_id(ids[i], xstats_names[i].name,
2216                                 sizeof(xstats_names[i].name))) {
2217                         PMD_INIT_LOG(WARNING, "id value %d isn't valid", i);
2218                         return -1;
2219                 }
2220         }
2221
2222         return i;
2223 }
2224
2225 static int
2226 txgbe_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
2227                                          unsigned int limit)
2228 {
2229         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2230         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
2231         unsigned int i, count;
2232
2233         txgbe_read_stats_registers(hw, hw_stats);
2234
2235         /* If this is a reset xstats is NULL, and we have cleared the
2236          * registers by reading them.
2237          */
2238         count = txgbe_xstats_calc_num(dev);
2239         if (xstats == NULL)
2240                 return count;
2241
2242         limit = min(limit, txgbe_xstats_calc_num(dev));
2243
2244         /* Extended stats from txgbe_hw_stats */
2245         for (i = 0; i < limit; i++) {
2246                 uint32_t offset = 0;
2247
2248                 if (txgbe_get_offset_by_id(i, &offset)) {
2249                         PMD_INIT_LOG(WARNING, "id value %d isn't valid", i);
2250                         break;
2251                 }
2252                 xstats[i].value = *(uint64_t *)(((char *)hw_stats) + offset);
2253                 xstats[i].id = i;
2254         }
2255
2256         return i;
2257 }
2258
2259 static int
2260 txgbe_dev_xstats_get_(struct rte_eth_dev *dev, uint64_t *values,
2261                                          unsigned int limit)
2262 {
2263         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2264         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
2265         unsigned int i, count;
2266
2267         txgbe_read_stats_registers(hw, hw_stats);
2268
2269         /* If this is a reset xstats is NULL, and we have cleared the
2270          * registers by reading them.
2271          */
2272         count = txgbe_xstats_calc_num(dev);
2273         if (values == NULL)
2274                 return count;
2275
2276         limit = min(limit, txgbe_xstats_calc_num(dev));
2277
2278         /* Extended stats from txgbe_hw_stats */
2279         for (i = 0; i < limit; i++) {
2280                 uint32_t offset;
2281
2282                 if (txgbe_get_offset_by_id(i, &offset)) {
2283                         PMD_INIT_LOG(WARNING, "id value %d isn't valid", i);
2284                         break;
2285                 }
2286                 values[i] = *(uint64_t *)(((char *)hw_stats) + offset);
2287         }
2288
2289         return i;
2290 }
2291
2292 static int
2293 txgbe_dev_xstats_get_by_id(struct rte_eth_dev *dev, const uint64_t *ids,
2294                 uint64_t *values, unsigned int limit)
2295 {
2296         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
2297         unsigned int i;
2298
2299         if (ids == NULL)
2300                 return txgbe_dev_xstats_get_(dev, values, limit);
2301
2302         for (i = 0; i < limit; i++) {
2303                 uint32_t offset;
2304
2305                 if (txgbe_get_offset_by_id(ids[i], &offset)) {
2306                         PMD_INIT_LOG(WARNING, "id value %d isn't valid", i);
2307                         break;
2308                 }
2309                 values[i] = *(uint64_t *)(((char *)hw_stats) + offset);
2310         }
2311
2312         return i;
2313 }
2314
2315 static int
2316 txgbe_dev_xstats_reset(struct rte_eth_dev *dev)
2317 {
2318         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2319         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
2320
2321         /* HW registers are cleared on read */
2322         hw->offset_loaded = 0;
2323         txgbe_read_stats_registers(hw, hw_stats);
2324         hw->offset_loaded = 1;
2325
2326         /* Reset software totals */
2327         memset(hw_stats, 0, sizeof(*hw_stats));
2328
2329         return 0;
2330 }
2331
2332 static int
2333 txgbe_fw_version_get(struct rte_eth_dev *dev, char *fw_version, size_t fw_size)
2334 {
2335         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2336         u16 eeprom_verh, eeprom_verl;
2337         u32 etrack_id;
2338         int ret;
2339
2340         hw->rom.readw_sw(hw, TXGBE_EEPROM_VERSION_H, &eeprom_verh);
2341         hw->rom.readw_sw(hw, TXGBE_EEPROM_VERSION_L, &eeprom_verl);
2342
2343         etrack_id = (eeprom_verh << 16) | eeprom_verl;
2344         ret = snprintf(fw_version, fw_size, "0x%08x", etrack_id);
2345
2346         ret += 1; /* add the size of '\0' */
2347         if (fw_size < (u32)ret)
2348                 return ret;
2349         else
2350                 return 0;
2351 }
2352
2353 static int
2354 txgbe_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2355 {
2356         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2357         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2358
2359         dev_info->max_rx_queues = (uint16_t)hw->mac.max_rx_queues;
2360         dev_info->max_tx_queues = (uint16_t)hw->mac.max_tx_queues;
2361         dev_info->min_rx_bufsize = 1024;
2362         dev_info->max_rx_pktlen = 15872;
2363         dev_info->max_mac_addrs = hw->mac.num_rar_entries;
2364         dev_info->max_hash_mac_addrs = TXGBE_VMDQ_NUM_UC_MAC;
2365         dev_info->max_vfs = pci_dev->max_vfs;
2366         dev_info->max_vmdq_pools = ETH_64_POOLS;
2367         dev_info->vmdq_queue_num = dev_info->max_rx_queues;
2368         dev_info->rx_queue_offload_capa = txgbe_get_rx_queue_offloads(dev);
2369         dev_info->rx_offload_capa = (txgbe_get_rx_port_offloads(dev) |
2370                                      dev_info->rx_queue_offload_capa);
2371         dev_info->tx_queue_offload_capa = txgbe_get_tx_queue_offloads(dev);
2372         dev_info->tx_offload_capa = txgbe_get_tx_port_offloads(dev);
2373
2374         dev_info->default_rxconf = (struct rte_eth_rxconf) {
2375                 .rx_thresh = {
2376                         .pthresh = TXGBE_DEFAULT_RX_PTHRESH,
2377                         .hthresh = TXGBE_DEFAULT_RX_HTHRESH,
2378                         .wthresh = TXGBE_DEFAULT_RX_WTHRESH,
2379                 },
2380                 .rx_free_thresh = TXGBE_DEFAULT_RX_FREE_THRESH,
2381                 .rx_drop_en = 0,
2382                 .offloads = 0,
2383         };
2384
2385         dev_info->default_txconf = (struct rte_eth_txconf) {
2386                 .tx_thresh = {
2387                         .pthresh = TXGBE_DEFAULT_TX_PTHRESH,
2388                         .hthresh = TXGBE_DEFAULT_TX_HTHRESH,
2389                         .wthresh = TXGBE_DEFAULT_TX_WTHRESH,
2390                 },
2391                 .tx_free_thresh = TXGBE_DEFAULT_TX_FREE_THRESH,
2392                 .offloads = 0,
2393         };
2394
2395         dev_info->rx_desc_lim = rx_desc_lim;
2396         dev_info->tx_desc_lim = tx_desc_lim;
2397
2398         dev_info->hash_key_size = TXGBE_HKEY_MAX_INDEX * sizeof(uint32_t);
2399         dev_info->reta_size = ETH_RSS_RETA_SIZE_128;
2400         dev_info->flow_type_rss_offloads = TXGBE_RSS_OFFLOAD_ALL;
2401
2402         dev_info->speed_capa = ETH_LINK_SPEED_1G | ETH_LINK_SPEED_10G;
2403         dev_info->speed_capa |= ETH_LINK_SPEED_100M;
2404
2405         /* Driver-preferred Rx/Tx parameters */
2406         dev_info->default_rxportconf.burst_size = 32;
2407         dev_info->default_txportconf.burst_size = 32;
2408         dev_info->default_rxportconf.nb_queues = 1;
2409         dev_info->default_txportconf.nb_queues = 1;
2410         dev_info->default_rxportconf.ring_size = 256;
2411         dev_info->default_txportconf.ring_size = 256;
2412
2413         return 0;
2414 }
2415
2416 const uint32_t *
2417 txgbe_dev_supported_ptypes_get(struct rte_eth_dev *dev)
2418 {
2419         if (dev->rx_pkt_burst == txgbe_recv_pkts ||
2420             dev->rx_pkt_burst == txgbe_recv_pkts_lro_single_alloc ||
2421             dev->rx_pkt_burst == txgbe_recv_pkts_lro_bulk_alloc ||
2422             dev->rx_pkt_burst == txgbe_recv_pkts_bulk_alloc)
2423                 return txgbe_get_supported_ptypes();
2424
2425         return NULL;
2426 }
2427
2428 void
2429 txgbe_dev_setup_link_alarm_handler(void *param)
2430 {
2431         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2432         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2433         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2434         u32 speed;
2435         bool autoneg = false;
2436
2437         speed = hw->phy.autoneg_advertised;
2438         if (!speed)
2439                 hw->mac.get_link_capabilities(hw, &speed, &autoneg);
2440
2441         hw->mac.setup_link(hw, speed, true);
2442
2443         intr->flags &= ~TXGBE_FLAG_NEED_LINK_CONFIG;
2444 }
2445
2446 /* return 0 means link status changed, -1 means not changed */
2447 int
2448 txgbe_dev_link_update_share(struct rte_eth_dev *dev,
2449                             int wait_to_complete)
2450 {
2451         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2452         struct rte_eth_link link;
2453         u32 link_speed = TXGBE_LINK_SPEED_UNKNOWN;
2454         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2455         bool link_up;
2456         int err;
2457         int wait = 1;
2458
2459         memset(&link, 0, sizeof(link));
2460         link.link_status = ETH_LINK_DOWN;
2461         link.link_speed = ETH_SPEED_NUM_NONE;
2462         link.link_duplex = ETH_LINK_HALF_DUPLEX;
2463         link.link_autoneg = ETH_LINK_AUTONEG;
2464
2465         hw->mac.get_link_status = true;
2466
2467         if (intr->flags & TXGBE_FLAG_NEED_LINK_CONFIG)
2468                 return rte_eth_linkstatus_set(dev, &link);
2469
2470         /* check if it needs to wait to complete, if lsc interrupt is enabled */
2471         if (wait_to_complete == 0 || dev->data->dev_conf.intr_conf.lsc != 0)
2472                 wait = 0;
2473
2474         err = hw->mac.check_link(hw, &link_speed, &link_up, wait);
2475
2476         if (err != 0) {
2477                 link.link_speed = ETH_SPEED_NUM_100M;
2478                 link.link_duplex = ETH_LINK_FULL_DUPLEX;
2479                 return rte_eth_linkstatus_set(dev, &link);
2480         }
2481
2482         if (link_up == 0) {
2483                 if (hw->phy.media_type == txgbe_media_type_fiber) {
2484                         intr->flags |= TXGBE_FLAG_NEED_LINK_CONFIG;
2485                         rte_eal_alarm_set(10,
2486                                 txgbe_dev_setup_link_alarm_handler, dev);
2487                 }
2488                 return rte_eth_linkstatus_set(dev, &link);
2489         }
2490
2491         intr->flags &= ~TXGBE_FLAG_NEED_LINK_CONFIG;
2492         link.link_status = ETH_LINK_UP;
2493         link.link_duplex = ETH_LINK_FULL_DUPLEX;
2494
2495         switch (link_speed) {
2496         default:
2497         case TXGBE_LINK_SPEED_UNKNOWN:
2498                 link.link_duplex = ETH_LINK_FULL_DUPLEX;
2499                 link.link_speed = ETH_SPEED_NUM_100M;
2500                 break;
2501
2502         case TXGBE_LINK_SPEED_100M_FULL:
2503                 link.link_speed = ETH_SPEED_NUM_100M;
2504                 break;
2505
2506         case TXGBE_LINK_SPEED_1GB_FULL:
2507                 link.link_speed = ETH_SPEED_NUM_1G;
2508                 break;
2509
2510         case TXGBE_LINK_SPEED_2_5GB_FULL:
2511                 link.link_speed = ETH_SPEED_NUM_2_5G;
2512                 break;
2513
2514         case TXGBE_LINK_SPEED_5GB_FULL:
2515                 link.link_speed = ETH_SPEED_NUM_5G;
2516                 break;
2517
2518         case TXGBE_LINK_SPEED_10GB_FULL:
2519                 link.link_speed = ETH_SPEED_NUM_10G;
2520                 break;
2521         }
2522
2523         return rte_eth_linkstatus_set(dev, &link);
2524 }
2525
2526 static int
2527 txgbe_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
2528 {
2529         return txgbe_dev_link_update_share(dev, wait_to_complete);
2530 }
2531
2532 static int
2533 txgbe_dev_promiscuous_enable(struct rte_eth_dev *dev)
2534 {
2535         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2536         uint32_t fctrl;
2537
2538         fctrl = rd32(hw, TXGBE_PSRCTL);
2539         fctrl |= (TXGBE_PSRCTL_UCP | TXGBE_PSRCTL_MCP);
2540         wr32(hw, TXGBE_PSRCTL, fctrl);
2541
2542         return 0;
2543 }
2544
2545 static int
2546 txgbe_dev_promiscuous_disable(struct rte_eth_dev *dev)
2547 {
2548         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2549         uint32_t fctrl;
2550
2551         fctrl = rd32(hw, TXGBE_PSRCTL);
2552         fctrl &= (~TXGBE_PSRCTL_UCP);
2553         if (dev->data->all_multicast == 1)
2554                 fctrl |= TXGBE_PSRCTL_MCP;
2555         else
2556                 fctrl &= (~TXGBE_PSRCTL_MCP);
2557         wr32(hw, TXGBE_PSRCTL, fctrl);
2558
2559         return 0;
2560 }
2561
2562 static int
2563 txgbe_dev_allmulticast_enable(struct rte_eth_dev *dev)
2564 {
2565         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2566         uint32_t fctrl;
2567
2568         fctrl = rd32(hw, TXGBE_PSRCTL);
2569         fctrl |= TXGBE_PSRCTL_MCP;
2570         wr32(hw, TXGBE_PSRCTL, fctrl);
2571
2572         return 0;
2573 }
2574
2575 static int
2576 txgbe_dev_allmulticast_disable(struct rte_eth_dev *dev)
2577 {
2578         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2579         uint32_t fctrl;
2580
2581         if (dev->data->promiscuous == 1)
2582                 return 0; /* must remain in all_multicast mode */
2583
2584         fctrl = rd32(hw, TXGBE_PSRCTL);
2585         fctrl &= (~TXGBE_PSRCTL_MCP);
2586         wr32(hw, TXGBE_PSRCTL, fctrl);
2587
2588         return 0;
2589 }
2590
2591 /**
2592  * It clears the interrupt causes and enables the interrupt.
2593  * It will be called once only during nic initialized.
2594  *
2595  * @param dev
2596  *  Pointer to struct rte_eth_dev.
2597  * @param on
2598  *  Enable or Disable.
2599  *
2600  * @return
2601  *  - On success, zero.
2602  *  - On failure, a negative value.
2603  */
2604 static int
2605 txgbe_dev_lsc_interrupt_setup(struct rte_eth_dev *dev, uint8_t on)
2606 {
2607         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2608
2609         txgbe_dev_link_status_print(dev);
2610         if (on)
2611                 intr->mask_misc |= TXGBE_ICRMISC_LSC;
2612         else
2613                 intr->mask_misc &= ~TXGBE_ICRMISC_LSC;
2614
2615         return 0;
2616 }
2617
2618 /**
2619  * It clears the interrupt causes and enables the interrupt.
2620  * It will be called once only during nic initialized.
2621  *
2622  * @param dev
2623  *  Pointer to struct rte_eth_dev.
2624  *
2625  * @return
2626  *  - On success, zero.
2627  *  - On failure, a negative value.
2628  */
2629 static int
2630 txgbe_dev_rxq_interrupt_setup(struct rte_eth_dev *dev)
2631 {
2632         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2633
2634         intr->mask[0] |= TXGBE_ICR_MASK;
2635         intr->mask[1] |= TXGBE_ICR_MASK;
2636
2637         return 0;
2638 }
2639
2640 /**
2641  * It clears the interrupt causes and enables the interrupt.
2642  * It will be called once only during nic initialized.
2643  *
2644  * @param dev
2645  *  Pointer to struct rte_eth_dev.
2646  *
2647  * @return
2648  *  - On success, zero.
2649  *  - On failure, a negative value.
2650  */
2651 static int
2652 txgbe_dev_macsec_interrupt_setup(struct rte_eth_dev *dev)
2653 {
2654         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2655
2656         intr->mask_misc |= TXGBE_ICRMISC_LNKSEC;
2657
2658         return 0;
2659 }
2660
2661 /*
2662  * It reads ICR and sets flag (TXGBE_ICRMISC_LSC) for the link_update.
2663  *
2664  * @param dev
2665  *  Pointer to struct rte_eth_dev.
2666  *
2667  * @return
2668  *  - On success, zero.
2669  *  - On failure, a negative value.
2670  */
2671 static int
2672 txgbe_dev_interrupt_get_status(struct rte_eth_dev *dev)
2673 {
2674         uint32_t eicr;
2675         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2676         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2677
2678         /* clear all cause mask */
2679         txgbe_disable_intr(hw);
2680
2681         /* read-on-clear nic registers here */
2682         eicr = ((u32 *)hw->isb_mem)[TXGBE_ISB_MISC];
2683         PMD_DRV_LOG(DEBUG, "eicr %x", eicr);
2684
2685         intr->flags = 0;
2686
2687         /* set flag for async link update */
2688         if (eicr & TXGBE_ICRMISC_LSC)
2689                 intr->flags |= TXGBE_FLAG_NEED_LINK_UPDATE;
2690
2691         if (eicr & TXGBE_ICRMISC_VFMBX)
2692                 intr->flags |= TXGBE_FLAG_MAILBOX;
2693
2694         if (eicr & TXGBE_ICRMISC_LNKSEC)
2695                 intr->flags |= TXGBE_FLAG_MACSEC;
2696
2697         if (eicr & TXGBE_ICRMISC_GPIO)
2698                 intr->flags |= TXGBE_FLAG_PHY_INTERRUPT;
2699
2700         return 0;
2701 }
2702
2703 /**
2704  * It gets and then prints the link status.
2705  *
2706  * @param dev
2707  *  Pointer to struct rte_eth_dev.
2708  *
2709  * @return
2710  *  - On success, zero.
2711  *  - On failure, a negative value.
2712  */
2713 static void
2714 txgbe_dev_link_status_print(struct rte_eth_dev *dev)
2715 {
2716         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2717         struct rte_eth_link link;
2718
2719         rte_eth_linkstatus_get(dev, &link);
2720
2721         if (link.link_status) {
2722                 PMD_INIT_LOG(INFO, "Port %d: Link Up - speed %u Mbps - %s",
2723                                         (int)(dev->data->port_id),
2724                                         (unsigned int)link.link_speed,
2725                         link.link_duplex == ETH_LINK_FULL_DUPLEX ?
2726                                         "full-duplex" : "half-duplex");
2727         } else {
2728                 PMD_INIT_LOG(INFO, " Port %d: Link Down",
2729                                 (int)(dev->data->port_id));
2730         }
2731         PMD_INIT_LOG(DEBUG, "PCI Address: " PCI_PRI_FMT,
2732                                 pci_dev->addr.domain,
2733                                 pci_dev->addr.bus,
2734                                 pci_dev->addr.devid,
2735                                 pci_dev->addr.function);
2736 }
2737
2738 /*
2739  * It executes link_update after knowing an interrupt occurred.
2740  *
2741  * @param dev
2742  *  Pointer to struct rte_eth_dev.
2743  *
2744  * @return
2745  *  - On success, zero.
2746  *  - On failure, a negative value.
2747  */
2748 static int
2749 txgbe_dev_interrupt_action(struct rte_eth_dev *dev,
2750                            struct rte_intr_handle *intr_handle)
2751 {
2752         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2753         int64_t timeout;
2754         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2755
2756         PMD_DRV_LOG(DEBUG, "intr action type %d", intr->flags);
2757
2758         if (intr->flags & TXGBE_FLAG_MAILBOX) {
2759                 txgbe_pf_mbx_process(dev);
2760                 intr->flags &= ~TXGBE_FLAG_MAILBOX;
2761         }
2762
2763         if (intr->flags & TXGBE_FLAG_PHY_INTERRUPT) {
2764                 hw->phy.handle_lasi(hw);
2765                 intr->flags &= ~TXGBE_FLAG_PHY_INTERRUPT;
2766         }
2767
2768         if (intr->flags & TXGBE_FLAG_NEED_LINK_UPDATE) {
2769                 struct rte_eth_link link;
2770
2771                 /*get the link status before link update, for predicting later*/
2772                 rte_eth_linkstatus_get(dev, &link);
2773
2774                 txgbe_dev_link_update(dev, 0);
2775
2776                 /* likely to up */
2777                 if (!link.link_status)
2778                         /* handle it 1 sec later, wait it being stable */
2779                         timeout = TXGBE_LINK_UP_CHECK_TIMEOUT;
2780                 /* likely to down */
2781                 else
2782                         /* handle it 4 sec later, wait it being stable */
2783                         timeout = TXGBE_LINK_DOWN_CHECK_TIMEOUT;
2784
2785                 txgbe_dev_link_status_print(dev);
2786                 if (rte_eal_alarm_set(timeout * 1000,
2787                                       txgbe_dev_interrupt_delayed_handler,
2788                                       (void *)dev) < 0) {
2789                         PMD_DRV_LOG(ERR, "Error setting alarm");
2790                 } else {
2791                         /* remember original mask */
2792                         intr->mask_misc_orig = intr->mask_misc;
2793                         /* only disable lsc interrupt */
2794                         intr->mask_misc &= ~TXGBE_ICRMISC_LSC;
2795                 }
2796         }
2797
2798         PMD_DRV_LOG(DEBUG, "enable intr immediately");
2799         txgbe_enable_intr(dev);
2800         rte_intr_enable(intr_handle);
2801
2802         return 0;
2803 }
2804
2805 /**
2806  * Interrupt handler which shall be registered for alarm callback for delayed
2807  * handling specific interrupt to wait for the stable nic state. As the
2808  * NIC interrupt state is not stable for txgbe after link is just down,
2809  * it needs to wait 4 seconds to get the stable status.
2810  *
2811  * @param handle
2812  *  Pointer to interrupt handle.
2813  * @param param
2814  *  The address of parameter (struct rte_eth_dev *) registered before.
2815  *
2816  * @return
2817  *  void
2818  */
2819 static void
2820 txgbe_dev_interrupt_delayed_handler(void *param)
2821 {
2822         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2823         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2824         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2825         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2826         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2827         uint32_t eicr;
2828
2829         txgbe_disable_intr(hw);
2830
2831         eicr = ((u32 *)hw->isb_mem)[TXGBE_ISB_MISC];
2832         if (eicr & TXGBE_ICRMISC_VFMBX)
2833                 txgbe_pf_mbx_process(dev);
2834
2835         if (intr->flags & TXGBE_FLAG_PHY_INTERRUPT) {
2836                 hw->phy.handle_lasi(hw);
2837                 intr->flags &= ~TXGBE_FLAG_PHY_INTERRUPT;
2838         }
2839
2840         if (intr->flags & TXGBE_FLAG_NEED_LINK_UPDATE) {
2841                 txgbe_dev_link_update(dev, 0);
2842                 intr->flags &= ~TXGBE_FLAG_NEED_LINK_UPDATE;
2843                 txgbe_dev_link_status_print(dev);
2844                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC,
2845                                               NULL);
2846         }
2847
2848         if (intr->flags & TXGBE_FLAG_MACSEC) {
2849                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_MACSEC,
2850                                               NULL);
2851                 intr->flags &= ~TXGBE_FLAG_MACSEC;
2852         }
2853
2854         /* restore original mask */
2855         intr->mask_misc = intr->mask_misc_orig;
2856         intr->mask_misc_orig = 0;
2857
2858         PMD_DRV_LOG(DEBUG, "enable intr in delayed handler S[%08x]", eicr);
2859         txgbe_enable_intr(dev);
2860         rte_intr_enable(intr_handle);
2861 }
2862
2863 /**
2864  * Interrupt handler triggered by NIC  for handling
2865  * specific interrupt.
2866  *
2867  * @param handle
2868  *  Pointer to interrupt handle.
2869  * @param param
2870  *  The address of parameter (struct rte_eth_dev *) registered before.
2871  *
2872  * @return
2873  *  void
2874  */
2875 static void
2876 txgbe_dev_interrupt_handler(void *param)
2877 {
2878         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2879
2880         txgbe_dev_interrupt_get_status(dev);
2881         txgbe_dev_interrupt_action(dev, dev->intr_handle);
2882 }
2883
2884 static int
2885 txgbe_dev_led_on(struct rte_eth_dev *dev)
2886 {
2887         struct txgbe_hw *hw;
2888
2889         hw = TXGBE_DEV_HW(dev);
2890         return txgbe_led_on(hw, 4) == 0 ? 0 : -ENOTSUP;
2891 }
2892
2893 static int
2894 txgbe_dev_led_off(struct rte_eth_dev *dev)
2895 {
2896         struct txgbe_hw *hw;
2897
2898         hw = TXGBE_DEV_HW(dev);
2899         return txgbe_led_off(hw, 4) == 0 ? 0 : -ENOTSUP;
2900 }
2901
2902 static int
2903 txgbe_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2904 {
2905         struct txgbe_hw *hw;
2906         uint32_t mflcn_reg;
2907         uint32_t fccfg_reg;
2908         int rx_pause;
2909         int tx_pause;
2910
2911         hw = TXGBE_DEV_HW(dev);
2912
2913         fc_conf->pause_time = hw->fc.pause_time;
2914         fc_conf->high_water = hw->fc.high_water[0];
2915         fc_conf->low_water = hw->fc.low_water[0];
2916         fc_conf->send_xon = hw->fc.send_xon;
2917         fc_conf->autoneg = !hw->fc.disable_fc_autoneg;
2918
2919         /*
2920          * Return rx_pause status according to actual setting of
2921          * RXFCCFG register.
2922          */
2923         mflcn_reg = rd32(hw, TXGBE_RXFCCFG);
2924         if (mflcn_reg & (TXGBE_RXFCCFG_FC | TXGBE_RXFCCFG_PFC))
2925                 rx_pause = 1;
2926         else
2927                 rx_pause = 0;
2928
2929         /*
2930          * Return tx_pause status according to actual setting of
2931          * TXFCCFG register.
2932          */
2933         fccfg_reg = rd32(hw, TXGBE_TXFCCFG);
2934         if (fccfg_reg & (TXGBE_TXFCCFG_FC | TXGBE_TXFCCFG_PFC))
2935                 tx_pause = 1;
2936         else
2937                 tx_pause = 0;
2938
2939         if (rx_pause && tx_pause)
2940                 fc_conf->mode = RTE_FC_FULL;
2941         else if (rx_pause)
2942                 fc_conf->mode = RTE_FC_RX_PAUSE;
2943         else if (tx_pause)
2944                 fc_conf->mode = RTE_FC_TX_PAUSE;
2945         else
2946                 fc_conf->mode = RTE_FC_NONE;
2947
2948         return 0;
2949 }
2950
2951 static int
2952 txgbe_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
2953 {
2954         struct txgbe_hw *hw;
2955         int err;
2956         uint32_t rx_buf_size;
2957         uint32_t max_high_water;
2958         enum txgbe_fc_mode rte_fcmode_2_txgbe_fcmode[] = {
2959                 txgbe_fc_none,
2960                 txgbe_fc_rx_pause,
2961                 txgbe_fc_tx_pause,
2962                 txgbe_fc_full
2963         };
2964
2965         PMD_INIT_FUNC_TRACE();
2966
2967         hw = TXGBE_DEV_HW(dev);
2968         rx_buf_size = rd32(hw, TXGBE_PBRXSIZE(0));
2969         PMD_INIT_LOG(DEBUG, "Rx packet buffer size = 0x%x", rx_buf_size);
2970
2971         /*
2972          * At least reserve one Ethernet frame for watermark
2973          * high_water/low_water in kilo bytes for txgbe
2974          */
2975         max_high_water = (rx_buf_size - RTE_ETHER_MAX_LEN) >> 10;
2976         if (fc_conf->high_water > max_high_water ||
2977             fc_conf->high_water < fc_conf->low_water) {
2978                 PMD_INIT_LOG(ERR, "Invalid high/low water setup value in KB");
2979                 PMD_INIT_LOG(ERR, "High_water must <= 0x%x", max_high_water);
2980                 return -EINVAL;
2981         }
2982
2983         hw->fc.requested_mode = rte_fcmode_2_txgbe_fcmode[fc_conf->mode];
2984         hw->fc.pause_time     = fc_conf->pause_time;
2985         hw->fc.high_water[0]  = fc_conf->high_water;
2986         hw->fc.low_water[0]   = fc_conf->low_water;
2987         hw->fc.send_xon       = fc_conf->send_xon;
2988         hw->fc.disable_fc_autoneg = !fc_conf->autoneg;
2989
2990         err = txgbe_fc_enable(hw);
2991
2992         /* Not negotiated is not an error case */
2993         if (err == 0 || err == TXGBE_ERR_FC_NOT_NEGOTIATED) {
2994                 wr32m(hw, TXGBE_MACRXFLT, TXGBE_MACRXFLT_CTL_MASK,
2995                       (fc_conf->mac_ctrl_frame_fwd
2996                        ? TXGBE_MACRXFLT_CTL_NOPS : TXGBE_MACRXFLT_CTL_DROP));
2997                 txgbe_flush(hw);
2998
2999                 return 0;
3000         }
3001
3002         PMD_INIT_LOG(ERR, "txgbe_fc_enable = 0x%x", err);
3003         return -EIO;
3004 }
3005
3006 static int
3007 txgbe_priority_flow_ctrl_set(struct rte_eth_dev *dev,
3008                 struct rte_eth_pfc_conf *pfc_conf)
3009 {
3010         int err;
3011         uint32_t rx_buf_size;
3012         uint32_t max_high_water;
3013         uint8_t tc_num;
3014         uint8_t  map[TXGBE_DCB_UP_MAX] = { 0 };
3015         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3016         struct txgbe_dcb_config *dcb_config = TXGBE_DEV_DCB_CONFIG(dev);
3017
3018         enum txgbe_fc_mode rte_fcmode_2_txgbe_fcmode[] = {
3019                 txgbe_fc_none,
3020                 txgbe_fc_rx_pause,
3021                 txgbe_fc_tx_pause,
3022                 txgbe_fc_full
3023         };
3024
3025         PMD_INIT_FUNC_TRACE();
3026
3027         txgbe_dcb_unpack_map_cee(dcb_config, TXGBE_DCB_RX_CONFIG, map);
3028         tc_num = map[pfc_conf->priority];
3029         rx_buf_size = rd32(hw, TXGBE_PBRXSIZE(tc_num));
3030         PMD_INIT_LOG(DEBUG, "Rx packet buffer size = 0x%x", rx_buf_size);
3031         /*
3032          * At least reserve one Ethernet frame for watermark
3033          * high_water/low_water in kilo bytes for txgbe
3034          */
3035         max_high_water = (rx_buf_size - RTE_ETHER_MAX_LEN) >> 10;
3036         if (pfc_conf->fc.high_water > max_high_water ||
3037             pfc_conf->fc.high_water <= pfc_conf->fc.low_water) {
3038                 PMD_INIT_LOG(ERR, "Invalid high/low water setup value in KB");
3039                 PMD_INIT_LOG(ERR, "High_water must <= 0x%x", max_high_water);
3040                 return -EINVAL;
3041         }
3042
3043         hw->fc.requested_mode = rte_fcmode_2_txgbe_fcmode[pfc_conf->fc.mode];
3044         hw->fc.pause_time = pfc_conf->fc.pause_time;
3045         hw->fc.send_xon = pfc_conf->fc.send_xon;
3046         hw->fc.low_water[tc_num] =  pfc_conf->fc.low_water;
3047         hw->fc.high_water[tc_num] = pfc_conf->fc.high_water;
3048
3049         err = txgbe_dcb_pfc_enable(hw, tc_num);
3050
3051         /* Not negotiated is not an error case */
3052         if (err == 0 || err == TXGBE_ERR_FC_NOT_NEGOTIATED)
3053                 return 0;
3054
3055         PMD_INIT_LOG(ERR, "txgbe_dcb_pfc_enable = 0x%x", err);
3056         return -EIO;
3057 }
3058
3059 int
3060 txgbe_dev_rss_reta_update(struct rte_eth_dev *dev,
3061                           struct rte_eth_rss_reta_entry64 *reta_conf,
3062                           uint16_t reta_size)
3063 {
3064         uint8_t i, j, mask;
3065         uint32_t reta;
3066         uint16_t idx, shift;
3067         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
3068         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3069
3070         PMD_INIT_FUNC_TRACE();
3071
3072         if (!txgbe_rss_update_sp(hw->mac.type)) {
3073                 PMD_DRV_LOG(ERR, "RSS reta update is not supported on this "
3074                         "NIC.");
3075                 return -ENOTSUP;
3076         }
3077
3078         if (reta_size != ETH_RSS_RETA_SIZE_128) {
3079                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
3080                         "(%d) doesn't match the number hardware can supported "
3081                         "(%d)", reta_size, ETH_RSS_RETA_SIZE_128);
3082                 return -EINVAL;
3083         }
3084
3085         for (i = 0; i < reta_size; i += 4) {
3086                 idx = i / RTE_RETA_GROUP_SIZE;
3087                 shift = i % RTE_RETA_GROUP_SIZE;
3088                 mask = (uint8_t)RS64(reta_conf[idx].mask, shift, 0xF);
3089                 if (!mask)
3090                         continue;
3091
3092                 reta = rd32a(hw, TXGBE_REG_RSSTBL, i >> 2);
3093                 for (j = 0; j < 4; j++) {
3094                         if (RS8(mask, j, 0x1)) {
3095                                 reta  &= ~(MS32(8 * j, 0xFF));
3096                                 reta |= LS32(reta_conf[idx].reta[shift + j],
3097                                                 8 * j, 0xFF);
3098                         }
3099                 }
3100                 wr32a(hw, TXGBE_REG_RSSTBL, i >> 2, reta);
3101         }
3102         adapter->rss_reta_updated = 1;
3103
3104         return 0;
3105 }
3106
3107 int
3108 txgbe_dev_rss_reta_query(struct rte_eth_dev *dev,
3109                          struct rte_eth_rss_reta_entry64 *reta_conf,
3110                          uint16_t reta_size)
3111 {
3112         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3113         uint8_t i, j, mask;
3114         uint32_t reta;
3115         uint16_t idx, shift;
3116
3117         PMD_INIT_FUNC_TRACE();
3118
3119         if (reta_size != ETH_RSS_RETA_SIZE_128) {
3120                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
3121                         "(%d) doesn't match the number hardware can supported "
3122                         "(%d)", reta_size, ETH_RSS_RETA_SIZE_128);
3123                 return -EINVAL;
3124         }
3125
3126         for (i = 0; i < reta_size; i += 4) {
3127                 idx = i / RTE_RETA_GROUP_SIZE;
3128                 shift = i % RTE_RETA_GROUP_SIZE;
3129                 mask = (uint8_t)RS64(reta_conf[idx].mask, shift, 0xF);
3130                 if (!mask)
3131                         continue;
3132
3133                 reta = rd32a(hw, TXGBE_REG_RSSTBL, i >> 2);
3134                 for (j = 0; j < 4; j++) {
3135                         if (RS8(mask, j, 0x1))
3136                                 reta_conf[idx].reta[shift + j] =
3137                                         (uint16_t)RS32(reta, 8 * j, 0xFF);
3138                 }
3139         }
3140
3141         return 0;
3142 }
3143
3144 static int
3145 txgbe_add_rar(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
3146                                 uint32_t index, uint32_t pool)
3147 {
3148         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3149         uint32_t enable_addr = 1;
3150
3151         return txgbe_set_rar(hw, index, mac_addr->addr_bytes,
3152                              pool, enable_addr);
3153 }
3154
3155 static void
3156 txgbe_remove_rar(struct rte_eth_dev *dev, uint32_t index)
3157 {
3158         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3159
3160         txgbe_clear_rar(hw, index);
3161 }
3162
3163 static int
3164 txgbe_set_default_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *addr)
3165 {
3166         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
3167
3168         txgbe_remove_rar(dev, 0);
3169         txgbe_add_rar(dev, addr, 0, pci_dev->max_vfs);
3170
3171         return 0;
3172 }
3173
3174 static int
3175 txgbe_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
3176 {
3177         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3178         struct rte_eth_dev_info dev_info;
3179         uint32_t frame_size = mtu + RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
3180         struct rte_eth_dev_data *dev_data = dev->data;
3181         int ret;
3182
3183         ret = txgbe_dev_info_get(dev, &dev_info);
3184         if (ret != 0)
3185                 return ret;
3186
3187         /* check that mtu is within the allowed range */
3188         if (mtu < RTE_ETHER_MIN_MTU || frame_size > dev_info.max_rx_pktlen)
3189                 return -EINVAL;
3190
3191         /* If device is started, refuse mtu that requires the support of
3192          * scattered packets when this feature has not been enabled before.
3193          */
3194         if (dev_data->dev_started && !dev_data->scattered_rx &&
3195             (frame_size + 2 * TXGBE_VLAN_TAG_SIZE >
3196              dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM)) {
3197                 PMD_INIT_LOG(ERR, "Stop port first.");
3198                 return -EINVAL;
3199         }
3200
3201         /* update max frame size */
3202         dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
3203
3204         if (hw->mode)
3205                 wr32m(hw, TXGBE_FRMSZ, TXGBE_FRMSZ_MAX_MASK,
3206                         TXGBE_FRAME_SIZE_MAX);
3207         else
3208                 wr32m(hw, TXGBE_FRMSZ, TXGBE_FRMSZ_MAX_MASK,
3209                         TXGBE_FRMSZ_MAX(frame_size));
3210
3211         return 0;
3212 }
3213
3214 static uint32_t
3215 txgbe_uta_vector(struct txgbe_hw *hw, struct rte_ether_addr *uc_addr)
3216 {
3217         uint32_t vector = 0;
3218
3219         switch (hw->mac.mc_filter_type) {
3220         case 0:   /* use bits [47:36] of the address */
3221                 vector = ((uc_addr->addr_bytes[4] >> 4) |
3222                         (((uint16_t)uc_addr->addr_bytes[5]) << 4));
3223                 break;
3224         case 1:   /* use bits [46:35] of the address */
3225                 vector = ((uc_addr->addr_bytes[4] >> 3) |
3226                         (((uint16_t)uc_addr->addr_bytes[5]) << 5));
3227                 break;
3228         case 2:   /* use bits [45:34] of the address */
3229                 vector = ((uc_addr->addr_bytes[4] >> 2) |
3230                         (((uint16_t)uc_addr->addr_bytes[5]) << 6));
3231                 break;
3232         case 3:   /* use bits [43:32] of the address */
3233                 vector = ((uc_addr->addr_bytes[4]) |
3234                         (((uint16_t)uc_addr->addr_bytes[5]) << 8));
3235                 break;
3236         default:  /* Invalid mc_filter_type */
3237                 break;
3238         }
3239
3240         /* vector can only be 12-bits or boundary will be exceeded */
3241         vector &= 0xFFF;
3242         return vector;
3243 }
3244
3245 static int
3246 txgbe_uc_hash_table_set(struct rte_eth_dev *dev,
3247                         struct rte_ether_addr *mac_addr, uint8_t on)
3248 {
3249         uint32_t vector;
3250         uint32_t uta_idx;
3251         uint32_t reg_val;
3252         uint32_t uta_mask;
3253         uint32_t psrctl;
3254
3255         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3256         struct txgbe_uta_info *uta_info = TXGBE_DEV_UTA_INFO(dev);
3257
3258         /* The UTA table only exists on pf hardware */
3259         if (hw->mac.type < txgbe_mac_raptor)
3260                 return -ENOTSUP;
3261
3262         vector = txgbe_uta_vector(hw, mac_addr);
3263         uta_idx = (vector >> 5) & 0x7F;
3264         uta_mask = 0x1UL << (vector & 0x1F);
3265
3266         if (!!on == !!(uta_info->uta_shadow[uta_idx] & uta_mask))
3267                 return 0;
3268
3269         reg_val = rd32(hw, TXGBE_UCADDRTBL(uta_idx));
3270         if (on) {
3271                 uta_info->uta_in_use++;
3272                 reg_val |= uta_mask;
3273                 uta_info->uta_shadow[uta_idx] |= uta_mask;
3274         } else {
3275                 uta_info->uta_in_use--;
3276                 reg_val &= ~uta_mask;
3277                 uta_info->uta_shadow[uta_idx] &= ~uta_mask;
3278         }
3279
3280         wr32(hw, TXGBE_UCADDRTBL(uta_idx), reg_val);
3281
3282         psrctl = rd32(hw, TXGBE_PSRCTL);
3283         if (uta_info->uta_in_use > 0)
3284                 psrctl |= TXGBE_PSRCTL_UCHFENA;
3285         else
3286                 psrctl &= ~TXGBE_PSRCTL_UCHFENA;
3287
3288         psrctl &= ~TXGBE_PSRCTL_ADHF12_MASK;
3289         psrctl |= TXGBE_PSRCTL_ADHF12(hw->mac.mc_filter_type);
3290         wr32(hw, TXGBE_PSRCTL, psrctl);
3291
3292         return 0;
3293 }
3294
3295 static int
3296 txgbe_uc_all_hash_table_set(struct rte_eth_dev *dev, uint8_t on)
3297 {
3298         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3299         struct txgbe_uta_info *uta_info = TXGBE_DEV_UTA_INFO(dev);
3300         uint32_t psrctl;
3301         int i;
3302
3303         /* The UTA table only exists on pf hardware */
3304         if (hw->mac.type < txgbe_mac_raptor)
3305                 return -ENOTSUP;
3306
3307         if (on) {
3308                 for (i = 0; i < ETH_VMDQ_NUM_UC_HASH_ARRAY; i++) {
3309                         uta_info->uta_shadow[i] = ~0;
3310                         wr32(hw, TXGBE_UCADDRTBL(i), ~0);
3311                 }
3312         } else {
3313                 for (i = 0; i < ETH_VMDQ_NUM_UC_HASH_ARRAY; i++) {
3314                         uta_info->uta_shadow[i] = 0;
3315                         wr32(hw, TXGBE_UCADDRTBL(i), 0);
3316                 }
3317         }
3318
3319         psrctl = rd32(hw, TXGBE_PSRCTL);
3320         if (on)
3321                 psrctl |= TXGBE_PSRCTL_UCHFENA;
3322         else
3323                 psrctl &= ~TXGBE_PSRCTL_UCHFENA;
3324
3325         psrctl &= ~TXGBE_PSRCTL_ADHF12_MASK;
3326         psrctl |= TXGBE_PSRCTL_ADHF12(hw->mac.mc_filter_type);
3327         wr32(hw, TXGBE_PSRCTL, psrctl);
3328
3329         return 0;
3330 }
3331
3332 uint32_t
3333 txgbe_convert_vm_rx_mask_to_val(uint16_t rx_mask, uint32_t orig_val)
3334 {
3335         uint32_t new_val = orig_val;
3336
3337         if (rx_mask & ETH_VMDQ_ACCEPT_UNTAG)
3338                 new_val |= TXGBE_POOLETHCTL_UTA;
3339         if (rx_mask & ETH_VMDQ_ACCEPT_HASH_MC)
3340                 new_val |= TXGBE_POOLETHCTL_MCHA;
3341         if (rx_mask & ETH_VMDQ_ACCEPT_HASH_UC)
3342                 new_val |= TXGBE_POOLETHCTL_UCHA;
3343         if (rx_mask & ETH_VMDQ_ACCEPT_BROADCAST)
3344                 new_val |= TXGBE_POOLETHCTL_BCA;
3345         if (rx_mask & ETH_VMDQ_ACCEPT_MULTICAST)
3346                 new_val |= TXGBE_POOLETHCTL_MCP;
3347
3348         return new_val;
3349 }
3350
3351 static int
3352 txgbe_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
3353 {
3354         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
3355         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
3356         uint32_t mask;
3357         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3358
3359         if (queue_id < 32) {
3360                 mask = rd32(hw, TXGBE_IMS(0));
3361                 mask &= (1 << queue_id);
3362                 wr32(hw, TXGBE_IMS(0), mask);
3363         } else if (queue_id < 64) {
3364                 mask = rd32(hw, TXGBE_IMS(1));
3365                 mask &= (1 << (queue_id - 32));
3366                 wr32(hw, TXGBE_IMS(1), mask);
3367         }
3368         rte_intr_enable(intr_handle);
3369
3370         return 0;
3371 }
3372
3373 static int
3374 txgbe_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
3375 {
3376         uint32_t mask;
3377         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3378
3379         if (queue_id < 32) {
3380                 mask = rd32(hw, TXGBE_IMS(0));
3381                 mask &= ~(1 << queue_id);
3382                 wr32(hw, TXGBE_IMS(0), mask);
3383         } else if (queue_id < 64) {
3384                 mask = rd32(hw, TXGBE_IMS(1));
3385                 mask &= ~(1 << (queue_id - 32));
3386                 wr32(hw, TXGBE_IMS(1), mask);
3387         }
3388
3389         return 0;
3390 }
3391
3392 /**
3393  * set the IVAR registers, mapping interrupt causes to vectors
3394  * @param hw
3395  *  pointer to txgbe_hw struct
3396  * @direction
3397  *  0 for Rx, 1 for Tx, -1 for other causes
3398  * @queue
3399  *  queue to map the corresponding interrupt to
3400  * @msix_vector
3401  *  the vector to map to the corresponding queue
3402  */
3403 void
3404 txgbe_set_ivar_map(struct txgbe_hw *hw, int8_t direction,
3405                    uint8_t queue, uint8_t msix_vector)
3406 {
3407         uint32_t tmp, idx;
3408
3409         if (direction == -1) {
3410                 /* other causes */
3411                 msix_vector |= TXGBE_IVARMISC_VLD;
3412                 idx = 0;
3413                 tmp = rd32(hw, TXGBE_IVARMISC);
3414                 tmp &= ~(0xFF << idx);
3415                 tmp |= (msix_vector << idx);
3416                 wr32(hw, TXGBE_IVARMISC, tmp);
3417         } else {
3418                 /* rx or tx causes */
3419                 /* Workround for ICR lost */
3420                 idx = ((16 * (queue & 1)) + (8 * direction));
3421                 tmp = rd32(hw, TXGBE_IVAR(queue >> 1));
3422                 tmp &= ~(0xFF << idx);
3423                 tmp |= (msix_vector << idx);
3424                 wr32(hw, TXGBE_IVAR(queue >> 1), tmp);
3425         }
3426 }
3427
3428 /**
3429  * Sets up the hardware to properly generate MSI-X interrupts
3430  * @hw
3431  *  board private structure
3432  */
3433 static void
3434 txgbe_configure_msix(struct rte_eth_dev *dev)
3435 {
3436         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
3437         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
3438         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3439         uint32_t queue_id, base = TXGBE_MISC_VEC_ID;
3440         uint32_t vec = TXGBE_MISC_VEC_ID;
3441         uint32_t gpie;
3442
3443         /* won't configure msix register if no mapping is done
3444          * between intr vector and event fd
3445          * but if misx has been enabled already, need to configure
3446          * auto clean, auto mask and throttling.
3447          */
3448         gpie = rd32(hw, TXGBE_GPIE);
3449         if (!rte_intr_dp_is_en(intr_handle) &&
3450             !(gpie & TXGBE_GPIE_MSIX))
3451                 return;
3452
3453         if (rte_intr_allow_others(intr_handle)) {
3454                 base = TXGBE_RX_VEC_START;
3455                 vec = base;
3456         }
3457
3458         /* setup GPIE for MSI-x mode */
3459         gpie = rd32(hw, TXGBE_GPIE);
3460         gpie |= TXGBE_GPIE_MSIX;
3461         wr32(hw, TXGBE_GPIE, gpie);
3462
3463         /* Populate the IVAR table and set the ITR values to the
3464          * corresponding register.
3465          */
3466         if (rte_intr_dp_is_en(intr_handle)) {
3467                 for (queue_id = 0; queue_id < dev->data->nb_rx_queues;
3468                         queue_id++) {
3469                         /* by default, 1:1 mapping */
3470                         txgbe_set_ivar_map(hw, 0, queue_id, vec);
3471                         intr_handle->intr_vec[queue_id] = vec;
3472                         if (vec < base + intr_handle->nb_efd - 1)
3473                                 vec++;
3474                 }
3475
3476                 txgbe_set_ivar_map(hw, -1, 1, TXGBE_MISC_VEC_ID);
3477         }
3478         wr32(hw, TXGBE_ITR(TXGBE_MISC_VEC_ID),
3479                         TXGBE_ITR_IVAL_10G(TXGBE_QUEUE_ITR_INTERVAL_DEFAULT)
3480                         | TXGBE_ITR_WRDSA);
3481 }
3482
3483 int
3484 txgbe_set_queue_rate_limit(struct rte_eth_dev *dev,
3485                            uint16_t queue_idx, uint16_t tx_rate)
3486 {
3487         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3488         uint32_t bcnrc_val;
3489
3490         if (queue_idx >= hw->mac.max_tx_queues)
3491                 return -EINVAL;
3492
3493         if (tx_rate != 0) {
3494                 bcnrc_val = TXGBE_ARBTXRATE_MAX(tx_rate);
3495                 bcnrc_val |= TXGBE_ARBTXRATE_MIN(tx_rate / 2);
3496         } else {
3497                 bcnrc_val = 0;
3498         }
3499
3500         /*
3501          * Set global transmit compensation time to the MMW_SIZE in ARBTXMMW
3502          * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported.
3503          */
3504         wr32(hw, TXGBE_ARBTXMMW, 0x14);
3505
3506         /* Set ARBTXRATE of queue X */
3507         wr32(hw, TXGBE_ARBPOOLIDX, queue_idx);
3508         wr32(hw, TXGBE_ARBTXRATE, bcnrc_val);
3509         txgbe_flush(hw);
3510
3511         return 0;
3512 }
3513
3514 static inline enum txgbe_5tuple_protocol
3515 convert_protocol_type(uint8_t protocol_value)
3516 {
3517         if (protocol_value == IPPROTO_TCP)
3518                 return TXGBE_5TF_PROT_TCP;
3519         else if (protocol_value == IPPROTO_UDP)
3520                 return TXGBE_5TF_PROT_UDP;
3521         else if (protocol_value == IPPROTO_SCTP)
3522                 return TXGBE_5TF_PROT_SCTP;
3523         else
3524                 return TXGBE_5TF_PROT_NONE;
3525 }
3526
3527 /* inject a 5-tuple filter to HW */
3528 static inline void
3529 txgbe_inject_5tuple_filter(struct rte_eth_dev *dev,
3530                            struct txgbe_5tuple_filter *filter)
3531 {
3532         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3533         int i;
3534         uint32_t ftqf, sdpqf;
3535         uint32_t l34timir = 0;
3536         uint32_t mask = TXGBE_5TFCTL0_MASK;
3537
3538         i = filter->index;
3539         sdpqf = TXGBE_5TFPORT_DST(be_to_le16(filter->filter_info.dst_port));
3540         sdpqf |= TXGBE_5TFPORT_SRC(be_to_le16(filter->filter_info.src_port));
3541
3542         ftqf = TXGBE_5TFCTL0_PROTO(filter->filter_info.proto);
3543         ftqf |= TXGBE_5TFCTL0_PRI(filter->filter_info.priority);
3544         if (filter->filter_info.src_ip_mask == 0) /* 0 means compare. */
3545                 mask &= ~TXGBE_5TFCTL0_MSADDR;
3546         if (filter->filter_info.dst_ip_mask == 0)
3547                 mask &= ~TXGBE_5TFCTL0_MDADDR;
3548         if (filter->filter_info.src_port_mask == 0)
3549                 mask &= ~TXGBE_5TFCTL0_MSPORT;
3550         if (filter->filter_info.dst_port_mask == 0)
3551                 mask &= ~TXGBE_5TFCTL0_MDPORT;
3552         if (filter->filter_info.proto_mask == 0)
3553                 mask &= ~TXGBE_5TFCTL0_MPROTO;
3554         ftqf |= mask;
3555         ftqf |= TXGBE_5TFCTL0_MPOOL;
3556         ftqf |= TXGBE_5TFCTL0_ENA;
3557
3558         wr32(hw, TXGBE_5TFDADDR(i), be_to_le32(filter->filter_info.dst_ip));
3559         wr32(hw, TXGBE_5TFSADDR(i), be_to_le32(filter->filter_info.src_ip));
3560         wr32(hw, TXGBE_5TFPORT(i), sdpqf);
3561         wr32(hw, TXGBE_5TFCTL0(i), ftqf);
3562
3563         l34timir |= TXGBE_5TFCTL1_QP(filter->queue);
3564         wr32(hw, TXGBE_5TFCTL1(i), l34timir);
3565 }
3566
3567 /*
3568  * add a 5tuple filter
3569  *
3570  * @param
3571  * dev: Pointer to struct rte_eth_dev.
3572  * index: the index the filter allocates.
3573  * filter: pointer to the filter that will be added.
3574  * rx_queue: the queue id the filter assigned to.
3575  *
3576  * @return
3577  *    - On success, zero.
3578  *    - On failure, a negative value.
3579  */
3580 static int
3581 txgbe_add_5tuple_filter(struct rte_eth_dev *dev,
3582                         struct txgbe_5tuple_filter *filter)
3583 {
3584         struct txgbe_filter_info *filter_info = TXGBE_DEV_FILTER(dev);
3585         int i, idx, shift;
3586
3587         /*
3588          * look for an unused 5tuple filter index,
3589          * and insert the filter to list.
3590          */
3591         for (i = 0; i < TXGBE_MAX_FTQF_FILTERS; i++) {
3592                 idx = i / (sizeof(uint32_t) * NBBY);
3593                 shift = i % (sizeof(uint32_t) * NBBY);
3594                 if (!(filter_info->fivetuple_mask[idx] & (1 << shift))) {
3595                         filter_info->fivetuple_mask[idx] |= 1 << shift;
3596                         filter->index = i;
3597                         TAILQ_INSERT_TAIL(&filter_info->fivetuple_list,
3598                                           filter,
3599                                           entries);
3600                         break;
3601                 }
3602         }
3603         if (i >= TXGBE_MAX_FTQF_FILTERS) {
3604                 PMD_DRV_LOG(ERR, "5tuple filters are full.");
3605                 return -ENOSYS;
3606         }
3607
3608         txgbe_inject_5tuple_filter(dev, filter);
3609
3610         return 0;
3611 }
3612
3613 /*
3614  * remove a 5tuple filter
3615  *
3616  * @param
3617  * dev: Pointer to struct rte_eth_dev.
3618  * filter: the pointer of the filter will be removed.
3619  */
3620 static void
3621 txgbe_remove_5tuple_filter(struct rte_eth_dev *dev,
3622                         struct txgbe_5tuple_filter *filter)
3623 {
3624         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3625         struct txgbe_filter_info *filter_info = TXGBE_DEV_FILTER(dev);
3626         uint16_t index = filter->index;
3627
3628         filter_info->fivetuple_mask[index / (sizeof(uint32_t) * NBBY)] &=
3629                                 ~(1 << (index % (sizeof(uint32_t) * NBBY)));
3630         TAILQ_REMOVE(&filter_info->fivetuple_list, filter, entries);
3631         rte_free(filter);
3632
3633         wr32(hw, TXGBE_5TFDADDR(index), 0);
3634         wr32(hw, TXGBE_5TFSADDR(index), 0);
3635         wr32(hw, TXGBE_5TFPORT(index), 0);
3636         wr32(hw, TXGBE_5TFCTL0(index), 0);
3637         wr32(hw, TXGBE_5TFCTL1(index), 0);
3638 }
3639
3640 static inline struct txgbe_5tuple_filter *
3641 txgbe_5tuple_filter_lookup(struct txgbe_5tuple_filter_list *filter_list,
3642                         struct txgbe_5tuple_filter_info *key)
3643 {
3644         struct txgbe_5tuple_filter *it;
3645
3646         TAILQ_FOREACH(it, filter_list, entries) {
3647                 if (memcmp(key, &it->filter_info,
3648                         sizeof(struct txgbe_5tuple_filter_info)) == 0) {
3649                         return it;
3650                 }
3651         }
3652         return NULL;
3653 }
3654
3655 /* translate elements in struct rte_eth_ntuple_filter
3656  * to struct txgbe_5tuple_filter_info
3657  */
3658 static inline int
3659 ntuple_filter_to_5tuple(struct rte_eth_ntuple_filter *filter,
3660                         struct txgbe_5tuple_filter_info *filter_info)
3661 {
3662         if (filter->queue >= TXGBE_MAX_RX_QUEUE_NUM ||
3663                 filter->priority > TXGBE_5TUPLE_MAX_PRI ||
3664                 filter->priority < TXGBE_5TUPLE_MIN_PRI)
3665                 return -EINVAL;
3666
3667         switch (filter->dst_ip_mask) {
3668         case UINT32_MAX:
3669                 filter_info->dst_ip_mask = 0;
3670                 filter_info->dst_ip = filter->dst_ip;
3671                 break;
3672         case 0:
3673                 filter_info->dst_ip_mask = 1;
3674                 break;
3675         default:
3676                 PMD_DRV_LOG(ERR, "invalid dst_ip mask.");
3677                 return -EINVAL;
3678         }
3679
3680         switch (filter->src_ip_mask) {
3681         case UINT32_MAX:
3682                 filter_info->src_ip_mask = 0;
3683                 filter_info->src_ip = filter->src_ip;
3684                 break;
3685         case 0:
3686                 filter_info->src_ip_mask = 1;
3687                 break;
3688         default:
3689                 PMD_DRV_LOG(ERR, "invalid src_ip mask.");
3690                 return -EINVAL;
3691         }
3692
3693         switch (filter->dst_port_mask) {
3694         case UINT16_MAX:
3695                 filter_info->dst_port_mask = 0;
3696                 filter_info->dst_port = filter->dst_port;
3697                 break;
3698         case 0:
3699                 filter_info->dst_port_mask = 1;
3700                 break;
3701         default:
3702                 PMD_DRV_LOG(ERR, "invalid dst_port mask.");
3703                 return -EINVAL;
3704         }
3705
3706         switch (filter->src_port_mask) {
3707         case UINT16_MAX:
3708                 filter_info->src_port_mask = 0;
3709                 filter_info->src_port = filter->src_port;
3710                 break;
3711         case 0:
3712                 filter_info->src_port_mask = 1;
3713                 break;
3714         default:
3715                 PMD_DRV_LOG(ERR, "invalid src_port mask.");
3716                 return -EINVAL;
3717         }
3718
3719         switch (filter->proto_mask) {
3720         case UINT8_MAX:
3721                 filter_info->proto_mask = 0;
3722                 filter_info->proto =
3723                         convert_protocol_type(filter->proto);
3724                 break;
3725         case 0:
3726                 filter_info->proto_mask = 1;
3727                 break;
3728         default:
3729                 PMD_DRV_LOG(ERR, "invalid protocol mask.");
3730                 return -EINVAL;
3731         }
3732
3733         filter_info->priority = (uint8_t)filter->priority;
3734         return 0;
3735 }
3736
3737 /*
3738  * add or delete a ntuple filter
3739  *
3740  * @param
3741  * dev: Pointer to struct rte_eth_dev.
3742  * ntuple_filter: Pointer to struct rte_eth_ntuple_filter
3743  * add: if true, add filter, if false, remove filter
3744  *
3745  * @return
3746  *    - On success, zero.
3747  *    - On failure, a negative value.
3748  */
3749 int
3750 txgbe_add_del_ntuple_filter(struct rte_eth_dev *dev,
3751                         struct rte_eth_ntuple_filter *ntuple_filter,
3752                         bool add)
3753 {
3754         struct txgbe_filter_info *filter_info = TXGBE_DEV_FILTER(dev);
3755         struct txgbe_5tuple_filter_info filter_5tuple;
3756         struct txgbe_5tuple_filter *filter;
3757         int ret;
3758
3759         if (ntuple_filter->flags != RTE_5TUPLE_FLAGS) {
3760                 PMD_DRV_LOG(ERR, "only 5tuple is supported.");
3761                 return -EINVAL;
3762         }
3763
3764         memset(&filter_5tuple, 0, sizeof(struct txgbe_5tuple_filter_info));
3765         ret = ntuple_filter_to_5tuple(ntuple_filter, &filter_5tuple);
3766         if (ret < 0)
3767                 return ret;
3768
3769         filter = txgbe_5tuple_filter_lookup(&filter_info->fivetuple_list,
3770                                          &filter_5tuple);
3771         if (filter != NULL && add) {
3772                 PMD_DRV_LOG(ERR, "filter exists.");
3773                 return -EEXIST;
3774         }
3775         if (filter == NULL && !add) {
3776                 PMD_DRV_LOG(ERR, "filter doesn't exist.");
3777                 return -ENOENT;
3778         }
3779
3780         if (add) {
3781                 filter = rte_zmalloc("txgbe_5tuple_filter",
3782                                 sizeof(struct txgbe_5tuple_filter), 0);
3783                 if (filter == NULL)
3784                         return -ENOMEM;
3785                 rte_memcpy(&filter->filter_info,
3786                                  &filter_5tuple,
3787                                  sizeof(struct txgbe_5tuple_filter_info));
3788                 filter->queue = ntuple_filter->queue;
3789                 ret = txgbe_add_5tuple_filter(dev, filter);
3790                 if (ret < 0) {
3791                         rte_free(filter);
3792                         return ret;
3793                 }
3794         } else {
3795                 txgbe_remove_5tuple_filter(dev, filter);
3796         }
3797
3798         return 0;
3799 }
3800
3801 int
3802 txgbe_add_del_ethertype_filter(struct rte_eth_dev *dev,
3803                         struct rte_eth_ethertype_filter *filter,
3804                         bool add)
3805 {
3806         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3807         struct txgbe_filter_info *filter_info = TXGBE_DEV_FILTER(dev);
3808         uint32_t etqf = 0;
3809         uint32_t etqs = 0;
3810         int ret;
3811         struct txgbe_ethertype_filter ethertype_filter;
3812
3813         if (filter->queue >= TXGBE_MAX_RX_QUEUE_NUM)
3814                 return -EINVAL;
3815
3816         if (filter->ether_type == RTE_ETHER_TYPE_IPV4 ||
3817             filter->ether_type == RTE_ETHER_TYPE_IPV6) {
3818                 PMD_DRV_LOG(ERR, "unsupported ether_type(0x%04x) in"
3819                         " ethertype filter.", filter->ether_type);
3820                 return -EINVAL;
3821         }
3822
3823         if (filter->flags & RTE_ETHTYPE_FLAGS_MAC) {
3824                 PMD_DRV_LOG(ERR, "mac compare is unsupported.");
3825                 return -EINVAL;
3826         }
3827         if (filter->flags & RTE_ETHTYPE_FLAGS_DROP) {
3828                 PMD_DRV_LOG(ERR, "drop option is unsupported.");
3829                 return -EINVAL;
3830         }
3831
3832         ret = txgbe_ethertype_filter_lookup(filter_info, filter->ether_type);
3833         if (ret >= 0 && add) {
3834                 PMD_DRV_LOG(ERR, "ethertype (0x%04x) filter exists.",
3835                             filter->ether_type);
3836                 return -EEXIST;
3837         }
3838         if (ret < 0 && !add) {
3839                 PMD_DRV_LOG(ERR, "ethertype (0x%04x) filter doesn't exist.",
3840                             filter->ether_type);
3841                 return -ENOENT;
3842         }
3843
3844         if (add) {
3845                 etqf = TXGBE_ETFLT_ENA;
3846                 etqf |= TXGBE_ETFLT_ETID(filter->ether_type);
3847                 etqs |= TXGBE_ETCLS_QPID(filter->queue);
3848                 etqs |= TXGBE_ETCLS_QENA;
3849
3850                 ethertype_filter.ethertype = filter->ether_type;
3851                 ethertype_filter.etqf = etqf;
3852                 ethertype_filter.etqs = etqs;
3853                 ethertype_filter.conf = FALSE;
3854                 ret = txgbe_ethertype_filter_insert(filter_info,
3855                                                     &ethertype_filter);
3856                 if (ret < 0) {
3857                         PMD_DRV_LOG(ERR, "ethertype filters are full.");
3858                         return -ENOSPC;
3859                 }
3860         } else {
3861                 ret = txgbe_ethertype_filter_remove(filter_info, (uint8_t)ret);
3862                 if (ret < 0)
3863                         return -ENOSYS;
3864         }
3865         wr32(hw, TXGBE_ETFLT(ret), etqf);
3866         wr32(hw, TXGBE_ETCLS(ret), etqs);
3867         txgbe_flush(hw);
3868
3869         return 0;
3870 }
3871
3872 static int
3873 txgbe_dev_filter_ctrl(__rte_unused struct rte_eth_dev *dev,
3874                      enum rte_filter_type filter_type,
3875                      enum rte_filter_op filter_op,
3876                      void *arg)
3877 {
3878         int ret = 0;
3879
3880         switch (filter_type) {
3881         case RTE_ETH_FILTER_GENERIC:
3882                 if (filter_op != RTE_ETH_FILTER_GET)
3883                         return -EINVAL;
3884                 *(const void **)arg = &txgbe_flow_ops;
3885                 break;
3886         default:
3887                 PMD_DRV_LOG(WARNING, "Filter type (%d) not supported",
3888                                                         filter_type);
3889                 ret = -EINVAL;
3890                 break;
3891         }
3892
3893         return ret;
3894 }
3895
3896 static u8 *
3897 txgbe_dev_addr_list_itr(__rte_unused struct txgbe_hw *hw,
3898                         u8 **mc_addr_ptr, u32 *vmdq)
3899 {
3900         u8 *mc_addr;
3901
3902         *vmdq = 0;
3903         mc_addr = *mc_addr_ptr;
3904         *mc_addr_ptr = (mc_addr + sizeof(struct rte_ether_addr));
3905         return mc_addr;
3906 }
3907
3908 int
3909 txgbe_dev_set_mc_addr_list(struct rte_eth_dev *dev,
3910                           struct rte_ether_addr *mc_addr_set,
3911                           uint32_t nb_mc_addr)
3912 {
3913         struct txgbe_hw *hw;
3914         u8 *mc_addr_list;
3915
3916         hw = TXGBE_DEV_HW(dev);
3917         mc_addr_list = (u8 *)mc_addr_set;
3918         return txgbe_update_mc_addr_list(hw, mc_addr_list, nb_mc_addr,
3919                                          txgbe_dev_addr_list_itr, TRUE);
3920 }
3921
3922 static uint64_t
3923 txgbe_read_systime_cyclecounter(struct rte_eth_dev *dev)
3924 {
3925         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3926         uint64_t systime_cycles;
3927
3928         systime_cycles = (uint64_t)rd32(hw, TXGBE_TSTIMEL);
3929         systime_cycles |= (uint64_t)rd32(hw, TXGBE_TSTIMEH) << 32;
3930
3931         return systime_cycles;
3932 }
3933
3934 static uint64_t
3935 txgbe_read_rx_tstamp_cyclecounter(struct rte_eth_dev *dev)
3936 {
3937         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3938         uint64_t rx_tstamp_cycles;
3939
3940         /* TSRXSTMPL stores ns and TSRXSTMPH stores seconds. */
3941         rx_tstamp_cycles = (uint64_t)rd32(hw, TXGBE_TSRXSTMPL);
3942         rx_tstamp_cycles |= (uint64_t)rd32(hw, TXGBE_TSRXSTMPH) << 32;
3943
3944         return rx_tstamp_cycles;
3945 }
3946
3947 static uint64_t
3948 txgbe_read_tx_tstamp_cyclecounter(struct rte_eth_dev *dev)
3949 {
3950         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3951         uint64_t tx_tstamp_cycles;
3952
3953         /* TSTXSTMPL stores ns and TSTXSTMPH stores seconds. */
3954         tx_tstamp_cycles = (uint64_t)rd32(hw, TXGBE_TSTXSTMPL);
3955         tx_tstamp_cycles |= (uint64_t)rd32(hw, TXGBE_TSTXSTMPH) << 32;
3956
3957         return tx_tstamp_cycles;
3958 }
3959
3960 static void
3961 txgbe_start_timecounters(struct rte_eth_dev *dev)
3962 {
3963         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3964         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
3965         struct rte_eth_link link;
3966         uint32_t incval = 0;
3967         uint32_t shift = 0;
3968
3969         /* Get current link speed. */
3970         txgbe_dev_link_update(dev, 1);
3971         rte_eth_linkstatus_get(dev, &link);
3972
3973         switch (link.link_speed) {
3974         case ETH_SPEED_NUM_100M:
3975                 incval = TXGBE_INCVAL_100;
3976                 shift = TXGBE_INCVAL_SHIFT_100;
3977                 break;
3978         case ETH_SPEED_NUM_1G:
3979                 incval = TXGBE_INCVAL_1GB;
3980                 shift = TXGBE_INCVAL_SHIFT_1GB;
3981                 break;
3982         case ETH_SPEED_NUM_10G:
3983         default:
3984                 incval = TXGBE_INCVAL_10GB;
3985                 shift = TXGBE_INCVAL_SHIFT_10GB;
3986                 break;
3987         }
3988
3989         wr32(hw, TXGBE_TSTIMEINC, TXGBE_TSTIMEINC_VP(incval, 2));
3990
3991         memset(&adapter->systime_tc, 0, sizeof(struct rte_timecounter));
3992         memset(&adapter->rx_tstamp_tc, 0, sizeof(struct rte_timecounter));
3993         memset(&adapter->tx_tstamp_tc, 0, sizeof(struct rte_timecounter));
3994
3995         adapter->systime_tc.cc_mask = TXGBE_CYCLECOUNTER_MASK;
3996         adapter->systime_tc.cc_shift = shift;
3997         adapter->systime_tc.nsec_mask = (1ULL << shift) - 1;
3998
3999         adapter->rx_tstamp_tc.cc_mask = TXGBE_CYCLECOUNTER_MASK;
4000         adapter->rx_tstamp_tc.cc_shift = shift;
4001         adapter->rx_tstamp_tc.nsec_mask = (1ULL << shift) - 1;
4002
4003         adapter->tx_tstamp_tc.cc_mask = TXGBE_CYCLECOUNTER_MASK;
4004         adapter->tx_tstamp_tc.cc_shift = shift;
4005         adapter->tx_tstamp_tc.nsec_mask = (1ULL << shift) - 1;
4006 }
4007
4008 static int
4009 txgbe_timesync_adjust_time(struct rte_eth_dev *dev, int64_t delta)
4010 {
4011         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
4012
4013         adapter->systime_tc.nsec += delta;
4014         adapter->rx_tstamp_tc.nsec += delta;
4015         adapter->tx_tstamp_tc.nsec += delta;
4016
4017         return 0;
4018 }
4019
4020 static int
4021 txgbe_timesync_write_time(struct rte_eth_dev *dev, const struct timespec *ts)
4022 {
4023         uint64_t ns;
4024         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
4025
4026         ns = rte_timespec_to_ns(ts);
4027         /* Set the timecounters to a new value. */
4028         adapter->systime_tc.nsec = ns;
4029         adapter->rx_tstamp_tc.nsec = ns;
4030         adapter->tx_tstamp_tc.nsec = ns;
4031
4032         return 0;
4033 }
4034
4035 static int
4036 txgbe_timesync_read_time(struct rte_eth_dev *dev, struct timespec *ts)
4037 {
4038         uint64_t ns, systime_cycles;
4039         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
4040
4041         systime_cycles = txgbe_read_systime_cyclecounter(dev);
4042         ns = rte_timecounter_update(&adapter->systime_tc, systime_cycles);
4043         *ts = rte_ns_to_timespec(ns);
4044
4045         return 0;
4046 }
4047
4048 static int
4049 txgbe_timesync_enable(struct rte_eth_dev *dev)
4050 {
4051         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4052         uint32_t tsync_ctl;
4053
4054         /* Stop the timesync system time. */
4055         wr32(hw, TXGBE_TSTIMEINC, 0x0);
4056         /* Reset the timesync system time value. */
4057         wr32(hw, TXGBE_TSTIMEL, 0x0);
4058         wr32(hw, TXGBE_TSTIMEH, 0x0);
4059
4060         txgbe_start_timecounters(dev);
4061
4062         /* Enable L2 filtering of IEEE1588/802.1AS Ethernet frame types. */
4063         wr32(hw, TXGBE_ETFLT(TXGBE_ETF_ID_1588),
4064                 RTE_ETHER_TYPE_1588 | TXGBE_ETFLT_ENA | TXGBE_ETFLT_1588);
4065
4066         /* Enable timestamping of received PTP packets. */
4067         tsync_ctl = rd32(hw, TXGBE_TSRXCTL);
4068         tsync_ctl |= TXGBE_TSRXCTL_ENA;
4069         wr32(hw, TXGBE_TSRXCTL, tsync_ctl);
4070
4071         /* Enable timestamping of transmitted PTP packets. */
4072         tsync_ctl = rd32(hw, TXGBE_TSTXCTL);
4073         tsync_ctl |= TXGBE_TSTXCTL_ENA;
4074         wr32(hw, TXGBE_TSTXCTL, tsync_ctl);
4075
4076         txgbe_flush(hw);
4077
4078         return 0;
4079 }
4080
4081 static int
4082 txgbe_timesync_disable(struct rte_eth_dev *dev)
4083 {
4084         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4085         uint32_t tsync_ctl;
4086
4087         /* Disable timestamping of transmitted PTP packets. */
4088         tsync_ctl = rd32(hw, TXGBE_TSTXCTL);
4089         tsync_ctl &= ~TXGBE_TSTXCTL_ENA;
4090         wr32(hw, TXGBE_TSTXCTL, tsync_ctl);
4091
4092         /* Disable timestamping of received PTP packets. */
4093         tsync_ctl = rd32(hw, TXGBE_TSRXCTL);
4094         tsync_ctl &= ~TXGBE_TSRXCTL_ENA;
4095         wr32(hw, TXGBE_TSRXCTL, tsync_ctl);
4096
4097         /* Disable L2 filtering of IEEE1588/802.1AS Ethernet frame types. */
4098         wr32(hw, TXGBE_ETFLT(TXGBE_ETF_ID_1588), 0);
4099
4100         /* Stop incrementating the System Time registers. */
4101         wr32(hw, TXGBE_TSTIMEINC, 0);
4102
4103         return 0;
4104 }
4105
4106 static int
4107 txgbe_timesync_read_rx_timestamp(struct rte_eth_dev *dev,
4108                                  struct timespec *timestamp,
4109                                  uint32_t flags __rte_unused)
4110 {
4111         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4112         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
4113         uint32_t tsync_rxctl;
4114         uint64_t rx_tstamp_cycles;
4115         uint64_t ns;
4116
4117         tsync_rxctl = rd32(hw, TXGBE_TSRXCTL);
4118         if ((tsync_rxctl & TXGBE_TSRXCTL_VLD) == 0)
4119                 return -EINVAL;
4120
4121         rx_tstamp_cycles = txgbe_read_rx_tstamp_cyclecounter(dev);
4122         ns = rte_timecounter_update(&adapter->rx_tstamp_tc, rx_tstamp_cycles);
4123         *timestamp = rte_ns_to_timespec(ns);
4124
4125         return  0;
4126 }
4127
4128 static int
4129 txgbe_timesync_read_tx_timestamp(struct rte_eth_dev *dev,
4130                                  struct timespec *timestamp)
4131 {
4132         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4133         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
4134         uint32_t tsync_txctl;
4135         uint64_t tx_tstamp_cycles;
4136         uint64_t ns;
4137
4138         tsync_txctl = rd32(hw, TXGBE_TSTXCTL);
4139         if ((tsync_txctl & TXGBE_TSTXCTL_VLD) == 0)
4140                 return -EINVAL;
4141
4142         tx_tstamp_cycles = txgbe_read_tx_tstamp_cyclecounter(dev);
4143         ns = rte_timecounter_update(&adapter->tx_tstamp_tc, tx_tstamp_cycles);
4144         *timestamp = rte_ns_to_timespec(ns);
4145
4146         return 0;
4147 }
4148
4149 static int
4150 txgbe_get_reg_length(struct rte_eth_dev *dev __rte_unused)
4151 {
4152         int count = 0;
4153         int g_ind = 0;
4154         const struct reg_info *reg_group;
4155         const struct reg_info **reg_set = txgbe_regs_others;
4156
4157         while ((reg_group = reg_set[g_ind++]))
4158                 count += txgbe_regs_group_count(reg_group);
4159
4160         return count;
4161 }
4162
4163 static int
4164 txgbe_get_regs(struct rte_eth_dev *dev,
4165               struct rte_dev_reg_info *regs)
4166 {
4167         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4168         uint32_t *data = regs->data;
4169         int g_ind = 0;
4170         int count = 0;
4171         const struct reg_info *reg_group;
4172         const struct reg_info **reg_set = txgbe_regs_others;
4173
4174         if (data == NULL) {
4175                 regs->length = txgbe_get_reg_length(dev);
4176                 regs->width = sizeof(uint32_t);
4177                 return 0;
4178         }
4179
4180         /* Support only full register dump */
4181         if (regs->length == 0 ||
4182             regs->length == (uint32_t)txgbe_get_reg_length(dev)) {
4183                 regs->version = hw->mac.type << 24 |
4184                                 hw->revision_id << 16 |
4185                                 hw->device_id;
4186                 while ((reg_group = reg_set[g_ind++]))
4187                         count += txgbe_read_regs_group(dev, &data[count],
4188                                                       reg_group);
4189                 return 0;
4190         }
4191
4192         return -ENOTSUP;
4193 }
4194
4195 static int
4196 txgbe_get_eeprom_length(struct rte_eth_dev *dev)
4197 {
4198         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4199
4200         /* Return unit is byte count */
4201         return hw->rom.word_size * 2;
4202 }
4203
4204 static int
4205 txgbe_get_eeprom(struct rte_eth_dev *dev,
4206                 struct rte_dev_eeprom_info *in_eeprom)
4207 {
4208         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4209         struct txgbe_rom_info *eeprom = &hw->rom;
4210         uint16_t *data = in_eeprom->data;
4211         int first, length;
4212
4213         first = in_eeprom->offset >> 1;
4214         length = in_eeprom->length >> 1;
4215         if (first > hw->rom.word_size ||
4216             ((first + length) > hw->rom.word_size))
4217                 return -EINVAL;
4218
4219         in_eeprom->magic = hw->vendor_id | (hw->device_id << 16);
4220
4221         return eeprom->readw_buffer(hw, first, length, data);
4222 }
4223
4224 static int
4225 txgbe_set_eeprom(struct rte_eth_dev *dev,
4226                 struct rte_dev_eeprom_info *in_eeprom)
4227 {
4228         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4229         struct txgbe_rom_info *eeprom = &hw->rom;
4230         uint16_t *data = in_eeprom->data;
4231         int first, length;
4232
4233         first = in_eeprom->offset >> 1;
4234         length = in_eeprom->length >> 1;
4235         if (first > hw->rom.word_size ||
4236             ((first + length) > hw->rom.word_size))
4237                 return -EINVAL;
4238
4239         in_eeprom->magic = hw->vendor_id | (hw->device_id << 16);
4240
4241         return eeprom->writew_buffer(hw,  first, length, data);
4242 }
4243
4244 static int
4245 txgbe_get_module_info(struct rte_eth_dev *dev,
4246                       struct rte_eth_dev_module_info *modinfo)
4247 {
4248         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4249         uint32_t status;
4250         uint8_t sff8472_rev, addr_mode;
4251         bool page_swap = false;
4252
4253         /* Check whether we support SFF-8472 or not */
4254         status = hw->phy.read_i2c_eeprom(hw,
4255                                              TXGBE_SFF_SFF_8472_COMP,
4256                                              &sff8472_rev);
4257         if (status != 0)
4258                 return -EIO;
4259
4260         /* addressing mode is not supported */
4261         status = hw->phy.read_i2c_eeprom(hw,
4262                                              TXGBE_SFF_SFF_8472_SWAP,
4263                                              &addr_mode);
4264         if (status != 0)
4265                 return -EIO;
4266
4267         if (addr_mode & TXGBE_SFF_ADDRESSING_MODE) {
4268                 PMD_DRV_LOG(ERR,
4269                             "Address change required to access page 0xA2, "
4270                             "but not supported. Please report the module "
4271                             "type to the driver maintainers.");
4272                 page_swap = true;
4273         }
4274
4275         if (sff8472_rev == TXGBE_SFF_SFF_8472_UNSUP || page_swap) {
4276                 /* We have a SFP, but it does not support SFF-8472 */
4277                 modinfo->type = RTE_ETH_MODULE_SFF_8079;
4278                 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8079_LEN;
4279         } else {
4280                 /* We have a SFP which supports a revision of SFF-8472. */
4281                 modinfo->type = RTE_ETH_MODULE_SFF_8472;
4282                 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8472_LEN;
4283         }
4284
4285         return 0;
4286 }
4287
4288 static int
4289 txgbe_get_module_eeprom(struct rte_eth_dev *dev,
4290                         struct rte_dev_eeprom_info *info)
4291 {
4292         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4293         uint32_t status = TXGBE_ERR_PHY_ADDR_INVALID;
4294         uint8_t databyte = 0xFF;
4295         uint8_t *data = info->data;
4296         uint32_t i = 0;
4297
4298         if (info->length == 0)
4299                 return -EINVAL;
4300
4301         for (i = info->offset; i < info->offset + info->length; i++) {
4302                 if (i < RTE_ETH_MODULE_SFF_8079_LEN)
4303                         status = hw->phy.read_i2c_eeprom(hw, i, &databyte);
4304                 else
4305                         status = hw->phy.read_i2c_sff8472(hw, i, &databyte);
4306
4307                 if (status != 0)
4308                         return -EIO;
4309
4310                 data[i - info->offset] = databyte;
4311         }
4312
4313         return 0;
4314 }
4315
4316 bool
4317 txgbe_rss_update_sp(enum txgbe_mac_type mac_type)
4318 {
4319         switch (mac_type) {
4320         case txgbe_mac_raptor:
4321                 return 1;
4322         default:
4323                 return 0;
4324         }
4325 }
4326
4327 static int
4328 txgbe_dev_get_dcb_info(struct rte_eth_dev *dev,
4329                         struct rte_eth_dcb_info *dcb_info)
4330 {
4331         struct txgbe_dcb_config *dcb_config = TXGBE_DEV_DCB_CONFIG(dev);
4332         struct txgbe_dcb_tc_config *tc;
4333         struct rte_eth_dcb_tc_queue_mapping *tc_queue;
4334         uint8_t nb_tcs;
4335         uint8_t i, j;
4336
4337         if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_DCB_FLAG)
4338                 dcb_info->nb_tcs = dcb_config->num_tcs.pg_tcs;
4339         else
4340                 dcb_info->nb_tcs = 1;
4341
4342         tc_queue = &dcb_info->tc_queue;
4343         nb_tcs = dcb_info->nb_tcs;
4344
4345         if (dcb_config->vt_mode) { /* vt is enabled */
4346                 struct rte_eth_vmdq_dcb_conf *vmdq_rx_conf =
4347                                 &dev->data->dev_conf.rx_adv_conf.vmdq_dcb_conf;
4348                 for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++)
4349                         dcb_info->prio_tc[i] = vmdq_rx_conf->dcb_tc[i];
4350                 if (RTE_ETH_DEV_SRIOV(dev).active > 0) {
4351                         for (j = 0; j < nb_tcs; j++) {
4352                                 tc_queue->tc_rxq[0][j].base = j;
4353                                 tc_queue->tc_rxq[0][j].nb_queue = 1;
4354                                 tc_queue->tc_txq[0][j].base = j;
4355                                 tc_queue->tc_txq[0][j].nb_queue = 1;
4356                         }
4357                 } else {
4358                         for (i = 0; i < vmdq_rx_conf->nb_queue_pools; i++) {
4359                                 for (j = 0; j < nb_tcs; j++) {
4360                                         tc_queue->tc_rxq[i][j].base =
4361                                                 i * nb_tcs + j;
4362                                         tc_queue->tc_rxq[i][j].nb_queue = 1;
4363                                         tc_queue->tc_txq[i][j].base =
4364                                                 i * nb_tcs + j;
4365                                         tc_queue->tc_txq[i][j].nb_queue = 1;
4366                                 }
4367                         }
4368                 }
4369         } else { /* vt is disabled */
4370                 struct rte_eth_dcb_rx_conf *rx_conf =
4371                                 &dev->data->dev_conf.rx_adv_conf.dcb_rx_conf;
4372                 for (i = 0; i < ETH_DCB_NUM_USER_PRIORITIES; i++)
4373                         dcb_info->prio_tc[i] = rx_conf->dcb_tc[i];
4374                 if (dcb_info->nb_tcs == ETH_4_TCS) {
4375                         for (i = 0; i < dcb_info->nb_tcs; i++) {
4376                                 dcb_info->tc_queue.tc_rxq[0][i].base = i * 32;
4377                                 dcb_info->tc_queue.tc_rxq[0][i].nb_queue = 16;
4378                         }
4379                         dcb_info->tc_queue.tc_txq[0][0].base = 0;
4380                         dcb_info->tc_queue.tc_txq[0][1].base = 64;
4381                         dcb_info->tc_queue.tc_txq[0][2].base = 96;
4382                         dcb_info->tc_queue.tc_txq[0][3].base = 112;
4383                         dcb_info->tc_queue.tc_txq[0][0].nb_queue = 64;
4384                         dcb_info->tc_queue.tc_txq[0][1].nb_queue = 32;
4385                         dcb_info->tc_queue.tc_txq[0][2].nb_queue = 16;
4386                         dcb_info->tc_queue.tc_txq[0][3].nb_queue = 16;
4387                 } else if (dcb_info->nb_tcs == ETH_8_TCS) {
4388                         for (i = 0; i < dcb_info->nb_tcs; i++) {
4389                                 dcb_info->tc_queue.tc_rxq[0][i].base = i * 16;
4390                                 dcb_info->tc_queue.tc_rxq[0][i].nb_queue = 16;
4391                         }
4392                         dcb_info->tc_queue.tc_txq[0][0].base = 0;
4393                         dcb_info->tc_queue.tc_txq[0][1].base = 32;
4394                         dcb_info->tc_queue.tc_txq[0][2].base = 64;
4395                         dcb_info->tc_queue.tc_txq[0][3].base = 80;
4396                         dcb_info->tc_queue.tc_txq[0][4].base = 96;
4397                         dcb_info->tc_queue.tc_txq[0][5].base = 104;
4398                         dcb_info->tc_queue.tc_txq[0][6].base = 112;
4399                         dcb_info->tc_queue.tc_txq[0][7].base = 120;
4400                         dcb_info->tc_queue.tc_txq[0][0].nb_queue = 32;
4401                         dcb_info->tc_queue.tc_txq[0][1].nb_queue = 32;
4402                         dcb_info->tc_queue.tc_txq[0][2].nb_queue = 16;
4403                         dcb_info->tc_queue.tc_txq[0][3].nb_queue = 16;
4404                         dcb_info->tc_queue.tc_txq[0][4].nb_queue = 8;
4405                         dcb_info->tc_queue.tc_txq[0][5].nb_queue = 8;
4406                         dcb_info->tc_queue.tc_txq[0][6].nb_queue = 8;
4407                         dcb_info->tc_queue.tc_txq[0][7].nb_queue = 8;
4408                 }
4409         }
4410         for (i = 0; i < dcb_info->nb_tcs; i++) {
4411                 tc = &dcb_config->tc_config[i];
4412                 dcb_info->tc_bws[i] = tc->path[TXGBE_DCB_TX_CONFIG].bwg_percent;
4413         }
4414         return 0;
4415 }
4416
4417 /* restore n-tuple filter */
4418 static inline void
4419 txgbe_ntuple_filter_restore(struct rte_eth_dev *dev)
4420 {
4421         struct txgbe_filter_info *filter_info = TXGBE_DEV_FILTER(dev);
4422         struct txgbe_5tuple_filter *node;
4423
4424         TAILQ_FOREACH(node, &filter_info->fivetuple_list, entries) {
4425                 txgbe_inject_5tuple_filter(dev, node);
4426         }
4427 }
4428
4429 /* restore ethernet type filter */
4430 static inline void
4431 txgbe_ethertype_filter_restore(struct rte_eth_dev *dev)
4432 {
4433         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
4434         struct txgbe_filter_info *filter_info = TXGBE_DEV_FILTER(dev);
4435         int i;
4436
4437         for (i = 0; i < TXGBE_ETF_ID_MAX; i++) {
4438                 if (filter_info->ethertype_mask & (1 << i)) {
4439                         wr32(hw, TXGBE_ETFLT(i),
4440                                         filter_info->ethertype_filters[i].etqf);
4441                         wr32(hw, TXGBE_ETCLS(i),
4442                                         filter_info->ethertype_filters[i].etqs);
4443                         txgbe_flush(hw);
4444                 }
4445         }
4446 }
4447
4448 static int
4449 txgbe_filter_restore(struct rte_eth_dev *dev)
4450 {
4451         txgbe_ntuple_filter_restore(dev);
4452         txgbe_ethertype_filter_restore(dev);
4453
4454         return 0;
4455 }
4456
4457 static const struct eth_dev_ops txgbe_eth_dev_ops = {
4458         .dev_configure              = txgbe_dev_configure,
4459         .dev_infos_get              = txgbe_dev_info_get,
4460         .dev_start                  = txgbe_dev_start,
4461         .dev_stop                   = txgbe_dev_stop,
4462         .dev_set_link_up            = txgbe_dev_set_link_up,
4463         .dev_set_link_down          = txgbe_dev_set_link_down,
4464         .dev_close                  = txgbe_dev_close,
4465         .dev_reset                  = txgbe_dev_reset,
4466         .promiscuous_enable         = txgbe_dev_promiscuous_enable,
4467         .promiscuous_disable        = txgbe_dev_promiscuous_disable,
4468         .allmulticast_enable        = txgbe_dev_allmulticast_enable,
4469         .allmulticast_disable       = txgbe_dev_allmulticast_disable,
4470         .link_update                = txgbe_dev_link_update,
4471         .stats_get                  = txgbe_dev_stats_get,
4472         .xstats_get                 = txgbe_dev_xstats_get,
4473         .xstats_get_by_id           = txgbe_dev_xstats_get_by_id,
4474         .stats_reset                = txgbe_dev_stats_reset,
4475         .xstats_reset               = txgbe_dev_xstats_reset,
4476         .xstats_get_names           = txgbe_dev_xstats_get_names,
4477         .xstats_get_names_by_id     = txgbe_dev_xstats_get_names_by_id,
4478         .queue_stats_mapping_set    = txgbe_dev_queue_stats_mapping_set,
4479         .fw_version_get             = txgbe_fw_version_get,
4480         .dev_supported_ptypes_get   = txgbe_dev_supported_ptypes_get,
4481         .mtu_set                    = txgbe_dev_mtu_set,
4482         .vlan_filter_set            = txgbe_vlan_filter_set,
4483         .vlan_tpid_set              = txgbe_vlan_tpid_set,
4484         .vlan_offload_set           = txgbe_vlan_offload_set,
4485         .vlan_strip_queue_set       = txgbe_vlan_strip_queue_set,
4486         .rx_queue_start             = txgbe_dev_rx_queue_start,
4487         .rx_queue_stop              = txgbe_dev_rx_queue_stop,
4488         .tx_queue_start             = txgbe_dev_tx_queue_start,
4489         .tx_queue_stop              = txgbe_dev_tx_queue_stop,
4490         .rx_queue_setup             = txgbe_dev_rx_queue_setup,
4491         .rx_queue_intr_enable       = txgbe_dev_rx_queue_intr_enable,
4492         .rx_queue_intr_disable      = txgbe_dev_rx_queue_intr_disable,
4493         .rx_queue_release           = txgbe_dev_rx_queue_release,
4494         .tx_queue_setup             = txgbe_dev_tx_queue_setup,
4495         .tx_queue_release           = txgbe_dev_tx_queue_release,
4496         .dev_led_on                 = txgbe_dev_led_on,
4497         .dev_led_off                = txgbe_dev_led_off,
4498         .flow_ctrl_get              = txgbe_flow_ctrl_get,
4499         .flow_ctrl_set              = txgbe_flow_ctrl_set,
4500         .priority_flow_ctrl_set     = txgbe_priority_flow_ctrl_set,
4501         .mac_addr_add               = txgbe_add_rar,
4502         .mac_addr_remove            = txgbe_remove_rar,
4503         .mac_addr_set               = txgbe_set_default_mac_addr,
4504         .uc_hash_table_set          = txgbe_uc_hash_table_set,
4505         .uc_all_hash_table_set      = txgbe_uc_all_hash_table_set,
4506         .set_queue_rate_limit       = txgbe_set_queue_rate_limit,
4507         .reta_update                = txgbe_dev_rss_reta_update,
4508         .reta_query                 = txgbe_dev_rss_reta_query,
4509         .rss_hash_update            = txgbe_dev_rss_hash_update,
4510         .rss_hash_conf_get          = txgbe_dev_rss_hash_conf_get,
4511         .filter_ctrl                = txgbe_dev_filter_ctrl,
4512         .set_mc_addr_list           = txgbe_dev_set_mc_addr_list,
4513         .rxq_info_get               = txgbe_rxq_info_get,
4514         .txq_info_get               = txgbe_txq_info_get,
4515         .timesync_enable            = txgbe_timesync_enable,
4516         .timesync_disable           = txgbe_timesync_disable,
4517         .timesync_read_rx_timestamp = txgbe_timesync_read_rx_timestamp,
4518         .timesync_read_tx_timestamp = txgbe_timesync_read_tx_timestamp,
4519         .get_reg                    = txgbe_get_regs,
4520         .get_eeprom_length          = txgbe_get_eeprom_length,
4521         .get_eeprom                 = txgbe_get_eeprom,
4522         .set_eeprom                 = txgbe_set_eeprom,
4523         .get_module_info            = txgbe_get_module_info,
4524         .get_module_eeprom          = txgbe_get_module_eeprom,
4525         .get_dcb_info               = txgbe_dev_get_dcb_info,
4526         .timesync_adjust_time       = txgbe_timesync_adjust_time,
4527         .timesync_read_time         = txgbe_timesync_read_time,
4528         .timesync_write_time        = txgbe_timesync_write_time,
4529         .tx_done_cleanup            = txgbe_dev_tx_done_cleanup,
4530 };
4531
4532 RTE_PMD_REGISTER_PCI(net_txgbe, rte_txgbe_pmd);
4533 RTE_PMD_REGISTER_PCI_TABLE(net_txgbe, pci_id_txgbe_map);
4534 RTE_PMD_REGISTER_KMOD_DEP(net_txgbe, "* igb_uio | uio_pci_generic | vfio-pci");
4535
4536 RTE_LOG_REGISTER(txgbe_logtype_init, pmd.net.txgbe.init, NOTICE);
4537 RTE_LOG_REGISTER(txgbe_logtype_driver, pmd.net.txgbe.driver, NOTICE);
4538
4539 #ifdef RTE_LIBRTE_TXGBE_DEBUG_RX
4540         RTE_LOG_REGISTER(txgbe_logtype_rx, pmd.net.txgbe.rx, DEBUG);
4541 #endif
4542 #ifdef RTE_LIBRTE_TXGBE_DEBUG_TX
4543         RTE_LOG_REGISTER(txgbe_logtype_tx, pmd.net.txgbe.tx, DEBUG);
4544 #endif
4545
4546 #ifdef RTE_LIBRTE_TXGBE_DEBUG_TX_FREE
4547         RTE_LOG_REGISTER(txgbe_logtype_tx_free, pmd.net.txgbe.tx_free, DEBUG);
4548 #endif