2afc97c163f4d7ec6de035ddccfc5f601ebaadb7
[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
25 static int  txgbe_dev_set_link_up(struct rte_eth_dev *dev);
26 static int  txgbe_dev_set_link_down(struct rte_eth_dev *dev);
27 static int txgbe_dev_close(struct rte_eth_dev *dev);
28 static int txgbe_dev_link_update(struct rte_eth_dev *dev,
29                                 int wait_to_complete);
30 static int txgbe_dev_stats_reset(struct rte_eth_dev *dev);
31 static void txgbe_vlan_hw_strip_enable(struct rte_eth_dev *dev, uint16_t queue);
32 static void txgbe_vlan_hw_strip_disable(struct rte_eth_dev *dev,
33                                         uint16_t queue);
34
35 static void txgbe_dev_link_status_print(struct rte_eth_dev *dev);
36 static int txgbe_dev_lsc_interrupt_setup(struct rte_eth_dev *dev, uint8_t on);
37 static int txgbe_dev_macsec_interrupt_setup(struct rte_eth_dev *dev);
38 static int txgbe_dev_rxq_interrupt_setup(struct rte_eth_dev *dev);
39 static int txgbe_dev_interrupt_get_status(struct rte_eth_dev *dev);
40 static int txgbe_dev_interrupt_action(struct rte_eth_dev *dev,
41                                       struct rte_intr_handle *handle);
42 static void txgbe_dev_interrupt_handler(void *param);
43 static void txgbe_dev_interrupt_delayed_handler(void *param);
44 static void txgbe_configure_msix(struct rte_eth_dev *dev);
45
46 #define TXGBE_SET_HWSTRIP(h, q) do {\
47                 uint32_t idx = (q) / (sizeof((h)->bitmap[0]) * NBBY); \
48                 uint32_t bit = (q) % (sizeof((h)->bitmap[0]) * NBBY); \
49                 (h)->bitmap[idx] |= 1 << bit;\
50         } while (0)
51
52 #define TXGBE_CLEAR_HWSTRIP(h, q) do {\
53                 uint32_t idx = (q) / (sizeof((h)->bitmap[0]) * NBBY); \
54                 uint32_t bit = (q) % (sizeof((h)->bitmap[0]) * NBBY); \
55                 (h)->bitmap[idx] &= ~(1 << bit);\
56         } while (0)
57
58 #define TXGBE_GET_HWSTRIP(h, q, r) do {\
59                 uint32_t idx = (q) / (sizeof((h)->bitmap[0]) * NBBY); \
60                 uint32_t bit = (q) % (sizeof((h)->bitmap[0]) * NBBY); \
61                 (r) = (h)->bitmap[idx] >> bit & 1;\
62         } while (0)
63
64 /*
65  * The set of PCI devices this driver supports
66  */
67 static const struct rte_pci_id pci_id_txgbe_map[] = {
68         { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, TXGBE_DEV_ID_RAPTOR_SFP) },
69         { RTE_PCI_DEVICE(PCI_VENDOR_ID_WANGXUN, TXGBE_DEV_ID_WX1820_SFP) },
70         { .vendor_id = 0, /* sentinel */ },
71 };
72
73 static const struct rte_eth_desc_lim rx_desc_lim = {
74         .nb_max = TXGBE_RING_DESC_MAX,
75         .nb_min = TXGBE_RING_DESC_MIN,
76         .nb_align = TXGBE_RXD_ALIGN,
77 };
78
79 static const struct rte_eth_desc_lim tx_desc_lim = {
80         .nb_max = TXGBE_RING_DESC_MAX,
81         .nb_min = TXGBE_RING_DESC_MIN,
82         .nb_align = TXGBE_TXD_ALIGN,
83         .nb_seg_max = TXGBE_TX_MAX_SEG,
84         .nb_mtu_seg_max = TXGBE_TX_MAX_SEG,
85 };
86
87 static const struct eth_dev_ops txgbe_eth_dev_ops;
88
89 #define HW_XSTAT(m) {#m, offsetof(struct txgbe_hw_stats, m)}
90 #define HW_XSTAT_NAME(m, n) {n, offsetof(struct txgbe_hw_stats, m)}
91 static const struct rte_txgbe_xstats_name_off rte_txgbe_stats_strings[] = {
92         /* MNG RxTx */
93         HW_XSTAT(mng_bmc2host_packets),
94         HW_XSTAT(mng_host2bmc_packets),
95         /* Basic RxTx */
96         HW_XSTAT(rx_packets),
97         HW_XSTAT(tx_packets),
98         HW_XSTAT(rx_bytes),
99         HW_XSTAT(tx_bytes),
100         HW_XSTAT(rx_total_bytes),
101         HW_XSTAT(rx_total_packets),
102         HW_XSTAT(tx_total_packets),
103         HW_XSTAT(rx_total_missed_packets),
104         HW_XSTAT(rx_broadcast_packets),
105         HW_XSTAT(rx_multicast_packets),
106         HW_XSTAT(rx_management_packets),
107         HW_XSTAT(tx_management_packets),
108         HW_XSTAT(rx_management_dropped),
109
110         /* Basic Error */
111         HW_XSTAT(rx_crc_errors),
112         HW_XSTAT(rx_illegal_byte_errors),
113         HW_XSTAT(rx_error_bytes),
114         HW_XSTAT(rx_mac_short_packet_dropped),
115         HW_XSTAT(rx_length_errors),
116         HW_XSTAT(rx_undersize_errors),
117         HW_XSTAT(rx_fragment_errors),
118         HW_XSTAT(rx_oversize_errors),
119         HW_XSTAT(rx_jabber_errors),
120         HW_XSTAT(rx_l3_l4_xsum_error),
121         HW_XSTAT(mac_local_errors),
122         HW_XSTAT(mac_remote_errors),
123
124         /* Flow Director */
125         HW_XSTAT(flow_director_added_filters),
126         HW_XSTAT(flow_director_removed_filters),
127         HW_XSTAT(flow_director_filter_add_errors),
128         HW_XSTAT(flow_director_filter_remove_errors),
129         HW_XSTAT(flow_director_matched_filters),
130         HW_XSTAT(flow_director_missed_filters),
131
132         /* FCoE */
133         HW_XSTAT(rx_fcoe_crc_errors),
134         HW_XSTAT(rx_fcoe_mbuf_allocation_errors),
135         HW_XSTAT(rx_fcoe_dropped),
136         HW_XSTAT(rx_fcoe_packets),
137         HW_XSTAT(tx_fcoe_packets),
138         HW_XSTAT(rx_fcoe_bytes),
139         HW_XSTAT(tx_fcoe_bytes),
140         HW_XSTAT(rx_fcoe_no_ddp),
141         HW_XSTAT(rx_fcoe_no_ddp_ext_buff),
142
143         /* MACSEC */
144         HW_XSTAT(tx_macsec_pkts_untagged),
145         HW_XSTAT(tx_macsec_pkts_encrypted),
146         HW_XSTAT(tx_macsec_pkts_protected),
147         HW_XSTAT(tx_macsec_octets_encrypted),
148         HW_XSTAT(tx_macsec_octets_protected),
149         HW_XSTAT(rx_macsec_pkts_untagged),
150         HW_XSTAT(rx_macsec_pkts_badtag),
151         HW_XSTAT(rx_macsec_pkts_nosci),
152         HW_XSTAT(rx_macsec_pkts_unknownsci),
153         HW_XSTAT(rx_macsec_octets_decrypted),
154         HW_XSTAT(rx_macsec_octets_validated),
155         HW_XSTAT(rx_macsec_sc_pkts_unchecked),
156         HW_XSTAT(rx_macsec_sc_pkts_delayed),
157         HW_XSTAT(rx_macsec_sc_pkts_late),
158         HW_XSTAT(rx_macsec_sa_pkts_ok),
159         HW_XSTAT(rx_macsec_sa_pkts_invalid),
160         HW_XSTAT(rx_macsec_sa_pkts_notvalid),
161         HW_XSTAT(rx_macsec_sa_pkts_unusedsa),
162         HW_XSTAT(rx_macsec_sa_pkts_notusingsa),
163
164         /* MAC RxTx */
165         HW_XSTAT(rx_size_64_packets),
166         HW_XSTAT(rx_size_65_to_127_packets),
167         HW_XSTAT(rx_size_128_to_255_packets),
168         HW_XSTAT(rx_size_256_to_511_packets),
169         HW_XSTAT(rx_size_512_to_1023_packets),
170         HW_XSTAT(rx_size_1024_to_max_packets),
171         HW_XSTAT(tx_size_64_packets),
172         HW_XSTAT(tx_size_65_to_127_packets),
173         HW_XSTAT(tx_size_128_to_255_packets),
174         HW_XSTAT(tx_size_256_to_511_packets),
175         HW_XSTAT(tx_size_512_to_1023_packets),
176         HW_XSTAT(tx_size_1024_to_max_packets),
177
178         /* Flow Control */
179         HW_XSTAT(tx_xon_packets),
180         HW_XSTAT(rx_xon_packets),
181         HW_XSTAT(tx_xoff_packets),
182         HW_XSTAT(rx_xoff_packets),
183
184         HW_XSTAT_NAME(tx_xon_packets, "tx_flow_control_xon_packets"),
185         HW_XSTAT_NAME(rx_xon_packets, "rx_flow_control_xon_packets"),
186         HW_XSTAT_NAME(tx_xoff_packets, "tx_flow_control_xoff_packets"),
187         HW_XSTAT_NAME(rx_xoff_packets, "rx_flow_control_xoff_packets"),
188 };
189
190 #define TXGBE_NB_HW_STATS (sizeof(rte_txgbe_stats_strings) / \
191                            sizeof(rte_txgbe_stats_strings[0]))
192
193 /* Per-priority statistics */
194 #define UP_XSTAT(m) {#m, offsetof(struct txgbe_hw_stats, up[0].m)}
195 static const struct rte_txgbe_xstats_name_off rte_txgbe_up_strings[] = {
196         UP_XSTAT(rx_up_packets),
197         UP_XSTAT(tx_up_packets),
198         UP_XSTAT(rx_up_bytes),
199         UP_XSTAT(tx_up_bytes),
200         UP_XSTAT(rx_up_drop_packets),
201
202         UP_XSTAT(tx_up_xon_packets),
203         UP_XSTAT(rx_up_xon_packets),
204         UP_XSTAT(tx_up_xoff_packets),
205         UP_XSTAT(rx_up_xoff_packets),
206         UP_XSTAT(rx_up_dropped),
207         UP_XSTAT(rx_up_mbuf_alloc_errors),
208         UP_XSTAT(tx_up_xon2off_packets),
209 };
210
211 #define TXGBE_NB_UP_STATS (sizeof(rte_txgbe_up_strings) / \
212                            sizeof(rte_txgbe_up_strings[0]))
213
214 /* Per-queue statistics */
215 #define QP_XSTAT(m) {#m, offsetof(struct txgbe_hw_stats, qp[0].m)}
216 static const struct rte_txgbe_xstats_name_off rte_txgbe_qp_strings[] = {
217         QP_XSTAT(rx_qp_packets),
218         QP_XSTAT(tx_qp_packets),
219         QP_XSTAT(rx_qp_bytes),
220         QP_XSTAT(tx_qp_bytes),
221         QP_XSTAT(rx_qp_mc_packets),
222 };
223
224 #define TXGBE_NB_QP_STATS (sizeof(rte_txgbe_qp_strings) / \
225                            sizeof(rte_txgbe_qp_strings[0]))
226
227 static inline int
228 txgbe_is_sfp(struct txgbe_hw *hw)
229 {
230         switch (hw->phy.type) {
231         case txgbe_phy_sfp_avago:
232         case txgbe_phy_sfp_ftl:
233         case txgbe_phy_sfp_intel:
234         case txgbe_phy_sfp_unknown:
235         case txgbe_phy_sfp_tyco_passive:
236         case txgbe_phy_sfp_unknown_passive:
237                 return 1;
238         default:
239                 return 0;
240         }
241 }
242
243 static inline int32_t
244 txgbe_pf_reset_hw(struct txgbe_hw *hw)
245 {
246         uint32_t ctrl_ext;
247         int32_t status;
248
249         status = hw->mac.reset_hw(hw);
250
251         ctrl_ext = rd32(hw, TXGBE_PORTCTL);
252         /* Set PF Reset Done bit so PF/VF Mail Ops can work */
253         ctrl_ext |= TXGBE_PORTCTL_RSTDONE;
254         wr32(hw, TXGBE_PORTCTL, ctrl_ext);
255         txgbe_flush(hw);
256
257         if (status == TXGBE_ERR_SFP_NOT_PRESENT)
258                 status = 0;
259         return status;
260 }
261
262 static inline void
263 txgbe_enable_intr(struct rte_eth_dev *dev)
264 {
265         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
266         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
267
268         wr32(hw, TXGBE_IENMISC, intr->mask_misc);
269         wr32(hw, TXGBE_IMC(0), TXGBE_IMC_MASK);
270         wr32(hw, TXGBE_IMC(1), TXGBE_IMC_MASK);
271         txgbe_flush(hw);
272 }
273
274 static void
275 txgbe_disable_intr(struct txgbe_hw *hw)
276 {
277         PMD_INIT_FUNC_TRACE();
278
279         wr32(hw, TXGBE_IENMISC, ~BIT_MASK32);
280         wr32(hw, TXGBE_IMS(0), TXGBE_IMC_MASK);
281         wr32(hw, TXGBE_IMS(1), TXGBE_IMC_MASK);
282         txgbe_flush(hw);
283 }
284
285 static int
286 txgbe_dev_queue_stats_mapping_set(struct rte_eth_dev *eth_dev,
287                                   uint16_t queue_id,
288                                   uint8_t stat_idx,
289                                   uint8_t is_rx)
290 {
291         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
292         struct txgbe_stat_mappings *stat_mappings =
293                 TXGBE_DEV_STAT_MAPPINGS(eth_dev);
294         uint32_t qsmr_mask = 0;
295         uint32_t clearing_mask = QMAP_FIELD_RESERVED_BITS_MASK;
296         uint32_t q_map;
297         uint8_t n, offset;
298
299         if (hw->mac.type != txgbe_mac_raptor)
300                 return -ENOSYS;
301
302         if (stat_idx & !QMAP_FIELD_RESERVED_BITS_MASK)
303                 return -EIO;
304
305         PMD_INIT_LOG(DEBUG, "Setting port %d, %s queue_id %d to stat index %d",
306                      (int)(eth_dev->data->port_id), is_rx ? "RX" : "TX",
307                      queue_id, stat_idx);
308
309         n = (uint8_t)(queue_id / NB_QMAP_FIELDS_PER_QSM_REG);
310         if (n >= TXGBE_NB_STAT_MAPPING) {
311                 PMD_INIT_LOG(ERR, "Nb of stat mapping registers exceeded");
312                 return -EIO;
313         }
314         offset = (uint8_t)(queue_id % NB_QMAP_FIELDS_PER_QSM_REG);
315
316         /* Now clear any previous stat_idx set */
317         clearing_mask <<= (QSM_REG_NB_BITS_PER_QMAP_FIELD * offset);
318         if (!is_rx)
319                 stat_mappings->tqsm[n] &= ~clearing_mask;
320         else
321                 stat_mappings->rqsm[n] &= ~clearing_mask;
322
323         q_map = (uint32_t)stat_idx;
324         q_map &= QMAP_FIELD_RESERVED_BITS_MASK;
325         qsmr_mask = q_map << (QSM_REG_NB_BITS_PER_QMAP_FIELD * offset);
326         if (!is_rx)
327                 stat_mappings->tqsm[n] |= qsmr_mask;
328         else
329                 stat_mappings->rqsm[n] |= qsmr_mask;
330
331         PMD_INIT_LOG(DEBUG, "Set port %d, %s queue_id %d to stat index %d",
332                      (int)(eth_dev->data->port_id), is_rx ? "RX" : "TX",
333                      queue_id, stat_idx);
334         PMD_INIT_LOG(DEBUG, "%s[%d] = 0x%08x", is_rx ? "RQSMR" : "TQSM", n,
335                      is_rx ? stat_mappings->rqsm[n] : stat_mappings->tqsm[n]);
336         return 0;
337 }
338
339 /*
340  * Ensure that all locks are released before first NVM or PHY access
341  */
342 static void
343 txgbe_swfw_lock_reset(struct txgbe_hw *hw)
344 {
345         uint16_t mask;
346
347         /*
348          * These ones are more tricky since they are common to all ports; but
349          * swfw_sync retries last long enough (1s) to be almost sure that if
350          * lock can not be taken it is due to an improper lock of the
351          * semaphore.
352          */
353         mask = TXGBE_MNGSEM_SWPHY |
354                TXGBE_MNGSEM_SWMBX |
355                TXGBE_MNGSEM_SWFLASH;
356         if (hw->mac.acquire_swfw_sync(hw, mask) < 0)
357                 PMD_DRV_LOG(DEBUG, "SWFW common locks released");
358
359         hw->mac.release_swfw_sync(hw, mask);
360 }
361
362 static int
363 eth_txgbe_dev_init(struct rte_eth_dev *eth_dev, void *init_params __rte_unused)
364 {
365         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
366         struct txgbe_hw *hw = TXGBE_DEV_HW(eth_dev);
367         struct txgbe_vfta *shadow_vfta = TXGBE_DEV_VFTA(eth_dev);
368         struct txgbe_hwstrip *hwstrip = TXGBE_DEV_HWSTRIP(eth_dev);
369         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
370         const struct rte_memzone *mz;
371         uint32_t ctrl_ext;
372         uint16_t csum;
373         int err;
374
375         PMD_INIT_FUNC_TRACE();
376
377         eth_dev->dev_ops = &txgbe_eth_dev_ops;
378         eth_dev->rx_pkt_burst = &txgbe_recv_pkts;
379         eth_dev->tx_pkt_burst = &txgbe_xmit_pkts;
380         eth_dev->tx_pkt_prepare = &txgbe_prep_pkts;
381
382         /*
383          * For secondary processes, we don't initialise any further as primary
384          * has already done this work. Only check we don't need a different
385          * RX and TX function.
386          */
387         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
388                 struct txgbe_tx_queue *txq;
389                 /* TX queue function in primary, set by last queue initialized
390                  * Tx queue may not initialized by primary process
391                  */
392                 if (eth_dev->data->tx_queues) {
393                         uint16_t nb_tx_queues = eth_dev->data->nb_tx_queues;
394                         txq = eth_dev->data->tx_queues[nb_tx_queues - 1];
395                         txgbe_set_tx_function(eth_dev, txq);
396                 } else {
397                         /* Use default TX function if we get here */
398                         PMD_INIT_LOG(NOTICE, "No TX queues configured yet. "
399                                      "Using default TX function.");
400                 }
401
402                 txgbe_set_rx_function(eth_dev);
403
404                 return 0;
405         }
406
407         rte_eth_copy_pci_info(eth_dev, pci_dev);
408
409         /* Vendor and Device ID need to be set before init of shared code */
410         hw->device_id = pci_dev->id.device_id;
411         hw->vendor_id = pci_dev->id.vendor_id;
412         hw->hw_addr = (void *)pci_dev->mem_resource[0].addr;
413         hw->allow_unsupported_sfp = 1;
414
415         /* Reserve memory for interrupt status block */
416         mz = rte_eth_dma_zone_reserve(eth_dev, "txgbe_driver", -1,
417                 16, TXGBE_ALIGN, SOCKET_ID_ANY);
418         if (mz == NULL)
419                 return -ENOMEM;
420
421         hw->isb_dma = TMZ_PADDR(mz);
422         hw->isb_mem = TMZ_VADDR(mz);
423
424         /* Initialize the shared code (base driver) */
425         err = txgbe_init_shared_code(hw);
426         if (err != 0) {
427                 PMD_INIT_LOG(ERR, "Shared code init failed: %d", err);
428                 return -EIO;
429         }
430
431         /* Unlock any pending hardware semaphore */
432         txgbe_swfw_lock_reset(hw);
433
434         err = hw->rom.init_params(hw);
435         if (err != 0) {
436                 PMD_INIT_LOG(ERR, "The EEPROM init failed: %d", err);
437                 return -EIO;
438         }
439
440         /* Make sure we have a good EEPROM before we read from it */
441         err = hw->rom.validate_checksum(hw, &csum);
442         if (err != 0) {
443                 PMD_INIT_LOG(ERR, "The EEPROM checksum is not valid: %d", err);
444                 return -EIO;
445         }
446
447         err = hw->mac.init_hw(hw);
448
449         /*
450          * Devices with copper phys will fail to initialise if txgbe_init_hw()
451          * is called too soon after the kernel driver unbinding/binding occurs.
452          * The failure occurs in txgbe_identify_phy() for all devices,
453          * but for non-copper devies, txgbe_identify_sfp_module() is
454          * also called. See txgbe_identify_phy(). The reason for the
455          * failure is not known, and only occuts when virtualisation features
456          * are disabled in the bios. A delay of 200ms  was found to be enough by
457          * trial-and-error, and is doubled to be safe.
458          */
459         if (err && hw->phy.media_type == txgbe_media_type_copper) {
460                 rte_delay_ms(200);
461                 err = hw->mac.init_hw(hw);
462         }
463
464         if (err == TXGBE_ERR_SFP_NOT_PRESENT)
465                 err = 0;
466
467         if (err == TXGBE_ERR_EEPROM_VERSION) {
468                 PMD_INIT_LOG(ERR, "This device is a pre-production adapter/"
469                              "LOM.  Please be aware there may be issues associated "
470                              "with your hardware.");
471                 PMD_INIT_LOG(ERR, "If you are experiencing problems "
472                              "please contact your hardware representative "
473                              "who provided you with this hardware.");
474         } else if (err == TXGBE_ERR_SFP_NOT_SUPPORTED) {
475                 PMD_INIT_LOG(ERR, "Unsupported SFP+ Module");
476         }
477         if (err) {
478                 PMD_INIT_LOG(ERR, "Hardware Initialization Failure: %d", err);
479                 return -EIO;
480         }
481
482         /* Reset the hw statistics */
483         txgbe_dev_stats_reset(eth_dev);
484
485         /* disable interrupt */
486         txgbe_disable_intr(hw);
487
488         /* Allocate memory for storing MAC addresses */
489         eth_dev->data->mac_addrs = rte_zmalloc("txgbe", RTE_ETHER_ADDR_LEN *
490                                                hw->mac.num_rar_entries, 0);
491         if (eth_dev->data->mac_addrs == NULL) {
492                 PMD_INIT_LOG(ERR,
493                              "Failed to allocate %u bytes needed to store "
494                              "MAC addresses",
495                              RTE_ETHER_ADDR_LEN * hw->mac.num_rar_entries);
496                 return -ENOMEM;
497         }
498
499         /* Copy the permanent MAC address */
500         rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.perm_addr,
501                         &eth_dev->data->mac_addrs[0]);
502
503         /* Allocate memory for storing hash filter MAC addresses */
504         eth_dev->data->hash_mac_addrs = rte_zmalloc("txgbe",
505                         RTE_ETHER_ADDR_LEN * TXGBE_VMDQ_NUM_UC_MAC, 0);
506         if (eth_dev->data->hash_mac_addrs == NULL) {
507                 PMD_INIT_LOG(ERR,
508                              "Failed to allocate %d bytes needed to store MAC addresses",
509                              RTE_ETHER_ADDR_LEN * TXGBE_VMDQ_NUM_UC_MAC);
510                 return -ENOMEM;
511         }
512
513         /* initialize the vfta */
514         memset(shadow_vfta, 0, sizeof(*shadow_vfta));
515
516         /* initialize the hw strip bitmap*/
517         memset(hwstrip, 0, sizeof(*hwstrip));
518
519         /* initialize PF if max_vfs not zero */
520         txgbe_pf_host_init(eth_dev);
521
522         ctrl_ext = rd32(hw, TXGBE_PORTCTL);
523         /* let hardware know driver is loaded */
524         ctrl_ext |= TXGBE_PORTCTL_DRVLOAD;
525         /* Set PF Reset Done bit so PF/VF Mail Ops can work */
526         ctrl_ext |= TXGBE_PORTCTL_RSTDONE;
527         wr32(hw, TXGBE_PORTCTL, ctrl_ext);
528         txgbe_flush(hw);
529
530         if (txgbe_is_sfp(hw) && hw->phy.sfp_type != txgbe_sfp_type_not_present)
531                 PMD_INIT_LOG(DEBUG, "MAC: %d, PHY: %d, SFP+: %d",
532                              (int)hw->mac.type, (int)hw->phy.type,
533                              (int)hw->phy.sfp_type);
534         else
535                 PMD_INIT_LOG(DEBUG, "MAC: %d, PHY: %d",
536                              (int)hw->mac.type, (int)hw->phy.type);
537
538         PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
539                      eth_dev->data->port_id, pci_dev->id.vendor_id,
540                      pci_dev->id.device_id);
541
542         rte_intr_callback_register(intr_handle,
543                                    txgbe_dev_interrupt_handler, eth_dev);
544
545         /* enable uio/vfio intr/eventfd mapping */
546         rte_intr_enable(intr_handle);
547
548         /* enable support intr */
549         txgbe_enable_intr(eth_dev);
550
551         return 0;
552 }
553
554 static int
555 eth_txgbe_dev_uninit(struct rte_eth_dev *eth_dev)
556 {
557         PMD_INIT_FUNC_TRACE();
558
559         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
560                 return 0;
561
562         txgbe_dev_close(eth_dev);
563
564         return 0;
565 }
566
567 static int
568 eth_txgbe_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
569                 struct rte_pci_device *pci_dev)
570 {
571         struct rte_eth_dev *pf_ethdev;
572         struct rte_eth_devargs eth_da;
573         int retval;
574
575         if (pci_dev->device.devargs) {
576                 retval = rte_eth_devargs_parse(pci_dev->device.devargs->args,
577                                 &eth_da);
578                 if (retval)
579                         return retval;
580         } else {
581                 memset(&eth_da, 0, sizeof(eth_da));
582         }
583
584         retval = rte_eth_dev_create(&pci_dev->device, pci_dev->device.name,
585                         sizeof(struct txgbe_adapter),
586                         eth_dev_pci_specific_init, pci_dev,
587                         eth_txgbe_dev_init, NULL);
588
589         if (retval || eth_da.nb_representor_ports < 1)
590                 return retval;
591
592         pf_ethdev = rte_eth_dev_allocated(pci_dev->device.name);
593         if (pf_ethdev == NULL)
594                 return -ENODEV;
595
596         return 0;
597 }
598
599 static int eth_txgbe_pci_remove(struct rte_pci_device *pci_dev)
600 {
601         struct rte_eth_dev *ethdev;
602
603         ethdev = rte_eth_dev_allocated(pci_dev->device.name);
604         if (!ethdev)
605                 return -ENODEV;
606
607         return rte_eth_dev_destroy(ethdev, eth_txgbe_dev_uninit);
608 }
609
610 static struct rte_pci_driver rte_txgbe_pmd = {
611         .id_table = pci_id_txgbe_map,
612         .drv_flags = RTE_PCI_DRV_NEED_MAPPING |
613                      RTE_PCI_DRV_INTR_LSC,
614         .probe = eth_txgbe_pci_probe,
615         .remove = eth_txgbe_pci_remove,
616 };
617
618 static int
619 txgbe_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
620 {
621         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
622         struct txgbe_vfta *shadow_vfta = TXGBE_DEV_VFTA(dev);
623         uint32_t vfta;
624         uint32_t vid_idx;
625         uint32_t vid_bit;
626
627         vid_idx = (uint32_t)((vlan_id >> 5) & 0x7F);
628         vid_bit = (uint32_t)(1 << (vlan_id & 0x1F));
629         vfta = rd32(hw, TXGBE_VLANTBL(vid_idx));
630         if (on)
631                 vfta |= vid_bit;
632         else
633                 vfta &= ~vid_bit;
634         wr32(hw, TXGBE_VLANTBL(vid_idx), vfta);
635
636         /* update local VFTA copy */
637         shadow_vfta->vfta[vid_idx] = vfta;
638
639         return 0;
640 }
641
642 static void
643 txgbe_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
644 {
645         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
646         struct txgbe_rx_queue *rxq;
647         bool restart;
648         uint32_t rxcfg, rxbal, rxbah;
649
650         if (on)
651                 txgbe_vlan_hw_strip_enable(dev, queue);
652         else
653                 txgbe_vlan_hw_strip_disable(dev, queue);
654
655         rxq = dev->data->rx_queues[queue];
656         rxbal = rd32(hw, TXGBE_RXBAL(rxq->reg_idx));
657         rxbah = rd32(hw, TXGBE_RXBAH(rxq->reg_idx));
658         rxcfg = rd32(hw, TXGBE_RXCFG(rxq->reg_idx));
659         if (rxq->offloads & DEV_RX_OFFLOAD_VLAN_STRIP) {
660                 restart = (rxcfg & TXGBE_RXCFG_ENA) &&
661                         !(rxcfg & TXGBE_RXCFG_VLAN);
662                 rxcfg |= TXGBE_RXCFG_VLAN;
663         } else {
664                 restart = (rxcfg & TXGBE_RXCFG_ENA) &&
665                         (rxcfg & TXGBE_RXCFG_VLAN);
666                 rxcfg &= ~TXGBE_RXCFG_VLAN;
667         }
668         rxcfg &= ~TXGBE_RXCFG_ENA;
669
670         if (restart) {
671                 /* set vlan strip for ring */
672                 txgbe_dev_rx_queue_stop(dev, queue);
673                 wr32(hw, TXGBE_RXBAL(rxq->reg_idx), rxbal);
674                 wr32(hw, TXGBE_RXBAH(rxq->reg_idx), rxbah);
675                 wr32(hw, TXGBE_RXCFG(rxq->reg_idx), rxcfg);
676                 txgbe_dev_rx_queue_start(dev, queue);
677         }
678 }
679
680 static int
681 txgbe_vlan_tpid_set(struct rte_eth_dev *dev,
682                     enum rte_vlan_type vlan_type,
683                     uint16_t tpid)
684 {
685         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
686         int ret = 0;
687         uint32_t portctrl, vlan_ext, qinq;
688
689         portctrl = rd32(hw, TXGBE_PORTCTL);
690
691         vlan_ext = (portctrl & TXGBE_PORTCTL_VLANEXT);
692         qinq = vlan_ext && (portctrl & TXGBE_PORTCTL_QINQ);
693         switch (vlan_type) {
694         case ETH_VLAN_TYPE_INNER:
695                 if (vlan_ext) {
696                         wr32m(hw, TXGBE_VLANCTL,
697                                 TXGBE_VLANCTL_TPID_MASK,
698                                 TXGBE_VLANCTL_TPID(tpid));
699                         wr32m(hw, TXGBE_DMATXCTRL,
700                                 TXGBE_DMATXCTRL_TPID_MASK,
701                                 TXGBE_DMATXCTRL_TPID(tpid));
702                 } else {
703                         ret = -ENOTSUP;
704                         PMD_DRV_LOG(ERR, "Inner type is not supported"
705                                     " by single VLAN");
706                 }
707
708                 if (qinq) {
709                         wr32m(hw, TXGBE_TAGTPID(0),
710                                 TXGBE_TAGTPID_LSB_MASK,
711                                 TXGBE_TAGTPID_LSB(tpid));
712                 }
713                 break;
714         case ETH_VLAN_TYPE_OUTER:
715                 if (vlan_ext) {
716                         /* Only the high 16-bits is valid */
717                         wr32m(hw, TXGBE_EXTAG,
718                                 TXGBE_EXTAG_VLAN_MASK,
719                                 TXGBE_EXTAG_VLAN(tpid));
720                 } else {
721                         wr32m(hw, TXGBE_VLANCTL,
722                                 TXGBE_VLANCTL_TPID_MASK,
723                                 TXGBE_VLANCTL_TPID(tpid));
724                         wr32m(hw, TXGBE_DMATXCTRL,
725                                 TXGBE_DMATXCTRL_TPID_MASK,
726                                 TXGBE_DMATXCTRL_TPID(tpid));
727                 }
728
729                 if (qinq) {
730                         wr32m(hw, TXGBE_TAGTPID(0),
731                                 TXGBE_TAGTPID_MSB_MASK,
732                                 TXGBE_TAGTPID_MSB(tpid));
733                 }
734                 break;
735         default:
736                 PMD_DRV_LOG(ERR, "Unsupported VLAN type %d", vlan_type);
737                 return -EINVAL;
738         }
739
740         return ret;
741 }
742
743 void
744 txgbe_vlan_hw_filter_disable(struct rte_eth_dev *dev)
745 {
746         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
747         uint32_t vlnctrl;
748
749         PMD_INIT_FUNC_TRACE();
750
751         /* Filter Table Disable */
752         vlnctrl = rd32(hw, TXGBE_VLANCTL);
753         vlnctrl &= ~TXGBE_VLANCTL_VFE;
754         wr32(hw, TXGBE_VLANCTL, vlnctrl);
755 }
756
757 void
758 txgbe_vlan_hw_filter_enable(struct rte_eth_dev *dev)
759 {
760         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
761         struct txgbe_vfta *shadow_vfta = TXGBE_DEV_VFTA(dev);
762         uint32_t vlnctrl;
763         uint16_t i;
764
765         PMD_INIT_FUNC_TRACE();
766
767         /* Filter Table Enable */
768         vlnctrl = rd32(hw, TXGBE_VLANCTL);
769         vlnctrl &= ~TXGBE_VLANCTL_CFIENA;
770         vlnctrl |= TXGBE_VLANCTL_VFE;
771         wr32(hw, TXGBE_VLANCTL, vlnctrl);
772
773         /* write whatever is in local vfta copy */
774         for (i = 0; i < TXGBE_VFTA_SIZE; i++)
775                 wr32(hw, TXGBE_VLANTBL(i), shadow_vfta->vfta[i]);
776 }
777
778 void
779 txgbe_vlan_hw_strip_bitmap_set(struct rte_eth_dev *dev, uint16_t queue, bool on)
780 {
781         struct txgbe_hwstrip *hwstrip = TXGBE_DEV_HWSTRIP(dev);
782         struct txgbe_rx_queue *rxq;
783
784         if (queue >= TXGBE_MAX_RX_QUEUE_NUM)
785                 return;
786
787         if (on)
788                 TXGBE_SET_HWSTRIP(hwstrip, queue);
789         else
790                 TXGBE_CLEAR_HWSTRIP(hwstrip, queue);
791
792         if (queue >= dev->data->nb_rx_queues)
793                 return;
794
795         rxq = dev->data->rx_queues[queue];
796
797         if (on) {
798                 rxq->vlan_flags = PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
799                 rxq->offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
800         } else {
801                 rxq->vlan_flags = PKT_RX_VLAN;
802                 rxq->offloads &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
803         }
804 }
805
806 static void
807 txgbe_vlan_hw_strip_disable(struct rte_eth_dev *dev, uint16_t queue)
808 {
809         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
810         uint32_t ctrl;
811
812         PMD_INIT_FUNC_TRACE();
813
814         ctrl = rd32(hw, TXGBE_RXCFG(queue));
815         ctrl &= ~TXGBE_RXCFG_VLAN;
816         wr32(hw, TXGBE_RXCFG(queue), ctrl);
817
818         /* record those setting for HW strip per queue */
819         txgbe_vlan_hw_strip_bitmap_set(dev, queue, 0);
820 }
821
822 static void
823 txgbe_vlan_hw_strip_enable(struct rte_eth_dev *dev, uint16_t queue)
824 {
825         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
826         uint32_t ctrl;
827
828         PMD_INIT_FUNC_TRACE();
829
830         ctrl = rd32(hw, TXGBE_RXCFG(queue));
831         ctrl |= TXGBE_RXCFG_VLAN;
832         wr32(hw, TXGBE_RXCFG(queue), ctrl);
833
834         /* record those setting for HW strip per queue */
835         txgbe_vlan_hw_strip_bitmap_set(dev, queue, 1);
836 }
837
838 static void
839 txgbe_vlan_hw_extend_disable(struct rte_eth_dev *dev)
840 {
841         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
842         uint32_t ctrl;
843
844         PMD_INIT_FUNC_TRACE();
845
846         ctrl = rd32(hw, TXGBE_PORTCTL);
847         ctrl &= ~TXGBE_PORTCTL_VLANEXT;
848         ctrl &= ~TXGBE_PORTCTL_QINQ;
849         wr32(hw, TXGBE_PORTCTL, ctrl);
850 }
851
852 static void
853 txgbe_vlan_hw_extend_enable(struct rte_eth_dev *dev)
854 {
855         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
856         struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
857         struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
858         uint32_t ctrl;
859
860         PMD_INIT_FUNC_TRACE();
861
862         ctrl  = rd32(hw, TXGBE_PORTCTL);
863         ctrl |= TXGBE_PORTCTL_VLANEXT;
864         if (rxmode->offloads & DEV_RX_OFFLOAD_QINQ_STRIP ||
865             txmode->offloads & DEV_TX_OFFLOAD_QINQ_INSERT)
866                 ctrl |= TXGBE_PORTCTL_QINQ;
867         wr32(hw, TXGBE_PORTCTL, ctrl);
868 }
869
870 void
871 txgbe_vlan_hw_strip_config(struct rte_eth_dev *dev)
872 {
873         struct txgbe_rx_queue *rxq;
874         uint16_t i;
875
876         PMD_INIT_FUNC_TRACE();
877
878         for (i = 0; i < dev->data->nb_rx_queues; i++) {
879                 rxq = dev->data->rx_queues[i];
880
881                 if (rxq->offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
882                         txgbe_vlan_strip_queue_set(dev, i, 1);
883                 else
884                         txgbe_vlan_strip_queue_set(dev, i, 0);
885         }
886 }
887
888 void
889 txgbe_config_vlan_strip_on_all_queues(struct rte_eth_dev *dev, int mask)
890 {
891         uint16_t i;
892         struct rte_eth_rxmode *rxmode;
893         struct txgbe_rx_queue *rxq;
894
895         if (mask & ETH_VLAN_STRIP_MASK) {
896                 rxmode = &dev->data->dev_conf.rxmode;
897                 if (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
898                         for (i = 0; i < dev->data->nb_rx_queues; i++) {
899                                 rxq = dev->data->rx_queues[i];
900                                 rxq->offloads |= DEV_RX_OFFLOAD_VLAN_STRIP;
901                         }
902                 else
903                         for (i = 0; i < dev->data->nb_rx_queues; i++) {
904                                 rxq = dev->data->rx_queues[i];
905                                 rxq->offloads &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
906                         }
907         }
908 }
909
910 static int
911 txgbe_vlan_offload_config(struct rte_eth_dev *dev, int mask)
912 {
913         struct rte_eth_rxmode *rxmode;
914         rxmode = &dev->data->dev_conf.rxmode;
915
916         if (mask & ETH_VLAN_STRIP_MASK)
917                 txgbe_vlan_hw_strip_config(dev);
918
919         if (mask & ETH_VLAN_FILTER_MASK) {
920                 if (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
921                         txgbe_vlan_hw_filter_enable(dev);
922                 else
923                         txgbe_vlan_hw_filter_disable(dev);
924         }
925
926         if (mask & ETH_VLAN_EXTEND_MASK) {
927                 if (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_EXTEND)
928                         txgbe_vlan_hw_extend_enable(dev);
929                 else
930                         txgbe_vlan_hw_extend_disable(dev);
931         }
932
933         return 0;
934 }
935
936 static int
937 txgbe_vlan_offload_set(struct rte_eth_dev *dev, int mask)
938 {
939         txgbe_config_vlan_strip_on_all_queues(dev, mask);
940
941         txgbe_vlan_offload_config(dev, mask);
942
943         return 0;
944 }
945
946 static void
947 txgbe_vmdq_vlan_hw_filter_enable(struct rte_eth_dev *dev)
948 {
949         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
950         /* VLNCTL: enable vlan filtering and allow all vlan tags through */
951         uint32_t vlanctrl = rd32(hw, TXGBE_VLANCTL);
952
953         vlanctrl |= TXGBE_VLANCTL_VFE; /* enable vlan filters */
954         wr32(hw, TXGBE_VLANCTL, vlanctrl);
955 }
956
957 static int
958 txgbe_check_vf_rss_rxq_num(struct rte_eth_dev *dev, uint16_t nb_rx_q)
959 {
960         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
961
962         switch (nb_rx_q) {
963         case 1:
964         case 2:
965                 RTE_ETH_DEV_SRIOV(dev).active = ETH_64_POOLS;
966                 break;
967         case 4:
968                 RTE_ETH_DEV_SRIOV(dev).active = ETH_32_POOLS;
969                 break;
970         default:
971                 return -EINVAL;
972         }
973
974         RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool =
975                 TXGBE_MAX_RX_QUEUE_NUM / RTE_ETH_DEV_SRIOV(dev).active;
976         RTE_ETH_DEV_SRIOV(dev).def_pool_q_idx =
977                 pci_dev->max_vfs * RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
978         return 0;
979 }
980
981 static int
982 txgbe_check_mq_mode(struct rte_eth_dev *dev)
983 {
984         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
985         uint16_t nb_rx_q = dev->data->nb_rx_queues;
986         uint16_t nb_tx_q = dev->data->nb_tx_queues;
987
988         if (RTE_ETH_DEV_SRIOV(dev).active != 0) {
989                 /* check multi-queue mode */
990                 switch (dev_conf->rxmode.mq_mode) {
991                 case ETH_MQ_RX_VMDQ_DCB:
992                         PMD_INIT_LOG(INFO, "ETH_MQ_RX_VMDQ_DCB mode supported in SRIOV");
993                         break;
994                 case ETH_MQ_RX_VMDQ_DCB_RSS:
995                         /* DCB/RSS VMDQ in SRIOV mode, not implement yet */
996                         PMD_INIT_LOG(ERR, "SRIOV active,"
997                                         " unsupported mq_mode rx %d.",
998                                         dev_conf->rxmode.mq_mode);
999                         return -EINVAL;
1000                 case ETH_MQ_RX_RSS:
1001                 case ETH_MQ_RX_VMDQ_RSS:
1002                         dev->data->dev_conf.rxmode.mq_mode = ETH_MQ_RX_VMDQ_RSS;
1003                         if (nb_rx_q <= RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool)
1004                                 if (txgbe_check_vf_rss_rxq_num(dev, nb_rx_q)) {
1005                                         PMD_INIT_LOG(ERR, "SRIOV is active,"
1006                                                 " invalid queue number"
1007                                                 " for VMDQ RSS, allowed"
1008                                                 " value are 1, 2 or 4.");
1009                                         return -EINVAL;
1010                                 }
1011                         break;
1012                 case ETH_MQ_RX_VMDQ_ONLY:
1013                 case ETH_MQ_RX_NONE:
1014                         /* if nothing mq mode configure, use default scheme */
1015                         dev->data->dev_conf.rxmode.mq_mode =
1016                                 ETH_MQ_RX_VMDQ_ONLY;
1017                         break;
1018                 default: /* ETH_MQ_RX_DCB, ETH_MQ_RX_DCB_RSS or ETH_MQ_TX_DCB*/
1019                         /* SRIOV only works in VMDq enable mode */
1020                         PMD_INIT_LOG(ERR, "SRIOV is active,"
1021                                         " wrong mq_mode rx %d.",
1022                                         dev_conf->rxmode.mq_mode);
1023                         return -EINVAL;
1024                 }
1025
1026                 switch (dev_conf->txmode.mq_mode) {
1027                 case ETH_MQ_TX_VMDQ_DCB:
1028                         PMD_INIT_LOG(INFO, "ETH_MQ_TX_VMDQ_DCB mode supported in SRIOV");
1029                         dev->data->dev_conf.txmode.mq_mode = ETH_MQ_TX_VMDQ_DCB;
1030                         break;
1031                 default: /* ETH_MQ_TX_VMDQ_ONLY or ETH_MQ_TX_NONE */
1032                         dev->data->dev_conf.txmode.mq_mode =
1033                                 ETH_MQ_TX_VMDQ_ONLY;
1034                         break;
1035                 }
1036
1037                 /* check valid queue number */
1038                 if ((nb_rx_q > RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool) ||
1039                     (nb_tx_q > RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool)) {
1040                         PMD_INIT_LOG(ERR, "SRIOV is active,"
1041                                         " nb_rx_q=%d nb_tx_q=%d queue number"
1042                                         " must be less than or equal to %d.",
1043                                         nb_rx_q, nb_tx_q,
1044                                         RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool);
1045                         return -EINVAL;
1046                 }
1047         } else {
1048                 if (dev_conf->rxmode.mq_mode == ETH_MQ_RX_VMDQ_DCB_RSS) {
1049                         PMD_INIT_LOG(ERR, "VMDQ+DCB+RSS mq_mode is"
1050                                           " not supported.");
1051                         return -EINVAL;
1052                 }
1053                 /* check configuration for vmdb+dcb mode */
1054                 if (dev_conf->rxmode.mq_mode == ETH_MQ_RX_VMDQ_DCB) {
1055                         const struct rte_eth_vmdq_dcb_conf *conf;
1056
1057                         if (nb_rx_q != TXGBE_VMDQ_DCB_NB_QUEUES) {
1058                                 PMD_INIT_LOG(ERR, "VMDQ+DCB, nb_rx_q != %d.",
1059                                                 TXGBE_VMDQ_DCB_NB_QUEUES);
1060                                 return -EINVAL;
1061                         }
1062                         conf = &dev_conf->rx_adv_conf.vmdq_dcb_conf;
1063                         if (!(conf->nb_queue_pools == ETH_16_POOLS ||
1064                                conf->nb_queue_pools == ETH_32_POOLS)) {
1065                                 PMD_INIT_LOG(ERR, "VMDQ+DCB selected,"
1066                                                 " nb_queue_pools must be %d or %d.",
1067                                                 ETH_16_POOLS, ETH_32_POOLS);
1068                                 return -EINVAL;
1069                         }
1070                 }
1071                 if (dev_conf->txmode.mq_mode == ETH_MQ_TX_VMDQ_DCB) {
1072                         const struct rte_eth_vmdq_dcb_tx_conf *conf;
1073
1074                         if (nb_tx_q != TXGBE_VMDQ_DCB_NB_QUEUES) {
1075                                 PMD_INIT_LOG(ERR, "VMDQ+DCB, nb_tx_q != %d",
1076                                                  TXGBE_VMDQ_DCB_NB_QUEUES);
1077                                 return -EINVAL;
1078                         }
1079                         conf = &dev_conf->tx_adv_conf.vmdq_dcb_tx_conf;
1080                         if (!(conf->nb_queue_pools == ETH_16_POOLS ||
1081                                conf->nb_queue_pools == ETH_32_POOLS)) {
1082                                 PMD_INIT_LOG(ERR, "VMDQ+DCB selected,"
1083                                                 " nb_queue_pools != %d and"
1084                                                 " nb_queue_pools != %d.",
1085                                                 ETH_16_POOLS, ETH_32_POOLS);
1086                                 return -EINVAL;
1087                         }
1088                 }
1089
1090                 /* For DCB mode check our configuration before we go further */
1091                 if (dev_conf->rxmode.mq_mode == ETH_MQ_RX_DCB) {
1092                         const struct rte_eth_dcb_rx_conf *conf;
1093
1094                         conf = &dev_conf->rx_adv_conf.dcb_rx_conf;
1095                         if (!(conf->nb_tcs == ETH_4_TCS ||
1096                                conf->nb_tcs == ETH_8_TCS)) {
1097                                 PMD_INIT_LOG(ERR, "DCB selected, nb_tcs != %d"
1098                                                 " and nb_tcs != %d.",
1099                                                 ETH_4_TCS, ETH_8_TCS);
1100                                 return -EINVAL;
1101                         }
1102                 }
1103
1104                 if (dev_conf->txmode.mq_mode == ETH_MQ_TX_DCB) {
1105                         const struct rte_eth_dcb_tx_conf *conf;
1106
1107                         conf = &dev_conf->tx_adv_conf.dcb_tx_conf;
1108                         if (!(conf->nb_tcs == ETH_4_TCS ||
1109                                conf->nb_tcs == ETH_8_TCS)) {
1110                                 PMD_INIT_LOG(ERR, "DCB selected, nb_tcs != %d"
1111                                                 " and nb_tcs != %d.",
1112                                                 ETH_4_TCS, ETH_8_TCS);
1113                                 return -EINVAL;
1114                         }
1115                 }
1116         }
1117         return 0;
1118 }
1119
1120 static int
1121 txgbe_dev_configure(struct rte_eth_dev *dev)
1122 {
1123         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
1124         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
1125         int ret;
1126
1127         PMD_INIT_FUNC_TRACE();
1128
1129         if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
1130                 dev->data->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
1131
1132         /* multiple queue mode checking */
1133         ret  = txgbe_check_mq_mode(dev);
1134         if (ret != 0) {
1135                 PMD_DRV_LOG(ERR, "txgbe_check_mq_mode fails with %d.",
1136                             ret);
1137                 return ret;
1138         }
1139
1140         /* set flag to update link status after init */
1141         intr->flags |= TXGBE_FLAG_NEED_LINK_UPDATE;
1142
1143         /*
1144          * Initialize to TRUE. If any of Rx queues doesn't meet the bulk
1145          * allocation Rx preconditions we will reset it.
1146          */
1147         adapter->rx_bulk_alloc_allowed = true;
1148
1149         return 0;
1150 }
1151
1152 static void
1153 txgbe_dev_phy_intr_setup(struct rte_eth_dev *dev)
1154 {
1155         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1156         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
1157         uint32_t gpie;
1158
1159         gpie = rd32(hw, TXGBE_GPIOINTEN);
1160         gpie |= TXGBE_GPIOBIT_6;
1161         wr32(hw, TXGBE_GPIOINTEN, gpie);
1162         intr->mask_misc |= TXGBE_ICRMISC_GPIO;
1163 }
1164
1165 int
1166 txgbe_set_vf_rate_limit(struct rte_eth_dev *dev, uint16_t vf,
1167                         uint16_t tx_rate, uint64_t q_msk)
1168 {
1169         struct txgbe_hw *hw;
1170         struct txgbe_vf_info *vfinfo;
1171         struct rte_eth_link link;
1172         uint8_t  nb_q_per_pool;
1173         uint32_t queue_stride;
1174         uint32_t queue_idx, idx = 0, vf_idx;
1175         uint32_t queue_end;
1176         uint16_t total_rate = 0;
1177         struct rte_pci_device *pci_dev;
1178         int ret;
1179
1180         pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1181         ret = rte_eth_link_get_nowait(dev->data->port_id, &link);
1182         if (ret < 0)
1183                 return ret;
1184
1185         if (vf >= pci_dev->max_vfs)
1186                 return -EINVAL;
1187
1188         if (tx_rate > link.link_speed)
1189                 return -EINVAL;
1190
1191         if (q_msk == 0)
1192                 return 0;
1193
1194         hw = TXGBE_DEV_HW(dev);
1195         vfinfo = *(TXGBE_DEV_VFDATA(dev));
1196         nb_q_per_pool = RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
1197         queue_stride = TXGBE_MAX_RX_QUEUE_NUM / RTE_ETH_DEV_SRIOV(dev).active;
1198         queue_idx = vf * queue_stride;
1199         queue_end = queue_idx + nb_q_per_pool - 1;
1200         if (queue_end >= hw->mac.max_tx_queues)
1201                 return -EINVAL;
1202
1203         if (vfinfo) {
1204                 for (vf_idx = 0; vf_idx < pci_dev->max_vfs; vf_idx++) {
1205                         if (vf_idx == vf)
1206                                 continue;
1207                         for (idx = 0; idx < RTE_DIM(vfinfo[vf_idx].tx_rate);
1208                                 idx++)
1209                                 total_rate += vfinfo[vf_idx].tx_rate[idx];
1210                 }
1211         } else {
1212                 return -EINVAL;
1213         }
1214
1215         /* Store tx_rate for this vf. */
1216         for (idx = 0; idx < nb_q_per_pool; idx++) {
1217                 if (((uint64_t)0x1 << idx) & q_msk) {
1218                         if (vfinfo[vf].tx_rate[idx] != tx_rate)
1219                                 vfinfo[vf].tx_rate[idx] = tx_rate;
1220                         total_rate += tx_rate;
1221                 }
1222         }
1223
1224         if (total_rate > dev->data->dev_link.link_speed) {
1225                 /* Reset stored TX rate of the VF if it causes exceed
1226                  * link speed.
1227                  */
1228                 memset(vfinfo[vf].tx_rate, 0, sizeof(vfinfo[vf].tx_rate));
1229                 return -EINVAL;
1230         }
1231
1232         /* Set ARBTXRATE of each queue/pool for vf X  */
1233         for (; queue_idx <= queue_end; queue_idx++) {
1234                 if (0x1 & q_msk)
1235                         txgbe_set_queue_rate_limit(dev, queue_idx, tx_rate);
1236                 q_msk = q_msk >> 1;
1237         }
1238
1239         return 0;
1240 }
1241
1242 /*
1243  * Configure device link speed and setup link.
1244  * It returns 0 on success.
1245  */
1246 static int
1247 txgbe_dev_start(struct rte_eth_dev *dev)
1248 {
1249         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1250         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
1251         struct txgbe_vf_info *vfinfo = *TXGBE_DEV_VFDATA(dev);
1252         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1253         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1254         uint32_t intr_vector = 0;
1255         int err;
1256         bool link_up = false, negotiate = 0;
1257         uint32_t speed = 0;
1258         uint32_t allowed_speeds = 0;
1259         int mask = 0;
1260         int status;
1261         uint16_t vf, idx;
1262         uint32_t *link_speeds;
1263
1264         PMD_INIT_FUNC_TRACE();
1265
1266         /* TXGBE devices don't support:
1267          *    - half duplex (checked afterwards for valid speeds)
1268          *    - fixed speed: TODO implement
1269          */
1270         if (dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED) {
1271                 PMD_INIT_LOG(ERR,
1272                 "Invalid link_speeds for port %u, fix speed not supported",
1273                                 dev->data->port_id);
1274                 return -EINVAL;
1275         }
1276
1277         /* Stop the link setup handler before resetting the HW. */
1278         rte_eal_alarm_cancel(txgbe_dev_setup_link_alarm_handler, dev);
1279
1280         /* disable uio/vfio intr/eventfd mapping */
1281         rte_intr_disable(intr_handle);
1282
1283         /* stop adapter */
1284         hw->adapter_stopped = 0;
1285         txgbe_stop_hw(hw);
1286
1287         /* reinitialize adapter
1288          * this calls reset and start
1289          */
1290         hw->nb_rx_queues = dev->data->nb_rx_queues;
1291         hw->nb_tx_queues = dev->data->nb_tx_queues;
1292         status = txgbe_pf_reset_hw(hw);
1293         if (status != 0)
1294                 return -1;
1295         hw->mac.start_hw(hw);
1296         hw->mac.get_link_status = true;
1297
1298         /* configure PF module if SRIOV enabled */
1299         txgbe_pf_host_configure(dev);
1300
1301         txgbe_dev_phy_intr_setup(dev);
1302
1303         /* check and configure queue intr-vector mapping */
1304         if ((rte_intr_cap_multiple(intr_handle) ||
1305              !RTE_ETH_DEV_SRIOV(dev).active) &&
1306             dev->data->dev_conf.intr_conf.rxq != 0) {
1307                 intr_vector = dev->data->nb_rx_queues;
1308                 if (rte_intr_efd_enable(intr_handle, intr_vector))
1309                         return -1;
1310         }
1311
1312         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
1313                 intr_handle->intr_vec =
1314                         rte_zmalloc("intr_vec",
1315                                     dev->data->nb_rx_queues * sizeof(int), 0);
1316                 if (intr_handle->intr_vec == NULL) {
1317                         PMD_INIT_LOG(ERR, "Failed to allocate %d rx_queues"
1318                                      " intr_vec", dev->data->nb_rx_queues);
1319                         return -ENOMEM;
1320                 }
1321         }
1322
1323         /* confiugre msix for sleep until rx interrupt */
1324         txgbe_configure_msix(dev);
1325
1326         /* initialize transmission unit */
1327         txgbe_dev_tx_init(dev);
1328
1329         /* This can fail when allocating mbufs for descriptor rings */
1330         err = txgbe_dev_rx_init(dev);
1331         if (err) {
1332                 PMD_INIT_LOG(ERR, "Unable to initialize RX hardware");
1333                 goto error;
1334         }
1335
1336         mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK |
1337                 ETH_VLAN_EXTEND_MASK;
1338         err = txgbe_vlan_offload_config(dev, mask);
1339         if (err) {
1340                 PMD_INIT_LOG(ERR, "Unable to set VLAN offload");
1341                 goto error;
1342         }
1343
1344         if (dev->data->dev_conf.rxmode.mq_mode == ETH_MQ_RX_VMDQ_ONLY) {
1345                 /* Enable vlan filtering for VMDq */
1346                 txgbe_vmdq_vlan_hw_filter_enable(dev);
1347         }
1348
1349         /* Restore vf rate limit */
1350         if (vfinfo != NULL) {
1351                 for (vf = 0; vf < pci_dev->max_vfs; vf++)
1352                         for (idx = 0; idx < TXGBE_MAX_QUEUE_NUM_PER_VF; idx++)
1353                                 if (vfinfo[vf].tx_rate[idx] != 0)
1354                                         txgbe_set_vf_rate_limit(dev, vf,
1355                                                 vfinfo[vf].tx_rate[idx],
1356                                                 1 << idx);
1357         }
1358
1359         err = txgbe_dev_rxtx_start(dev);
1360         if (err < 0) {
1361                 PMD_INIT_LOG(ERR, "Unable to start rxtx queues");
1362                 goto error;
1363         }
1364
1365         /* Skip link setup if loopback mode is enabled. */
1366         if (hw->mac.type == txgbe_mac_raptor &&
1367             dev->data->dev_conf.lpbk_mode)
1368                 goto skip_link_setup;
1369
1370         if (txgbe_is_sfp(hw) && hw->phy.multispeed_fiber) {
1371                 err = hw->mac.setup_sfp(hw);
1372                 if (err)
1373                         goto error;
1374         }
1375
1376         if (hw->phy.media_type == txgbe_media_type_copper) {
1377                 /* Turn on the copper */
1378                 hw->phy.set_phy_power(hw, true);
1379         } else {
1380                 /* Turn on the laser */
1381                 hw->mac.enable_tx_laser(hw);
1382         }
1383
1384         err = hw->mac.check_link(hw, &speed, &link_up, 0);
1385         if (err)
1386                 goto error;
1387         dev->data->dev_link.link_status = link_up;
1388
1389         err = hw->mac.get_link_capabilities(hw, &speed, &negotiate);
1390         if (err)
1391                 goto error;
1392
1393         allowed_speeds = ETH_LINK_SPEED_100M | ETH_LINK_SPEED_1G |
1394                         ETH_LINK_SPEED_10G;
1395
1396         link_speeds = &dev->data->dev_conf.link_speeds;
1397         if (*link_speeds & ~allowed_speeds) {
1398                 PMD_INIT_LOG(ERR, "Invalid link setting");
1399                 goto error;
1400         }
1401
1402         speed = 0x0;
1403         if (*link_speeds == ETH_LINK_SPEED_AUTONEG) {
1404                 speed = (TXGBE_LINK_SPEED_100M_FULL |
1405                          TXGBE_LINK_SPEED_1GB_FULL |
1406                          TXGBE_LINK_SPEED_10GB_FULL);
1407         } else {
1408                 if (*link_speeds & ETH_LINK_SPEED_10G)
1409                         speed |= TXGBE_LINK_SPEED_10GB_FULL;
1410                 if (*link_speeds & ETH_LINK_SPEED_5G)
1411                         speed |= TXGBE_LINK_SPEED_5GB_FULL;
1412                 if (*link_speeds & ETH_LINK_SPEED_2_5G)
1413                         speed |= TXGBE_LINK_SPEED_2_5GB_FULL;
1414                 if (*link_speeds & ETH_LINK_SPEED_1G)
1415                         speed |= TXGBE_LINK_SPEED_1GB_FULL;
1416                 if (*link_speeds & ETH_LINK_SPEED_100M)
1417                         speed |= TXGBE_LINK_SPEED_100M_FULL;
1418         }
1419
1420         err = hw->mac.setup_link(hw, speed, link_up);
1421         if (err)
1422                 goto error;
1423
1424 skip_link_setup:
1425
1426         if (rte_intr_allow_others(intr_handle)) {
1427                 /* check if lsc interrupt is enabled */
1428                 if (dev->data->dev_conf.intr_conf.lsc != 0)
1429                         txgbe_dev_lsc_interrupt_setup(dev, TRUE);
1430                 else
1431                         txgbe_dev_lsc_interrupt_setup(dev, FALSE);
1432                 txgbe_dev_macsec_interrupt_setup(dev);
1433                 txgbe_set_ivar_map(hw, -1, 1, TXGBE_MISC_VEC_ID);
1434         } else {
1435                 rte_intr_callback_unregister(intr_handle,
1436                                              txgbe_dev_interrupt_handler, dev);
1437                 if (dev->data->dev_conf.intr_conf.lsc != 0)
1438                         PMD_INIT_LOG(INFO, "lsc won't enable because of"
1439                                      " no intr multiplex");
1440         }
1441
1442         /* check if rxq interrupt is enabled */
1443         if (dev->data->dev_conf.intr_conf.rxq != 0 &&
1444             rte_intr_dp_is_en(intr_handle))
1445                 txgbe_dev_rxq_interrupt_setup(dev);
1446
1447         /* enable uio/vfio intr/eventfd mapping */
1448         rte_intr_enable(intr_handle);
1449
1450         /* resume enabled intr since hw reset */
1451         txgbe_enable_intr(dev);
1452
1453         /*
1454          * Update link status right before return, because it may
1455          * start link configuration process in a separate thread.
1456          */
1457         txgbe_dev_link_update(dev, 0);
1458
1459         wr32m(hw, TXGBE_LEDCTL, 0xFFFFFFFF, TXGBE_LEDCTL_ORD_MASK);
1460
1461         txgbe_read_stats_registers(hw, hw_stats);
1462         hw->offset_loaded = 1;
1463
1464         return 0;
1465
1466 error:
1467         PMD_INIT_LOG(ERR, "failure in dev start: %d", err);
1468         txgbe_dev_clear_queues(dev);
1469         return -EIO;
1470 }
1471
1472 /*
1473  * Stop device: disable rx and tx functions to allow for reconfiguring.
1474  */
1475 static int
1476 txgbe_dev_stop(struct rte_eth_dev *dev)
1477 {
1478         struct rte_eth_link link;
1479         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
1480         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1481         struct txgbe_vf_info *vfinfo = *TXGBE_DEV_VFDATA(dev);
1482         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1483         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1484         int vf;
1485
1486         if (hw->adapter_stopped)
1487                 return 0;
1488
1489         PMD_INIT_FUNC_TRACE();
1490
1491         rte_eal_alarm_cancel(txgbe_dev_setup_link_alarm_handler, dev);
1492
1493         /* disable interrupts */
1494         txgbe_disable_intr(hw);
1495
1496         /* reset the NIC */
1497         txgbe_pf_reset_hw(hw);
1498         hw->adapter_stopped = 0;
1499
1500         /* stop adapter */
1501         txgbe_stop_hw(hw);
1502
1503         for (vf = 0; vfinfo != NULL && vf < pci_dev->max_vfs; vf++)
1504                 vfinfo[vf].clear_to_send = false;
1505
1506         if (hw->phy.media_type == txgbe_media_type_copper) {
1507                 /* Turn off the copper */
1508                 hw->phy.set_phy_power(hw, false);
1509         } else {
1510                 /* Turn off the laser */
1511                 hw->mac.disable_tx_laser(hw);
1512         }
1513
1514         txgbe_dev_clear_queues(dev);
1515
1516         /* Clear stored conf */
1517         dev->data->scattered_rx = 0;
1518         dev->data->lro = 0;
1519
1520         /* Clear recorded link status */
1521         memset(&link, 0, sizeof(link));
1522         rte_eth_linkstatus_set(dev, &link);
1523
1524         if (!rte_intr_allow_others(intr_handle))
1525                 /* resume to the default handler */
1526                 rte_intr_callback_register(intr_handle,
1527                                            txgbe_dev_interrupt_handler,
1528                                            (void *)dev);
1529
1530         /* Clean datapath event and queue/vec mapping */
1531         rte_intr_efd_disable(intr_handle);
1532         if (intr_handle->intr_vec != NULL) {
1533                 rte_free(intr_handle->intr_vec);
1534                 intr_handle->intr_vec = NULL;
1535         }
1536
1537         adapter->rss_reta_updated = 0;
1538         wr32m(hw, TXGBE_LEDCTL, 0xFFFFFFFF, TXGBE_LEDCTL_SEL_MASK);
1539
1540         hw->adapter_stopped = true;
1541         dev->data->dev_started = 0;
1542
1543         return 0;
1544 }
1545
1546 /*
1547  * Set device link up: enable tx.
1548  */
1549 static int
1550 txgbe_dev_set_link_up(struct rte_eth_dev *dev)
1551 {
1552         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1553
1554         if (hw->phy.media_type == txgbe_media_type_copper) {
1555                 /* Turn on the copper */
1556                 hw->phy.set_phy_power(hw, true);
1557         } else {
1558                 /* Turn on the laser */
1559                 hw->mac.enable_tx_laser(hw);
1560                 txgbe_dev_link_update(dev, 0);
1561         }
1562
1563         return 0;
1564 }
1565
1566 /*
1567  * Set device link down: disable tx.
1568  */
1569 static int
1570 txgbe_dev_set_link_down(struct rte_eth_dev *dev)
1571 {
1572         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1573
1574         if (hw->phy.media_type == txgbe_media_type_copper) {
1575                 /* Turn off the copper */
1576                 hw->phy.set_phy_power(hw, false);
1577         } else {
1578                 /* Turn off the laser */
1579                 hw->mac.disable_tx_laser(hw);
1580                 txgbe_dev_link_update(dev, 0);
1581         }
1582
1583         return 0;
1584 }
1585
1586 /*
1587  * Reset and stop device.
1588  */
1589 static int
1590 txgbe_dev_close(struct rte_eth_dev *dev)
1591 {
1592         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1593         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1594         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1595         int retries = 0;
1596         int ret;
1597
1598         PMD_INIT_FUNC_TRACE();
1599
1600         txgbe_pf_reset_hw(hw);
1601
1602         ret = txgbe_dev_stop(dev);
1603
1604         txgbe_dev_free_queues(dev);
1605
1606         /* reprogram the RAR[0] in case user changed it. */
1607         txgbe_set_rar(hw, 0, hw->mac.addr, 0, true);
1608
1609         /* Unlock any pending hardware semaphore */
1610         txgbe_swfw_lock_reset(hw);
1611
1612         /* disable uio intr before callback unregister */
1613         rte_intr_disable(intr_handle);
1614
1615         do {
1616                 ret = rte_intr_callback_unregister(intr_handle,
1617                                 txgbe_dev_interrupt_handler, dev);
1618                 if (ret >= 0 || ret == -ENOENT) {
1619                         break;
1620                 } else if (ret != -EAGAIN) {
1621                         PMD_INIT_LOG(ERR,
1622                                 "intr callback unregister failed: %d",
1623                                 ret);
1624                 }
1625                 rte_delay_ms(100);
1626         } while (retries++ < (10 + TXGBE_LINK_UP_TIME));
1627
1628         /* cancel the delay handler before remove dev */
1629         rte_eal_alarm_cancel(txgbe_dev_interrupt_delayed_handler, dev);
1630
1631         /* uninitialize PF if max_vfs not zero */
1632         txgbe_pf_host_uninit(dev);
1633
1634         rte_free(dev->data->mac_addrs);
1635         dev->data->mac_addrs = NULL;
1636
1637         rte_free(dev->data->hash_mac_addrs);
1638         dev->data->hash_mac_addrs = NULL;
1639
1640         return ret;
1641 }
1642
1643 /*
1644  * Reset PF device.
1645  */
1646 static int
1647 txgbe_dev_reset(struct rte_eth_dev *dev)
1648 {
1649         int ret;
1650
1651         /* When a DPDK PMD PF begin to reset PF port, it should notify all
1652          * its VF to make them align with it. The detailed notification
1653          * mechanism is PMD specific. As to txgbe PF, it is rather complex.
1654          * To avoid unexpected behavior in VF, currently reset of PF with
1655          * SR-IOV activation is not supported. It might be supported later.
1656          */
1657         if (dev->data->sriov.active)
1658                 return -ENOTSUP;
1659
1660         ret = eth_txgbe_dev_uninit(dev);
1661         if (ret)
1662                 return ret;
1663
1664         ret = eth_txgbe_dev_init(dev, NULL);
1665
1666         return ret;
1667 }
1668
1669 #define UPDATE_QP_COUNTER_32bit(reg, last_counter, counter)     \
1670         {                                                       \
1671                 uint32_t current_counter = rd32(hw, reg);       \
1672                 if (current_counter < last_counter)             \
1673                         current_counter += 0x100000000LL;       \
1674                 if (!hw->offset_loaded)                         \
1675                         last_counter = current_counter;         \
1676                 counter = current_counter - last_counter;       \
1677                 counter &= 0xFFFFFFFFLL;                        \
1678         }
1679
1680 #define UPDATE_QP_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
1681         {                                                                \
1682                 uint64_t current_counter_lsb = rd32(hw, reg_lsb);        \
1683                 uint64_t current_counter_msb = rd32(hw, reg_msb);        \
1684                 uint64_t current_counter = (current_counter_msb << 32) | \
1685                         current_counter_lsb;                             \
1686                 if (current_counter < last_counter)                      \
1687                         current_counter += 0x1000000000LL;               \
1688                 if (!hw->offset_loaded)                                  \
1689                         last_counter = current_counter;                  \
1690                 counter = current_counter - last_counter;                \
1691                 counter &= 0xFFFFFFFFFLL;                                \
1692         }
1693
1694 void
1695 txgbe_read_stats_registers(struct txgbe_hw *hw,
1696                            struct txgbe_hw_stats *hw_stats)
1697 {
1698         unsigned int i;
1699
1700         /* QP Stats */
1701         for (i = 0; i < hw->nb_rx_queues; i++) {
1702                 UPDATE_QP_COUNTER_32bit(TXGBE_QPRXPKT(i),
1703                         hw->qp_last[i].rx_qp_packets,
1704                         hw_stats->qp[i].rx_qp_packets);
1705                 UPDATE_QP_COUNTER_36bit(TXGBE_QPRXOCTL(i), TXGBE_QPRXOCTH(i),
1706                         hw->qp_last[i].rx_qp_bytes,
1707                         hw_stats->qp[i].rx_qp_bytes);
1708                 UPDATE_QP_COUNTER_32bit(TXGBE_QPRXMPKT(i),
1709                         hw->qp_last[i].rx_qp_mc_packets,
1710                         hw_stats->qp[i].rx_qp_mc_packets);
1711         }
1712
1713         for (i = 0; i < hw->nb_tx_queues; i++) {
1714                 UPDATE_QP_COUNTER_32bit(TXGBE_QPTXPKT(i),
1715                         hw->qp_last[i].tx_qp_packets,
1716                         hw_stats->qp[i].tx_qp_packets);
1717                 UPDATE_QP_COUNTER_36bit(TXGBE_QPTXOCTL(i), TXGBE_QPTXOCTH(i),
1718                         hw->qp_last[i].tx_qp_bytes,
1719                         hw_stats->qp[i].tx_qp_bytes);
1720         }
1721         /* PB Stats */
1722         for (i = 0; i < TXGBE_MAX_UP; i++) {
1723                 hw_stats->up[i].rx_up_xon_packets +=
1724                                 rd32(hw, TXGBE_PBRXUPXON(i));
1725                 hw_stats->up[i].rx_up_xoff_packets +=
1726                                 rd32(hw, TXGBE_PBRXUPXOFF(i));
1727                 hw_stats->up[i].tx_up_xon_packets +=
1728                                 rd32(hw, TXGBE_PBTXUPXON(i));
1729                 hw_stats->up[i].tx_up_xoff_packets +=
1730                                 rd32(hw, TXGBE_PBTXUPXOFF(i));
1731                 hw_stats->up[i].tx_up_xon2off_packets +=
1732                                 rd32(hw, TXGBE_PBTXUPOFF(i));
1733                 hw_stats->up[i].rx_up_dropped +=
1734                                 rd32(hw, TXGBE_PBRXMISS(i));
1735         }
1736         hw_stats->rx_xon_packets += rd32(hw, TXGBE_PBRXLNKXON);
1737         hw_stats->rx_xoff_packets += rd32(hw, TXGBE_PBRXLNKXOFF);
1738         hw_stats->tx_xon_packets += rd32(hw, TXGBE_PBTXLNKXON);
1739         hw_stats->tx_xoff_packets += rd32(hw, TXGBE_PBTXLNKXOFF);
1740
1741         /* DMA Stats */
1742         hw_stats->rx_packets += rd32(hw, TXGBE_DMARXPKT);
1743         hw_stats->tx_packets += rd32(hw, TXGBE_DMATXPKT);
1744
1745         hw_stats->rx_bytes += rd64(hw, TXGBE_DMARXOCTL);
1746         hw_stats->tx_bytes += rd64(hw, TXGBE_DMATXOCTL);
1747         hw_stats->rx_drop_packets += rd32(hw, TXGBE_PBRXDROP);
1748
1749         /* MAC Stats */
1750         hw_stats->rx_crc_errors += rd64(hw, TXGBE_MACRXERRCRCL);
1751         hw_stats->rx_multicast_packets += rd64(hw, TXGBE_MACRXMPKTL);
1752         hw_stats->tx_multicast_packets += rd64(hw, TXGBE_MACTXMPKTL);
1753
1754         hw_stats->rx_total_packets += rd64(hw, TXGBE_MACRXPKTL);
1755         hw_stats->tx_total_packets += rd64(hw, TXGBE_MACTXPKTL);
1756         hw_stats->rx_total_bytes += rd64(hw, TXGBE_MACRXGBOCTL);
1757
1758         hw_stats->rx_broadcast_packets += rd64(hw, TXGBE_MACRXOCTL);
1759         hw_stats->tx_broadcast_packets += rd32(hw, TXGBE_MACTXOCTL);
1760
1761         hw_stats->rx_size_64_packets += rd64(hw, TXGBE_MACRX1TO64L);
1762         hw_stats->rx_size_65_to_127_packets += rd64(hw, TXGBE_MACRX65TO127L);
1763         hw_stats->rx_size_128_to_255_packets += rd64(hw, TXGBE_MACRX128TO255L);
1764         hw_stats->rx_size_256_to_511_packets += rd64(hw, TXGBE_MACRX256TO511L);
1765         hw_stats->rx_size_512_to_1023_packets +=
1766                         rd64(hw, TXGBE_MACRX512TO1023L);
1767         hw_stats->rx_size_1024_to_max_packets +=
1768                         rd64(hw, TXGBE_MACRX1024TOMAXL);
1769         hw_stats->tx_size_64_packets += rd64(hw, TXGBE_MACTX1TO64L);
1770         hw_stats->tx_size_65_to_127_packets += rd64(hw, TXGBE_MACTX65TO127L);
1771         hw_stats->tx_size_128_to_255_packets += rd64(hw, TXGBE_MACTX128TO255L);
1772         hw_stats->tx_size_256_to_511_packets += rd64(hw, TXGBE_MACTX256TO511L);
1773         hw_stats->tx_size_512_to_1023_packets +=
1774                         rd64(hw, TXGBE_MACTX512TO1023L);
1775         hw_stats->tx_size_1024_to_max_packets +=
1776                         rd64(hw, TXGBE_MACTX1024TOMAXL);
1777
1778         hw_stats->rx_undersize_errors += rd64(hw, TXGBE_MACRXERRLENL);
1779         hw_stats->rx_oversize_errors += rd32(hw, TXGBE_MACRXOVERSIZE);
1780         hw_stats->rx_jabber_errors += rd32(hw, TXGBE_MACRXJABBER);
1781
1782         /* MNG Stats */
1783         hw_stats->mng_bmc2host_packets = rd32(hw, TXGBE_MNGBMC2OS);
1784         hw_stats->mng_host2bmc_packets = rd32(hw, TXGBE_MNGOS2BMC);
1785         hw_stats->rx_management_packets = rd32(hw, TXGBE_DMARXMNG);
1786         hw_stats->tx_management_packets = rd32(hw, TXGBE_DMATXMNG);
1787
1788         /* FCoE Stats */
1789         hw_stats->rx_fcoe_crc_errors += rd32(hw, TXGBE_FCOECRC);
1790         hw_stats->rx_fcoe_mbuf_allocation_errors += rd32(hw, TXGBE_FCOELAST);
1791         hw_stats->rx_fcoe_dropped += rd32(hw, TXGBE_FCOERPDC);
1792         hw_stats->rx_fcoe_packets += rd32(hw, TXGBE_FCOEPRC);
1793         hw_stats->tx_fcoe_packets += rd32(hw, TXGBE_FCOEPTC);
1794         hw_stats->rx_fcoe_bytes += rd32(hw, TXGBE_FCOEDWRC);
1795         hw_stats->tx_fcoe_bytes += rd32(hw, TXGBE_FCOEDWTC);
1796
1797         /* Flow Director Stats */
1798         hw_stats->flow_director_matched_filters += rd32(hw, TXGBE_FDIRMATCH);
1799         hw_stats->flow_director_missed_filters += rd32(hw, TXGBE_FDIRMISS);
1800         hw_stats->flow_director_added_filters +=
1801                 TXGBE_FDIRUSED_ADD(rd32(hw, TXGBE_FDIRUSED));
1802         hw_stats->flow_director_removed_filters +=
1803                 TXGBE_FDIRUSED_REM(rd32(hw, TXGBE_FDIRUSED));
1804         hw_stats->flow_director_filter_add_errors +=
1805                 TXGBE_FDIRFAIL_ADD(rd32(hw, TXGBE_FDIRFAIL));
1806         hw_stats->flow_director_filter_remove_errors +=
1807                 TXGBE_FDIRFAIL_REM(rd32(hw, TXGBE_FDIRFAIL));
1808
1809         /* MACsec Stats */
1810         hw_stats->tx_macsec_pkts_untagged += rd32(hw, TXGBE_LSECTX_UTPKT);
1811         hw_stats->tx_macsec_pkts_encrypted +=
1812                         rd32(hw, TXGBE_LSECTX_ENCPKT);
1813         hw_stats->tx_macsec_pkts_protected +=
1814                         rd32(hw, TXGBE_LSECTX_PROTPKT);
1815         hw_stats->tx_macsec_octets_encrypted +=
1816                         rd32(hw, TXGBE_LSECTX_ENCOCT);
1817         hw_stats->tx_macsec_octets_protected +=
1818                         rd32(hw, TXGBE_LSECTX_PROTOCT);
1819         hw_stats->rx_macsec_pkts_untagged += rd32(hw, TXGBE_LSECRX_UTPKT);
1820         hw_stats->rx_macsec_pkts_badtag += rd32(hw, TXGBE_LSECRX_BTPKT);
1821         hw_stats->rx_macsec_pkts_nosci += rd32(hw, TXGBE_LSECRX_NOSCIPKT);
1822         hw_stats->rx_macsec_pkts_unknownsci += rd32(hw, TXGBE_LSECRX_UNSCIPKT);
1823         hw_stats->rx_macsec_octets_decrypted += rd32(hw, TXGBE_LSECRX_DECOCT);
1824         hw_stats->rx_macsec_octets_validated += rd32(hw, TXGBE_LSECRX_VLDOCT);
1825         hw_stats->rx_macsec_sc_pkts_unchecked +=
1826                         rd32(hw, TXGBE_LSECRX_UNCHKPKT);
1827         hw_stats->rx_macsec_sc_pkts_delayed += rd32(hw, TXGBE_LSECRX_DLYPKT);
1828         hw_stats->rx_macsec_sc_pkts_late += rd32(hw, TXGBE_LSECRX_LATEPKT);
1829         for (i = 0; i < 2; i++) {
1830                 hw_stats->rx_macsec_sa_pkts_ok +=
1831                         rd32(hw, TXGBE_LSECRX_OKPKT(i));
1832                 hw_stats->rx_macsec_sa_pkts_invalid +=
1833                         rd32(hw, TXGBE_LSECRX_INVPKT(i));
1834                 hw_stats->rx_macsec_sa_pkts_notvalid +=
1835                         rd32(hw, TXGBE_LSECRX_BADPKT(i));
1836         }
1837         hw_stats->rx_macsec_sa_pkts_unusedsa +=
1838                         rd32(hw, TXGBE_LSECRX_INVSAPKT);
1839         hw_stats->rx_macsec_sa_pkts_notusingsa +=
1840                         rd32(hw, TXGBE_LSECRX_BADSAPKT);
1841
1842         hw_stats->rx_total_missed_packets = 0;
1843         for (i = 0; i < TXGBE_MAX_UP; i++) {
1844                 hw_stats->rx_total_missed_packets +=
1845                         hw_stats->up[i].rx_up_dropped;
1846         }
1847 }
1848
1849 static int
1850 txgbe_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1851 {
1852         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1853         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
1854         struct txgbe_stat_mappings *stat_mappings =
1855                         TXGBE_DEV_STAT_MAPPINGS(dev);
1856         uint32_t i, j;
1857
1858         txgbe_read_stats_registers(hw, hw_stats);
1859
1860         if (stats == NULL)
1861                 return -EINVAL;
1862
1863         /* Fill out the rte_eth_stats statistics structure */
1864         stats->ipackets = hw_stats->rx_packets;
1865         stats->ibytes = hw_stats->rx_bytes;
1866         stats->opackets = hw_stats->tx_packets;
1867         stats->obytes = hw_stats->tx_bytes;
1868
1869         memset(&stats->q_ipackets, 0, sizeof(stats->q_ipackets));
1870         memset(&stats->q_opackets, 0, sizeof(stats->q_opackets));
1871         memset(&stats->q_ibytes, 0, sizeof(stats->q_ibytes));
1872         memset(&stats->q_obytes, 0, sizeof(stats->q_obytes));
1873         memset(&stats->q_errors, 0, sizeof(stats->q_errors));
1874         for (i = 0; i < TXGBE_MAX_QP; i++) {
1875                 uint32_t n = i / NB_QMAP_FIELDS_PER_QSM_REG;
1876                 uint32_t offset = (i % NB_QMAP_FIELDS_PER_QSM_REG) * 8;
1877                 uint32_t q_map;
1878
1879                 q_map = (stat_mappings->rqsm[n] >> offset)
1880                                 & QMAP_FIELD_RESERVED_BITS_MASK;
1881                 j = (q_map < RTE_ETHDEV_QUEUE_STAT_CNTRS
1882                      ? q_map : q_map % RTE_ETHDEV_QUEUE_STAT_CNTRS);
1883                 stats->q_ipackets[j] += hw_stats->qp[i].rx_qp_packets;
1884                 stats->q_ibytes[j] += hw_stats->qp[i].rx_qp_bytes;
1885
1886                 q_map = (stat_mappings->tqsm[n] >> offset)
1887                                 & QMAP_FIELD_RESERVED_BITS_MASK;
1888                 j = (q_map < RTE_ETHDEV_QUEUE_STAT_CNTRS
1889                      ? q_map : q_map % RTE_ETHDEV_QUEUE_STAT_CNTRS);
1890                 stats->q_opackets[j] += hw_stats->qp[i].tx_qp_packets;
1891                 stats->q_obytes[j] += hw_stats->qp[i].tx_qp_bytes;
1892         }
1893
1894         /* Rx Errors */
1895         stats->imissed  = hw_stats->rx_total_missed_packets;
1896         stats->ierrors  = hw_stats->rx_crc_errors +
1897                           hw_stats->rx_mac_short_packet_dropped +
1898                           hw_stats->rx_length_errors +
1899                           hw_stats->rx_undersize_errors +
1900                           hw_stats->rx_oversize_errors +
1901                           hw_stats->rx_drop_packets +
1902                           hw_stats->rx_illegal_byte_errors +
1903                           hw_stats->rx_error_bytes +
1904                           hw_stats->rx_fragment_errors +
1905                           hw_stats->rx_fcoe_crc_errors +
1906                           hw_stats->rx_fcoe_mbuf_allocation_errors;
1907
1908         /* Tx Errors */
1909         stats->oerrors  = 0;
1910         return 0;
1911 }
1912
1913 static int
1914 txgbe_dev_stats_reset(struct rte_eth_dev *dev)
1915 {
1916         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1917         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
1918
1919         /* HW registers are cleared on read */
1920         hw->offset_loaded = 0;
1921         txgbe_dev_stats_get(dev, NULL);
1922         hw->offset_loaded = 1;
1923
1924         /* Reset software totals */
1925         memset(hw_stats, 0, sizeof(*hw_stats));
1926
1927         return 0;
1928 }
1929
1930 /* This function calculates the number of xstats based on the current config */
1931 static unsigned
1932 txgbe_xstats_calc_num(struct rte_eth_dev *dev)
1933 {
1934         int nb_queues = max(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
1935         return TXGBE_NB_HW_STATS +
1936                TXGBE_NB_UP_STATS * TXGBE_MAX_UP +
1937                TXGBE_NB_QP_STATS * nb_queues;
1938 }
1939
1940 static inline int
1941 txgbe_get_name_by_id(uint32_t id, char *name, uint32_t size)
1942 {
1943         int nb, st;
1944
1945         /* Extended stats from txgbe_hw_stats */
1946         if (id < TXGBE_NB_HW_STATS) {
1947                 snprintf(name, size, "[hw]%s",
1948                         rte_txgbe_stats_strings[id].name);
1949                 return 0;
1950         }
1951         id -= TXGBE_NB_HW_STATS;
1952
1953         /* Priority Stats */
1954         if (id < TXGBE_NB_UP_STATS * TXGBE_MAX_UP) {
1955                 nb = id / TXGBE_NB_UP_STATS;
1956                 st = id % TXGBE_NB_UP_STATS;
1957                 snprintf(name, size, "[p%u]%s", nb,
1958                         rte_txgbe_up_strings[st].name);
1959                 return 0;
1960         }
1961         id -= TXGBE_NB_UP_STATS * TXGBE_MAX_UP;
1962
1963         /* Queue Stats */
1964         if (id < TXGBE_NB_QP_STATS * TXGBE_MAX_QP) {
1965                 nb = id / TXGBE_NB_QP_STATS;
1966                 st = id % TXGBE_NB_QP_STATS;
1967                 snprintf(name, size, "[q%u]%s", nb,
1968                         rte_txgbe_qp_strings[st].name);
1969                 return 0;
1970         }
1971         id -= TXGBE_NB_QP_STATS * TXGBE_MAX_QP;
1972
1973         return -(int)(id + 1);
1974 }
1975
1976 static inline int
1977 txgbe_get_offset_by_id(uint32_t id, uint32_t *offset)
1978 {
1979         int nb, st;
1980
1981         /* Extended stats from txgbe_hw_stats */
1982         if (id < TXGBE_NB_HW_STATS) {
1983                 *offset = rte_txgbe_stats_strings[id].offset;
1984                 return 0;
1985         }
1986         id -= TXGBE_NB_HW_STATS;
1987
1988         /* Priority Stats */
1989         if (id < TXGBE_NB_UP_STATS * TXGBE_MAX_UP) {
1990                 nb = id / TXGBE_NB_UP_STATS;
1991                 st = id % TXGBE_NB_UP_STATS;
1992                 *offset = rte_txgbe_up_strings[st].offset +
1993                         nb * (TXGBE_NB_UP_STATS * sizeof(uint64_t));
1994                 return 0;
1995         }
1996         id -= TXGBE_NB_UP_STATS * TXGBE_MAX_UP;
1997
1998         /* Queue Stats */
1999         if (id < TXGBE_NB_QP_STATS * TXGBE_MAX_QP) {
2000                 nb = id / TXGBE_NB_QP_STATS;
2001                 st = id % TXGBE_NB_QP_STATS;
2002                 *offset = rte_txgbe_qp_strings[st].offset +
2003                         nb * (TXGBE_NB_QP_STATS * sizeof(uint64_t));
2004                 return 0;
2005         }
2006         id -= TXGBE_NB_QP_STATS * TXGBE_MAX_QP;
2007
2008         return -(int)(id + 1);
2009 }
2010
2011 static int txgbe_dev_xstats_get_names(struct rte_eth_dev *dev,
2012         struct rte_eth_xstat_name *xstats_names, unsigned int limit)
2013 {
2014         unsigned int i, count;
2015
2016         count = txgbe_xstats_calc_num(dev);
2017         if (xstats_names == NULL)
2018                 return count;
2019
2020         /* Note: limit >= cnt_stats checked upstream
2021          * in rte_eth_xstats_names()
2022          */
2023         limit = min(limit, count);
2024
2025         /* Extended stats from txgbe_hw_stats */
2026         for (i = 0; i < limit; i++) {
2027                 if (txgbe_get_name_by_id(i, xstats_names[i].name,
2028                         sizeof(xstats_names[i].name))) {
2029                         PMD_INIT_LOG(WARNING, "id value %d isn't valid", i);
2030                         break;
2031                 }
2032         }
2033
2034         return i;
2035 }
2036
2037 static int txgbe_dev_xstats_get_names_by_id(struct rte_eth_dev *dev,
2038         struct rte_eth_xstat_name *xstats_names,
2039         const uint64_t *ids,
2040         unsigned int limit)
2041 {
2042         unsigned int i;
2043
2044         if (ids == NULL)
2045                 return txgbe_dev_xstats_get_names(dev, xstats_names, limit);
2046
2047         for (i = 0; i < limit; i++) {
2048                 if (txgbe_get_name_by_id(ids[i], xstats_names[i].name,
2049                                 sizeof(xstats_names[i].name))) {
2050                         PMD_INIT_LOG(WARNING, "id value %d isn't valid", i);
2051                         return -1;
2052                 }
2053         }
2054
2055         return i;
2056 }
2057
2058 static int
2059 txgbe_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
2060                                          unsigned int limit)
2061 {
2062         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2063         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
2064         unsigned int i, count;
2065
2066         txgbe_read_stats_registers(hw, hw_stats);
2067
2068         /* If this is a reset xstats is NULL, and we have cleared the
2069          * registers by reading them.
2070          */
2071         count = txgbe_xstats_calc_num(dev);
2072         if (xstats == NULL)
2073                 return count;
2074
2075         limit = min(limit, txgbe_xstats_calc_num(dev));
2076
2077         /* Extended stats from txgbe_hw_stats */
2078         for (i = 0; i < limit; i++) {
2079                 uint32_t offset = 0;
2080
2081                 if (txgbe_get_offset_by_id(i, &offset)) {
2082                         PMD_INIT_LOG(WARNING, "id value %d isn't valid", i);
2083                         break;
2084                 }
2085                 xstats[i].value = *(uint64_t *)(((char *)hw_stats) + offset);
2086                 xstats[i].id = i;
2087         }
2088
2089         return i;
2090 }
2091
2092 static int
2093 txgbe_dev_xstats_get_(struct rte_eth_dev *dev, uint64_t *values,
2094                                          unsigned int limit)
2095 {
2096         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2097         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
2098         unsigned int i, count;
2099
2100         txgbe_read_stats_registers(hw, hw_stats);
2101
2102         /* If this is a reset xstats is NULL, and we have cleared the
2103          * registers by reading them.
2104          */
2105         count = txgbe_xstats_calc_num(dev);
2106         if (values == NULL)
2107                 return count;
2108
2109         limit = min(limit, txgbe_xstats_calc_num(dev));
2110
2111         /* Extended stats from txgbe_hw_stats */
2112         for (i = 0; i < limit; i++) {
2113                 uint32_t offset;
2114
2115                 if (txgbe_get_offset_by_id(i, &offset)) {
2116                         PMD_INIT_LOG(WARNING, "id value %d isn't valid", i);
2117                         break;
2118                 }
2119                 values[i] = *(uint64_t *)(((char *)hw_stats) + offset);
2120         }
2121
2122         return i;
2123 }
2124
2125 static int
2126 txgbe_dev_xstats_get_by_id(struct rte_eth_dev *dev, const uint64_t *ids,
2127                 uint64_t *values, unsigned int limit)
2128 {
2129         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
2130         unsigned int i;
2131
2132         if (ids == NULL)
2133                 return txgbe_dev_xstats_get_(dev, values, limit);
2134
2135         for (i = 0; i < limit; i++) {
2136                 uint32_t offset;
2137
2138                 if (txgbe_get_offset_by_id(ids[i], &offset)) {
2139                         PMD_INIT_LOG(WARNING, "id value %d isn't valid", i);
2140                         break;
2141                 }
2142                 values[i] = *(uint64_t *)(((char *)hw_stats) + offset);
2143         }
2144
2145         return i;
2146 }
2147
2148 static int
2149 txgbe_dev_xstats_reset(struct rte_eth_dev *dev)
2150 {
2151         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2152         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
2153
2154         /* HW registers are cleared on read */
2155         hw->offset_loaded = 0;
2156         txgbe_read_stats_registers(hw, hw_stats);
2157         hw->offset_loaded = 1;
2158
2159         /* Reset software totals */
2160         memset(hw_stats, 0, sizeof(*hw_stats));
2161
2162         return 0;
2163 }
2164
2165 static int
2166 txgbe_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2167 {
2168         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2169         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2170
2171         dev_info->max_rx_queues = (uint16_t)hw->mac.max_rx_queues;
2172         dev_info->max_tx_queues = (uint16_t)hw->mac.max_tx_queues;
2173         dev_info->min_rx_bufsize = 1024;
2174         dev_info->max_rx_pktlen = 15872;
2175         dev_info->max_mac_addrs = hw->mac.num_rar_entries;
2176         dev_info->max_hash_mac_addrs = TXGBE_VMDQ_NUM_UC_MAC;
2177         dev_info->max_vfs = pci_dev->max_vfs;
2178         dev_info->max_vmdq_pools = ETH_64_POOLS;
2179         dev_info->vmdq_queue_num = dev_info->max_rx_queues;
2180         dev_info->rx_queue_offload_capa = txgbe_get_rx_queue_offloads(dev);
2181         dev_info->rx_offload_capa = (txgbe_get_rx_port_offloads(dev) |
2182                                      dev_info->rx_queue_offload_capa);
2183         dev_info->tx_queue_offload_capa = txgbe_get_tx_queue_offloads(dev);
2184         dev_info->tx_offload_capa = txgbe_get_tx_port_offloads(dev);
2185
2186         dev_info->default_rxconf = (struct rte_eth_rxconf) {
2187                 .rx_thresh = {
2188                         .pthresh = TXGBE_DEFAULT_RX_PTHRESH,
2189                         .hthresh = TXGBE_DEFAULT_RX_HTHRESH,
2190                         .wthresh = TXGBE_DEFAULT_RX_WTHRESH,
2191                 },
2192                 .rx_free_thresh = TXGBE_DEFAULT_RX_FREE_THRESH,
2193                 .rx_drop_en = 0,
2194                 .offloads = 0,
2195         };
2196
2197         dev_info->default_txconf = (struct rte_eth_txconf) {
2198                 .tx_thresh = {
2199                         .pthresh = TXGBE_DEFAULT_TX_PTHRESH,
2200                         .hthresh = TXGBE_DEFAULT_TX_HTHRESH,
2201                         .wthresh = TXGBE_DEFAULT_TX_WTHRESH,
2202                 },
2203                 .tx_free_thresh = TXGBE_DEFAULT_TX_FREE_THRESH,
2204                 .offloads = 0,
2205         };
2206
2207         dev_info->rx_desc_lim = rx_desc_lim;
2208         dev_info->tx_desc_lim = tx_desc_lim;
2209
2210         dev_info->hash_key_size = TXGBE_HKEY_MAX_INDEX * sizeof(uint32_t);
2211         dev_info->reta_size = ETH_RSS_RETA_SIZE_128;
2212         dev_info->flow_type_rss_offloads = TXGBE_RSS_OFFLOAD_ALL;
2213
2214         dev_info->speed_capa = ETH_LINK_SPEED_1G | ETH_LINK_SPEED_10G;
2215         dev_info->speed_capa |= ETH_LINK_SPEED_100M;
2216
2217         /* Driver-preferred Rx/Tx parameters */
2218         dev_info->default_rxportconf.burst_size = 32;
2219         dev_info->default_txportconf.burst_size = 32;
2220         dev_info->default_rxportconf.nb_queues = 1;
2221         dev_info->default_txportconf.nb_queues = 1;
2222         dev_info->default_rxportconf.ring_size = 256;
2223         dev_info->default_txportconf.ring_size = 256;
2224
2225         return 0;
2226 }
2227
2228 const uint32_t *
2229 txgbe_dev_supported_ptypes_get(struct rte_eth_dev *dev)
2230 {
2231         if (dev->rx_pkt_burst == txgbe_recv_pkts ||
2232             dev->rx_pkt_burst == txgbe_recv_pkts_lro_single_alloc ||
2233             dev->rx_pkt_burst == txgbe_recv_pkts_lro_bulk_alloc ||
2234             dev->rx_pkt_burst == txgbe_recv_pkts_bulk_alloc)
2235                 return txgbe_get_supported_ptypes();
2236
2237         return NULL;
2238 }
2239
2240 void
2241 txgbe_dev_setup_link_alarm_handler(void *param)
2242 {
2243         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2244         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2245         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2246         u32 speed;
2247         bool autoneg = false;
2248
2249         speed = hw->phy.autoneg_advertised;
2250         if (!speed)
2251                 hw->mac.get_link_capabilities(hw, &speed, &autoneg);
2252
2253         hw->mac.setup_link(hw, speed, true);
2254
2255         intr->flags &= ~TXGBE_FLAG_NEED_LINK_CONFIG;
2256 }
2257
2258 /* return 0 means link status changed, -1 means not changed */
2259 int
2260 txgbe_dev_link_update_share(struct rte_eth_dev *dev,
2261                             int wait_to_complete)
2262 {
2263         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2264         struct rte_eth_link link;
2265         u32 link_speed = TXGBE_LINK_SPEED_UNKNOWN;
2266         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2267         bool link_up;
2268         int err;
2269         int wait = 1;
2270
2271         memset(&link, 0, sizeof(link));
2272         link.link_status = ETH_LINK_DOWN;
2273         link.link_speed = ETH_SPEED_NUM_NONE;
2274         link.link_duplex = ETH_LINK_HALF_DUPLEX;
2275         link.link_autoneg = ETH_LINK_AUTONEG;
2276
2277         hw->mac.get_link_status = true;
2278
2279         if (intr->flags & TXGBE_FLAG_NEED_LINK_CONFIG)
2280                 return rte_eth_linkstatus_set(dev, &link);
2281
2282         /* check if it needs to wait to complete, if lsc interrupt is enabled */
2283         if (wait_to_complete == 0 || dev->data->dev_conf.intr_conf.lsc != 0)
2284                 wait = 0;
2285
2286         err = hw->mac.check_link(hw, &link_speed, &link_up, wait);
2287
2288         if (err != 0) {
2289                 link.link_speed = ETH_SPEED_NUM_100M;
2290                 link.link_duplex = ETH_LINK_FULL_DUPLEX;
2291                 return rte_eth_linkstatus_set(dev, &link);
2292         }
2293
2294         if (link_up == 0) {
2295                 if (hw->phy.media_type == txgbe_media_type_fiber) {
2296                         intr->flags |= TXGBE_FLAG_NEED_LINK_CONFIG;
2297                         rte_eal_alarm_set(10,
2298                                 txgbe_dev_setup_link_alarm_handler, dev);
2299                 }
2300                 return rte_eth_linkstatus_set(dev, &link);
2301         }
2302
2303         intr->flags &= ~TXGBE_FLAG_NEED_LINK_CONFIG;
2304         link.link_status = ETH_LINK_UP;
2305         link.link_duplex = ETH_LINK_FULL_DUPLEX;
2306
2307         switch (link_speed) {
2308         default:
2309         case TXGBE_LINK_SPEED_UNKNOWN:
2310                 link.link_duplex = ETH_LINK_FULL_DUPLEX;
2311                 link.link_speed = ETH_SPEED_NUM_100M;
2312                 break;
2313
2314         case TXGBE_LINK_SPEED_100M_FULL:
2315                 link.link_speed = ETH_SPEED_NUM_100M;
2316                 break;
2317
2318         case TXGBE_LINK_SPEED_1GB_FULL:
2319                 link.link_speed = ETH_SPEED_NUM_1G;
2320                 break;
2321
2322         case TXGBE_LINK_SPEED_2_5GB_FULL:
2323                 link.link_speed = ETH_SPEED_NUM_2_5G;
2324                 break;
2325
2326         case TXGBE_LINK_SPEED_5GB_FULL:
2327                 link.link_speed = ETH_SPEED_NUM_5G;
2328                 break;
2329
2330         case TXGBE_LINK_SPEED_10GB_FULL:
2331                 link.link_speed = ETH_SPEED_NUM_10G;
2332                 break;
2333         }
2334
2335         return rte_eth_linkstatus_set(dev, &link);
2336 }
2337
2338 static int
2339 txgbe_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
2340 {
2341         return txgbe_dev_link_update_share(dev, wait_to_complete);
2342 }
2343
2344 /**
2345  * It clears the interrupt causes and enables the interrupt.
2346  * It will be called once only during nic initialized.
2347  *
2348  * @param dev
2349  *  Pointer to struct rte_eth_dev.
2350  * @param on
2351  *  Enable or Disable.
2352  *
2353  * @return
2354  *  - On success, zero.
2355  *  - On failure, a negative value.
2356  */
2357 static int
2358 txgbe_dev_lsc_interrupt_setup(struct rte_eth_dev *dev, uint8_t on)
2359 {
2360         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2361
2362         txgbe_dev_link_status_print(dev);
2363         if (on)
2364                 intr->mask_misc |= TXGBE_ICRMISC_LSC;
2365         else
2366                 intr->mask_misc &= ~TXGBE_ICRMISC_LSC;
2367
2368         return 0;
2369 }
2370
2371 /**
2372  * It clears the interrupt causes and enables the interrupt.
2373  * It will be called once only during nic initialized.
2374  *
2375  * @param dev
2376  *  Pointer to struct rte_eth_dev.
2377  *
2378  * @return
2379  *  - On success, zero.
2380  *  - On failure, a negative value.
2381  */
2382 static int
2383 txgbe_dev_rxq_interrupt_setup(struct rte_eth_dev *dev)
2384 {
2385         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2386
2387         intr->mask[0] |= TXGBE_ICR_MASK;
2388         intr->mask[1] |= TXGBE_ICR_MASK;
2389
2390         return 0;
2391 }
2392
2393 /**
2394  * It clears the interrupt causes and enables the interrupt.
2395  * It will be called once only during nic initialized.
2396  *
2397  * @param dev
2398  *  Pointer to struct rte_eth_dev.
2399  *
2400  * @return
2401  *  - On success, zero.
2402  *  - On failure, a negative value.
2403  */
2404 static int
2405 txgbe_dev_macsec_interrupt_setup(struct rte_eth_dev *dev)
2406 {
2407         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2408
2409         intr->mask_misc |= TXGBE_ICRMISC_LNKSEC;
2410
2411         return 0;
2412 }
2413
2414 /*
2415  * It reads ICR and sets flag (TXGBE_ICRMISC_LSC) for the link_update.
2416  *
2417  * @param dev
2418  *  Pointer to struct rte_eth_dev.
2419  *
2420  * @return
2421  *  - On success, zero.
2422  *  - On failure, a negative value.
2423  */
2424 static int
2425 txgbe_dev_interrupt_get_status(struct rte_eth_dev *dev)
2426 {
2427         uint32_t eicr;
2428         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2429         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2430
2431         /* clear all cause mask */
2432         txgbe_disable_intr(hw);
2433
2434         /* read-on-clear nic registers here */
2435         eicr = ((u32 *)hw->isb_mem)[TXGBE_ISB_MISC];
2436         PMD_DRV_LOG(DEBUG, "eicr %x", eicr);
2437
2438         intr->flags = 0;
2439
2440         /* set flag for async link update */
2441         if (eicr & TXGBE_ICRMISC_LSC)
2442                 intr->flags |= TXGBE_FLAG_NEED_LINK_UPDATE;
2443
2444         if (eicr & TXGBE_ICRMISC_VFMBX)
2445                 intr->flags |= TXGBE_FLAG_MAILBOX;
2446
2447         if (eicr & TXGBE_ICRMISC_LNKSEC)
2448                 intr->flags |= TXGBE_FLAG_MACSEC;
2449
2450         if (eicr & TXGBE_ICRMISC_GPIO)
2451                 intr->flags |= TXGBE_FLAG_PHY_INTERRUPT;
2452
2453         return 0;
2454 }
2455
2456 /**
2457  * It gets and then prints the link status.
2458  *
2459  * @param dev
2460  *  Pointer to struct rte_eth_dev.
2461  *
2462  * @return
2463  *  - On success, zero.
2464  *  - On failure, a negative value.
2465  */
2466 static void
2467 txgbe_dev_link_status_print(struct rte_eth_dev *dev)
2468 {
2469         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2470         struct rte_eth_link link;
2471
2472         rte_eth_linkstatus_get(dev, &link);
2473
2474         if (link.link_status) {
2475                 PMD_INIT_LOG(INFO, "Port %d: Link Up - speed %u Mbps - %s",
2476                                         (int)(dev->data->port_id),
2477                                         (unsigned int)link.link_speed,
2478                         link.link_duplex == ETH_LINK_FULL_DUPLEX ?
2479                                         "full-duplex" : "half-duplex");
2480         } else {
2481                 PMD_INIT_LOG(INFO, " Port %d: Link Down",
2482                                 (int)(dev->data->port_id));
2483         }
2484         PMD_INIT_LOG(DEBUG, "PCI Address: " PCI_PRI_FMT,
2485                                 pci_dev->addr.domain,
2486                                 pci_dev->addr.bus,
2487                                 pci_dev->addr.devid,
2488                                 pci_dev->addr.function);
2489 }
2490
2491 /*
2492  * It executes link_update after knowing an interrupt occurred.
2493  *
2494  * @param dev
2495  *  Pointer to struct rte_eth_dev.
2496  *
2497  * @return
2498  *  - On success, zero.
2499  *  - On failure, a negative value.
2500  */
2501 static int
2502 txgbe_dev_interrupt_action(struct rte_eth_dev *dev,
2503                            struct rte_intr_handle *intr_handle)
2504 {
2505         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2506         int64_t timeout;
2507         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2508
2509         PMD_DRV_LOG(DEBUG, "intr action type %d", intr->flags);
2510
2511         if (intr->flags & TXGBE_FLAG_MAILBOX) {
2512                 txgbe_pf_mbx_process(dev);
2513                 intr->flags &= ~TXGBE_FLAG_MAILBOX;
2514         }
2515
2516         if (intr->flags & TXGBE_FLAG_PHY_INTERRUPT) {
2517                 hw->phy.handle_lasi(hw);
2518                 intr->flags &= ~TXGBE_FLAG_PHY_INTERRUPT;
2519         }
2520
2521         if (intr->flags & TXGBE_FLAG_NEED_LINK_UPDATE) {
2522                 struct rte_eth_link link;
2523
2524                 /*get the link status before link update, for predicting later*/
2525                 rte_eth_linkstatus_get(dev, &link);
2526
2527                 txgbe_dev_link_update(dev, 0);
2528
2529                 /* likely to up */
2530                 if (!link.link_status)
2531                         /* handle it 1 sec later, wait it being stable */
2532                         timeout = TXGBE_LINK_UP_CHECK_TIMEOUT;
2533                 /* likely to down */
2534                 else
2535                         /* handle it 4 sec later, wait it being stable */
2536                         timeout = TXGBE_LINK_DOWN_CHECK_TIMEOUT;
2537
2538                 txgbe_dev_link_status_print(dev);
2539                 if (rte_eal_alarm_set(timeout * 1000,
2540                                       txgbe_dev_interrupt_delayed_handler,
2541                                       (void *)dev) < 0) {
2542                         PMD_DRV_LOG(ERR, "Error setting alarm");
2543                 } else {
2544                         /* remember original mask */
2545                         intr->mask_misc_orig = intr->mask_misc;
2546                         /* only disable lsc interrupt */
2547                         intr->mask_misc &= ~TXGBE_ICRMISC_LSC;
2548                 }
2549         }
2550
2551         PMD_DRV_LOG(DEBUG, "enable intr immediately");
2552         txgbe_enable_intr(dev);
2553         rte_intr_enable(intr_handle);
2554
2555         return 0;
2556 }
2557
2558 /**
2559  * Interrupt handler which shall be registered for alarm callback for delayed
2560  * handling specific interrupt to wait for the stable nic state. As the
2561  * NIC interrupt state is not stable for txgbe after link is just down,
2562  * it needs to wait 4 seconds to get the stable status.
2563  *
2564  * @param handle
2565  *  Pointer to interrupt handle.
2566  * @param param
2567  *  The address of parameter (struct rte_eth_dev *) registered before.
2568  *
2569  * @return
2570  *  void
2571  */
2572 static void
2573 txgbe_dev_interrupt_delayed_handler(void *param)
2574 {
2575         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2576         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2577         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2578         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2579         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2580         uint32_t eicr;
2581
2582         txgbe_disable_intr(hw);
2583
2584         eicr = ((u32 *)hw->isb_mem)[TXGBE_ISB_MISC];
2585         if (eicr & TXGBE_ICRMISC_VFMBX)
2586                 txgbe_pf_mbx_process(dev);
2587
2588         if (intr->flags & TXGBE_FLAG_PHY_INTERRUPT) {
2589                 hw->phy.handle_lasi(hw);
2590                 intr->flags &= ~TXGBE_FLAG_PHY_INTERRUPT;
2591         }
2592
2593         if (intr->flags & TXGBE_FLAG_NEED_LINK_UPDATE) {
2594                 txgbe_dev_link_update(dev, 0);
2595                 intr->flags &= ~TXGBE_FLAG_NEED_LINK_UPDATE;
2596                 txgbe_dev_link_status_print(dev);
2597                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC,
2598                                               NULL);
2599         }
2600
2601         if (intr->flags & TXGBE_FLAG_MACSEC) {
2602                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_MACSEC,
2603                                               NULL);
2604                 intr->flags &= ~TXGBE_FLAG_MACSEC;
2605         }
2606
2607         /* restore original mask */
2608         intr->mask_misc = intr->mask_misc_orig;
2609         intr->mask_misc_orig = 0;
2610
2611         PMD_DRV_LOG(DEBUG, "enable intr in delayed handler S[%08x]", eicr);
2612         txgbe_enable_intr(dev);
2613         rte_intr_enable(intr_handle);
2614 }
2615
2616 /**
2617  * Interrupt handler triggered by NIC  for handling
2618  * specific interrupt.
2619  *
2620  * @param handle
2621  *  Pointer to interrupt handle.
2622  * @param param
2623  *  The address of parameter (struct rte_eth_dev *) registered before.
2624  *
2625  * @return
2626  *  void
2627  */
2628 static void
2629 txgbe_dev_interrupt_handler(void *param)
2630 {
2631         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2632
2633         txgbe_dev_interrupt_get_status(dev);
2634         txgbe_dev_interrupt_action(dev, dev->intr_handle);
2635 }
2636
2637 int
2638 txgbe_dev_rss_reta_update(struct rte_eth_dev *dev,
2639                           struct rte_eth_rss_reta_entry64 *reta_conf,
2640                           uint16_t reta_size)
2641 {
2642         uint8_t i, j, mask;
2643         uint32_t reta;
2644         uint16_t idx, shift;
2645         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
2646         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2647
2648         PMD_INIT_FUNC_TRACE();
2649
2650         if (!txgbe_rss_update_sp(hw->mac.type)) {
2651                 PMD_DRV_LOG(ERR, "RSS reta update is not supported on this "
2652                         "NIC.");
2653                 return -ENOTSUP;
2654         }
2655
2656         if (reta_size != ETH_RSS_RETA_SIZE_128) {
2657                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
2658                         "(%d) doesn't match the number hardware can supported "
2659                         "(%d)", reta_size, ETH_RSS_RETA_SIZE_128);
2660                 return -EINVAL;
2661         }
2662
2663         for (i = 0; i < reta_size; i += 4) {
2664                 idx = i / RTE_RETA_GROUP_SIZE;
2665                 shift = i % RTE_RETA_GROUP_SIZE;
2666                 mask = (uint8_t)RS64(reta_conf[idx].mask, shift, 0xF);
2667                 if (!mask)
2668                         continue;
2669
2670                 reta = rd32a(hw, TXGBE_REG_RSSTBL, i >> 2);
2671                 for (j = 0; j < 4; j++) {
2672                         if (RS8(mask, j, 0x1)) {
2673                                 reta  &= ~(MS32(8 * j, 0xFF));
2674                                 reta |= LS32(reta_conf[idx].reta[shift + j],
2675                                                 8 * j, 0xFF);
2676                         }
2677                 }
2678                 wr32a(hw, TXGBE_REG_RSSTBL, i >> 2, reta);
2679         }
2680         adapter->rss_reta_updated = 1;
2681
2682         return 0;
2683 }
2684
2685 int
2686 txgbe_dev_rss_reta_query(struct rte_eth_dev *dev,
2687                          struct rte_eth_rss_reta_entry64 *reta_conf,
2688                          uint16_t reta_size)
2689 {
2690         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2691         uint8_t i, j, mask;
2692         uint32_t reta;
2693         uint16_t idx, shift;
2694
2695         PMD_INIT_FUNC_TRACE();
2696
2697         if (reta_size != ETH_RSS_RETA_SIZE_128) {
2698                 PMD_DRV_LOG(ERR, "The size of hash lookup table configured "
2699                         "(%d) doesn't match the number hardware can supported "
2700                         "(%d)", reta_size, ETH_RSS_RETA_SIZE_128);
2701                 return -EINVAL;
2702         }
2703
2704         for (i = 0; i < reta_size; i += 4) {
2705                 idx = i / RTE_RETA_GROUP_SIZE;
2706                 shift = i % RTE_RETA_GROUP_SIZE;
2707                 mask = (uint8_t)RS64(reta_conf[idx].mask, shift, 0xF);
2708                 if (!mask)
2709                         continue;
2710
2711                 reta = rd32a(hw, TXGBE_REG_RSSTBL, i >> 2);
2712                 for (j = 0; j < 4; j++) {
2713                         if (RS8(mask, j, 0x1))
2714                                 reta_conf[idx].reta[shift + j] =
2715                                         (uint16_t)RS32(reta, 8 * j, 0xFF);
2716                 }
2717         }
2718
2719         return 0;
2720 }
2721
2722 static int
2723 txgbe_add_rar(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
2724                                 uint32_t index, uint32_t pool)
2725 {
2726         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2727         uint32_t enable_addr = 1;
2728
2729         return txgbe_set_rar(hw, index, mac_addr->addr_bytes,
2730                              pool, enable_addr);
2731 }
2732
2733 static void
2734 txgbe_remove_rar(struct rte_eth_dev *dev, uint32_t index)
2735 {
2736         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2737
2738         txgbe_clear_rar(hw, index);
2739 }
2740
2741 static int
2742 txgbe_set_default_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *addr)
2743 {
2744         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2745
2746         txgbe_remove_rar(dev, 0);
2747         txgbe_add_rar(dev, addr, 0, pci_dev->max_vfs);
2748
2749         return 0;
2750 }
2751
2752 static uint32_t
2753 txgbe_uta_vector(struct txgbe_hw *hw, struct rte_ether_addr *uc_addr)
2754 {
2755         uint32_t vector = 0;
2756
2757         switch (hw->mac.mc_filter_type) {
2758         case 0:   /* use bits [47:36] of the address */
2759                 vector = ((uc_addr->addr_bytes[4] >> 4) |
2760                         (((uint16_t)uc_addr->addr_bytes[5]) << 4));
2761                 break;
2762         case 1:   /* use bits [46:35] of the address */
2763                 vector = ((uc_addr->addr_bytes[4] >> 3) |
2764                         (((uint16_t)uc_addr->addr_bytes[5]) << 5));
2765                 break;
2766         case 2:   /* use bits [45:34] of the address */
2767                 vector = ((uc_addr->addr_bytes[4] >> 2) |
2768                         (((uint16_t)uc_addr->addr_bytes[5]) << 6));
2769                 break;
2770         case 3:   /* use bits [43:32] of the address */
2771                 vector = ((uc_addr->addr_bytes[4]) |
2772                         (((uint16_t)uc_addr->addr_bytes[5]) << 8));
2773                 break;
2774         default:  /* Invalid mc_filter_type */
2775                 break;
2776         }
2777
2778         /* vector can only be 12-bits or boundary will be exceeded */
2779         vector &= 0xFFF;
2780         return vector;
2781 }
2782
2783 static int
2784 txgbe_uc_hash_table_set(struct rte_eth_dev *dev,
2785                         struct rte_ether_addr *mac_addr, uint8_t on)
2786 {
2787         uint32_t vector;
2788         uint32_t uta_idx;
2789         uint32_t reg_val;
2790         uint32_t uta_mask;
2791         uint32_t psrctl;
2792
2793         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2794         struct txgbe_uta_info *uta_info = TXGBE_DEV_UTA_INFO(dev);
2795
2796         /* The UTA table only exists on pf hardware */
2797         if (hw->mac.type < txgbe_mac_raptor)
2798                 return -ENOTSUP;
2799
2800         vector = txgbe_uta_vector(hw, mac_addr);
2801         uta_idx = (vector >> 5) & 0x7F;
2802         uta_mask = 0x1UL << (vector & 0x1F);
2803
2804         if (!!on == !!(uta_info->uta_shadow[uta_idx] & uta_mask))
2805                 return 0;
2806
2807         reg_val = rd32(hw, TXGBE_UCADDRTBL(uta_idx));
2808         if (on) {
2809                 uta_info->uta_in_use++;
2810                 reg_val |= uta_mask;
2811                 uta_info->uta_shadow[uta_idx] |= uta_mask;
2812         } else {
2813                 uta_info->uta_in_use--;
2814                 reg_val &= ~uta_mask;
2815                 uta_info->uta_shadow[uta_idx] &= ~uta_mask;
2816         }
2817
2818         wr32(hw, TXGBE_UCADDRTBL(uta_idx), reg_val);
2819
2820         psrctl = rd32(hw, TXGBE_PSRCTL);
2821         if (uta_info->uta_in_use > 0)
2822                 psrctl |= TXGBE_PSRCTL_UCHFENA;
2823         else
2824                 psrctl &= ~TXGBE_PSRCTL_UCHFENA;
2825
2826         psrctl &= ~TXGBE_PSRCTL_ADHF12_MASK;
2827         psrctl |= TXGBE_PSRCTL_ADHF12(hw->mac.mc_filter_type);
2828         wr32(hw, TXGBE_PSRCTL, psrctl);
2829
2830         return 0;
2831 }
2832
2833 static int
2834 txgbe_uc_all_hash_table_set(struct rte_eth_dev *dev, uint8_t on)
2835 {
2836         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2837         struct txgbe_uta_info *uta_info = TXGBE_DEV_UTA_INFO(dev);
2838         uint32_t psrctl;
2839         int i;
2840
2841         /* The UTA table only exists on pf hardware */
2842         if (hw->mac.type < txgbe_mac_raptor)
2843                 return -ENOTSUP;
2844
2845         if (on) {
2846                 for (i = 0; i < ETH_VMDQ_NUM_UC_HASH_ARRAY; i++) {
2847                         uta_info->uta_shadow[i] = ~0;
2848                         wr32(hw, TXGBE_UCADDRTBL(i), ~0);
2849                 }
2850         } else {
2851                 for (i = 0; i < ETH_VMDQ_NUM_UC_HASH_ARRAY; i++) {
2852                         uta_info->uta_shadow[i] = 0;
2853                         wr32(hw, TXGBE_UCADDRTBL(i), 0);
2854                 }
2855         }
2856
2857         psrctl = rd32(hw, TXGBE_PSRCTL);
2858         if (on)
2859                 psrctl |= TXGBE_PSRCTL_UCHFENA;
2860         else
2861                 psrctl &= ~TXGBE_PSRCTL_UCHFENA;
2862
2863         psrctl &= ~TXGBE_PSRCTL_ADHF12_MASK;
2864         psrctl |= TXGBE_PSRCTL_ADHF12(hw->mac.mc_filter_type);
2865         wr32(hw, TXGBE_PSRCTL, psrctl);
2866
2867         return 0;
2868 }
2869
2870 uint32_t
2871 txgbe_convert_vm_rx_mask_to_val(uint16_t rx_mask, uint32_t orig_val)
2872 {
2873         uint32_t new_val = orig_val;
2874
2875         if (rx_mask & ETH_VMDQ_ACCEPT_UNTAG)
2876                 new_val |= TXGBE_POOLETHCTL_UTA;
2877         if (rx_mask & ETH_VMDQ_ACCEPT_HASH_MC)
2878                 new_val |= TXGBE_POOLETHCTL_MCHA;
2879         if (rx_mask & ETH_VMDQ_ACCEPT_HASH_UC)
2880                 new_val |= TXGBE_POOLETHCTL_UCHA;
2881         if (rx_mask & ETH_VMDQ_ACCEPT_BROADCAST)
2882                 new_val |= TXGBE_POOLETHCTL_BCA;
2883         if (rx_mask & ETH_VMDQ_ACCEPT_MULTICAST)
2884                 new_val |= TXGBE_POOLETHCTL_MCP;
2885
2886         return new_val;
2887 }
2888
2889 static int
2890 txgbe_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
2891 {
2892         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2893         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2894         uint32_t mask;
2895         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2896
2897         if (queue_id < 32) {
2898                 mask = rd32(hw, TXGBE_IMS(0));
2899                 mask &= (1 << queue_id);
2900                 wr32(hw, TXGBE_IMS(0), mask);
2901         } else if (queue_id < 64) {
2902                 mask = rd32(hw, TXGBE_IMS(1));
2903                 mask &= (1 << (queue_id - 32));
2904                 wr32(hw, TXGBE_IMS(1), mask);
2905         }
2906         rte_intr_enable(intr_handle);
2907
2908         return 0;
2909 }
2910
2911 static int
2912 txgbe_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
2913 {
2914         uint32_t mask;
2915         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2916
2917         if (queue_id < 32) {
2918                 mask = rd32(hw, TXGBE_IMS(0));
2919                 mask &= ~(1 << queue_id);
2920                 wr32(hw, TXGBE_IMS(0), mask);
2921         } else if (queue_id < 64) {
2922                 mask = rd32(hw, TXGBE_IMS(1));
2923                 mask &= ~(1 << (queue_id - 32));
2924                 wr32(hw, TXGBE_IMS(1), mask);
2925         }
2926
2927         return 0;
2928 }
2929
2930 /**
2931  * set the IVAR registers, mapping interrupt causes to vectors
2932  * @param hw
2933  *  pointer to txgbe_hw struct
2934  * @direction
2935  *  0 for Rx, 1 for Tx, -1 for other causes
2936  * @queue
2937  *  queue to map the corresponding interrupt to
2938  * @msix_vector
2939  *  the vector to map to the corresponding queue
2940  */
2941 void
2942 txgbe_set_ivar_map(struct txgbe_hw *hw, int8_t direction,
2943                    uint8_t queue, uint8_t msix_vector)
2944 {
2945         uint32_t tmp, idx;
2946
2947         if (direction == -1) {
2948                 /* other causes */
2949                 msix_vector |= TXGBE_IVARMISC_VLD;
2950                 idx = 0;
2951                 tmp = rd32(hw, TXGBE_IVARMISC);
2952                 tmp &= ~(0xFF << idx);
2953                 tmp |= (msix_vector << idx);
2954                 wr32(hw, TXGBE_IVARMISC, tmp);
2955         } else {
2956                 /* rx or tx causes */
2957                 /* Workround for ICR lost */
2958                 idx = ((16 * (queue & 1)) + (8 * direction));
2959                 tmp = rd32(hw, TXGBE_IVAR(queue >> 1));
2960                 tmp &= ~(0xFF << idx);
2961                 tmp |= (msix_vector << idx);
2962                 wr32(hw, TXGBE_IVAR(queue >> 1), tmp);
2963         }
2964 }
2965
2966 /**
2967  * Sets up the hardware to properly generate MSI-X interrupts
2968  * @hw
2969  *  board private structure
2970  */
2971 static void
2972 txgbe_configure_msix(struct rte_eth_dev *dev)
2973 {
2974         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2975         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2976         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2977         uint32_t queue_id, base = TXGBE_MISC_VEC_ID;
2978         uint32_t vec = TXGBE_MISC_VEC_ID;
2979         uint32_t gpie;
2980
2981         /* won't configure msix register if no mapping is done
2982          * between intr vector and event fd
2983          * but if misx has been enabled already, need to configure
2984          * auto clean, auto mask and throttling.
2985          */
2986         gpie = rd32(hw, TXGBE_GPIE);
2987         if (!rte_intr_dp_is_en(intr_handle) &&
2988             !(gpie & TXGBE_GPIE_MSIX))
2989                 return;
2990
2991         if (rte_intr_allow_others(intr_handle)) {
2992                 base = TXGBE_RX_VEC_START;
2993                 vec = base;
2994         }
2995
2996         /* setup GPIE for MSI-x mode */
2997         gpie = rd32(hw, TXGBE_GPIE);
2998         gpie |= TXGBE_GPIE_MSIX;
2999         wr32(hw, TXGBE_GPIE, gpie);
3000
3001         /* Populate the IVAR table and set the ITR values to the
3002          * corresponding register.
3003          */
3004         if (rte_intr_dp_is_en(intr_handle)) {
3005                 for (queue_id = 0; queue_id < dev->data->nb_rx_queues;
3006                         queue_id++) {
3007                         /* by default, 1:1 mapping */
3008                         txgbe_set_ivar_map(hw, 0, queue_id, vec);
3009                         intr_handle->intr_vec[queue_id] = vec;
3010                         if (vec < base + intr_handle->nb_efd - 1)
3011                                 vec++;
3012                 }
3013
3014                 txgbe_set_ivar_map(hw, -1, 1, TXGBE_MISC_VEC_ID);
3015         }
3016         wr32(hw, TXGBE_ITR(TXGBE_MISC_VEC_ID),
3017                         TXGBE_ITR_IVAL_10G(TXGBE_QUEUE_ITR_INTERVAL_DEFAULT)
3018                         | TXGBE_ITR_WRDSA);
3019 }
3020
3021 int
3022 txgbe_set_queue_rate_limit(struct rte_eth_dev *dev,
3023                            uint16_t queue_idx, uint16_t tx_rate)
3024 {
3025         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
3026         uint32_t bcnrc_val;
3027
3028         if (queue_idx >= hw->mac.max_tx_queues)
3029                 return -EINVAL;
3030
3031         if (tx_rate != 0) {
3032                 bcnrc_val = TXGBE_ARBTXRATE_MAX(tx_rate);
3033                 bcnrc_val |= TXGBE_ARBTXRATE_MIN(tx_rate / 2);
3034         } else {
3035                 bcnrc_val = 0;
3036         }
3037
3038         /*
3039          * Set global transmit compensation time to the MMW_SIZE in ARBTXMMW
3040          * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported.
3041          */
3042         wr32(hw, TXGBE_ARBTXMMW, 0x14);
3043
3044         /* Set ARBTXRATE of queue X */
3045         wr32(hw, TXGBE_ARBPOOLIDX, queue_idx);
3046         wr32(hw, TXGBE_ARBTXRATE, bcnrc_val);
3047         txgbe_flush(hw);
3048
3049         return 0;
3050 }
3051
3052 static u8 *
3053 txgbe_dev_addr_list_itr(__rte_unused struct txgbe_hw *hw,
3054                         u8 **mc_addr_ptr, u32 *vmdq)
3055 {
3056         u8 *mc_addr;
3057
3058         *vmdq = 0;
3059         mc_addr = *mc_addr_ptr;
3060         *mc_addr_ptr = (mc_addr + sizeof(struct rte_ether_addr));
3061         return mc_addr;
3062 }
3063
3064 int
3065 txgbe_dev_set_mc_addr_list(struct rte_eth_dev *dev,
3066                           struct rte_ether_addr *mc_addr_set,
3067                           uint32_t nb_mc_addr)
3068 {
3069         struct txgbe_hw *hw;
3070         u8 *mc_addr_list;
3071
3072         hw = TXGBE_DEV_HW(dev);
3073         mc_addr_list = (u8 *)mc_addr_set;
3074         return txgbe_update_mc_addr_list(hw, mc_addr_list, nb_mc_addr,
3075                                          txgbe_dev_addr_list_itr, TRUE);
3076 }
3077
3078 bool
3079 txgbe_rss_update_sp(enum txgbe_mac_type mac_type)
3080 {
3081         switch (mac_type) {
3082         case txgbe_mac_raptor:
3083                 return 1;
3084         default:
3085                 return 0;
3086         }
3087 }
3088
3089 static const struct eth_dev_ops txgbe_eth_dev_ops = {
3090         .dev_configure              = txgbe_dev_configure,
3091         .dev_infos_get              = txgbe_dev_info_get,
3092         .dev_start                  = txgbe_dev_start,
3093         .dev_stop                   = txgbe_dev_stop,
3094         .dev_set_link_up            = txgbe_dev_set_link_up,
3095         .dev_set_link_down          = txgbe_dev_set_link_down,
3096         .dev_close                  = txgbe_dev_close,
3097         .dev_reset                  = txgbe_dev_reset,
3098         .link_update                = txgbe_dev_link_update,
3099         .stats_get                  = txgbe_dev_stats_get,
3100         .xstats_get                 = txgbe_dev_xstats_get,
3101         .xstats_get_by_id           = txgbe_dev_xstats_get_by_id,
3102         .stats_reset                = txgbe_dev_stats_reset,
3103         .xstats_reset               = txgbe_dev_xstats_reset,
3104         .xstats_get_names           = txgbe_dev_xstats_get_names,
3105         .xstats_get_names_by_id     = txgbe_dev_xstats_get_names_by_id,
3106         .queue_stats_mapping_set    = txgbe_dev_queue_stats_mapping_set,
3107         .dev_supported_ptypes_get   = txgbe_dev_supported_ptypes_get,
3108         .vlan_filter_set            = txgbe_vlan_filter_set,
3109         .vlan_tpid_set              = txgbe_vlan_tpid_set,
3110         .vlan_offload_set           = txgbe_vlan_offload_set,
3111         .vlan_strip_queue_set       = txgbe_vlan_strip_queue_set,
3112         .rx_queue_start             = txgbe_dev_rx_queue_start,
3113         .rx_queue_stop              = txgbe_dev_rx_queue_stop,
3114         .tx_queue_start             = txgbe_dev_tx_queue_start,
3115         .tx_queue_stop              = txgbe_dev_tx_queue_stop,
3116         .rx_queue_setup             = txgbe_dev_rx_queue_setup,
3117         .rx_queue_intr_enable       = txgbe_dev_rx_queue_intr_enable,
3118         .rx_queue_intr_disable      = txgbe_dev_rx_queue_intr_disable,
3119         .rx_queue_release           = txgbe_dev_rx_queue_release,
3120         .tx_queue_setup             = txgbe_dev_tx_queue_setup,
3121         .tx_queue_release           = txgbe_dev_tx_queue_release,
3122         .mac_addr_add               = txgbe_add_rar,
3123         .mac_addr_remove            = txgbe_remove_rar,
3124         .mac_addr_set               = txgbe_set_default_mac_addr,
3125         .uc_hash_table_set          = txgbe_uc_hash_table_set,
3126         .uc_all_hash_table_set      = txgbe_uc_all_hash_table_set,
3127         .set_queue_rate_limit       = txgbe_set_queue_rate_limit,
3128         .reta_update                = txgbe_dev_rss_reta_update,
3129         .reta_query                 = txgbe_dev_rss_reta_query,
3130         .rss_hash_update            = txgbe_dev_rss_hash_update,
3131         .rss_hash_conf_get          = txgbe_dev_rss_hash_conf_get,
3132         .set_mc_addr_list           = txgbe_dev_set_mc_addr_list,
3133         .rxq_info_get               = txgbe_rxq_info_get,
3134         .txq_info_get               = txgbe_txq_info_get,
3135 };
3136
3137 RTE_PMD_REGISTER_PCI(net_txgbe, rte_txgbe_pmd);
3138 RTE_PMD_REGISTER_PCI_TABLE(net_txgbe, pci_id_txgbe_map);
3139 RTE_PMD_REGISTER_KMOD_DEP(net_txgbe, "* igb_uio | uio_pci_generic | vfio-pci");
3140
3141 RTE_LOG_REGISTER(txgbe_logtype_init, pmd.net.txgbe.init, NOTICE);
3142 RTE_LOG_REGISTER(txgbe_logtype_driver, pmd.net.txgbe.driver, NOTICE);
3143
3144 #ifdef RTE_LIBRTE_TXGBE_DEBUG_RX
3145         RTE_LOG_REGISTER(txgbe_logtype_rx, pmd.net.txgbe.rx, DEBUG);
3146 #endif
3147 #ifdef RTE_LIBRTE_TXGBE_DEBUG_TX
3148         RTE_LOG_REGISTER(txgbe_logtype_tx, pmd.net.txgbe.tx, DEBUG);
3149 #endif
3150
3151 #ifdef RTE_LIBRTE_TXGBE_DEBUG_TX_FREE
3152         RTE_LOG_REGISTER(txgbe_logtype_tx_free, pmd.net.txgbe.tx_free, DEBUG);
3153 #endif