6e2f0f72f5ddae74b56990d4c40418dc2b5d2479
[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 int
947 txgbe_check_vf_rss_rxq_num(struct rte_eth_dev *dev, uint16_t nb_rx_q)
948 {
949         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
950
951         switch (nb_rx_q) {
952         case 1:
953         case 2:
954                 RTE_ETH_DEV_SRIOV(dev).active = ETH_64_POOLS;
955                 break;
956         case 4:
957                 RTE_ETH_DEV_SRIOV(dev).active = ETH_32_POOLS;
958                 break;
959         default:
960                 return -EINVAL;
961         }
962
963         RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool =
964                 TXGBE_MAX_RX_QUEUE_NUM / RTE_ETH_DEV_SRIOV(dev).active;
965         RTE_ETH_DEV_SRIOV(dev).def_pool_q_idx =
966                 pci_dev->max_vfs * RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
967         return 0;
968 }
969
970 static int
971 txgbe_check_mq_mode(struct rte_eth_dev *dev)
972 {
973         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
974         uint16_t nb_rx_q = dev->data->nb_rx_queues;
975         uint16_t nb_tx_q = dev->data->nb_tx_queues;
976
977         if (RTE_ETH_DEV_SRIOV(dev).active != 0) {
978                 /* check multi-queue mode */
979                 switch (dev_conf->rxmode.mq_mode) {
980                 case ETH_MQ_RX_VMDQ_DCB:
981                         PMD_INIT_LOG(INFO, "ETH_MQ_RX_VMDQ_DCB mode supported in SRIOV");
982                         break;
983                 case ETH_MQ_RX_VMDQ_DCB_RSS:
984                         /* DCB/RSS VMDQ in SRIOV mode, not implement yet */
985                         PMD_INIT_LOG(ERR, "SRIOV active,"
986                                         " unsupported mq_mode rx %d.",
987                                         dev_conf->rxmode.mq_mode);
988                         return -EINVAL;
989                 case ETH_MQ_RX_RSS:
990                 case ETH_MQ_RX_VMDQ_RSS:
991                         dev->data->dev_conf.rxmode.mq_mode = ETH_MQ_RX_VMDQ_RSS;
992                         if (nb_rx_q <= RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool)
993                                 if (txgbe_check_vf_rss_rxq_num(dev, nb_rx_q)) {
994                                         PMD_INIT_LOG(ERR, "SRIOV is active,"
995                                                 " invalid queue number"
996                                                 " for VMDQ RSS, allowed"
997                                                 " value are 1, 2 or 4.");
998                                         return -EINVAL;
999                                 }
1000                         break;
1001                 case ETH_MQ_RX_VMDQ_ONLY:
1002                 case ETH_MQ_RX_NONE:
1003                         /* if nothing mq mode configure, use default scheme */
1004                         dev->data->dev_conf.rxmode.mq_mode =
1005                                 ETH_MQ_RX_VMDQ_ONLY;
1006                         break;
1007                 default: /* ETH_MQ_RX_DCB, ETH_MQ_RX_DCB_RSS or ETH_MQ_TX_DCB*/
1008                         /* SRIOV only works in VMDq enable mode */
1009                         PMD_INIT_LOG(ERR, "SRIOV is active,"
1010                                         " wrong mq_mode rx %d.",
1011                                         dev_conf->rxmode.mq_mode);
1012                         return -EINVAL;
1013                 }
1014
1015                 switch (dev_conf->txmode.mq_mode) {
1016                 case ETH_MQ_TX_VMDQ_DCB:
1017                         PMD_INIT_LOG(INFO, "ETH_MQ_TX_VMDQ_DCB mode supported in SRIOV");
1018                         dev->data->dev_conf.txmode.mq_mode = ETH_MQ_TX_VMDQ_DCB;
1019                         break;
1020                 default: /* ETH_MQ_TX_VMDQ_ONLY or ETH_MQ_TX_NONE */
1021                         dev->data->dev_conf.txmode.mq_mode =
1022                                 ETH_MQ_TX_VMDQ_ONLY;
1023                         break;
1024                 }
1025
1026                 /* check valid queue number */
1027                 if ((nb_rx_q > RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool) ||
1028                     (nb_tx_q > RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool)) {
1029                         PMD_INIT_LOG(ERR, "SRIOV is active,"
1030                                         " nb_rx_q=%d nb_tx_q=%d queue number"
1031                                         " must be less than or equal to %d.",
1032                                         nb_rx_q, nb_tx_q,
1033                                         RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool);
1034                         return -EINVAL;
1035                 }
1036         } else {
1037                 if (dev_conf->rxmode.mq_mode == ETH_MQ_RX_VMDQ_DCB_RSS) {
1038                         PMD_INIT_LOG(ERR, "VMDQ+DCB+RSS mq_mode is"
1039                                           " not supported.");
1040                         return -EINVAL;
1041                 }
1042                 /* check configuration for vmdb+dcb mode */
1043                 if (dev_conf->rxmode.mq_mode == ETH_MQ_RX_VMDQ_DCB) {
1044                         const struct rte_eth_vmdq_dcb_conf *conf;
1045
1046                         if (nb_rx_q != TXGBE_VMDQ_DCB_NB_QUEUES) {
1047                                 PMD_INIT_LOG(ERR, "VMDQ+DCB, nb_rx_q != %d.",
1048                                                 TXGBE_VMDQ_DCB_NB_QUEUES);
1049                                 return -EINVAL;
1050                         }
1051                         conf = &dev_conf->rx_adv_conf.vmdq_dcb_conf;
1052                         if (!(conf->nb_queue_pools == ETH_16_POOLS ||
1053                                conf->nb_queue_pools == ETH_32_POOLS)) {
1054                                 PMD_INIT_LOG(ERR, "VMDQ+DCB selected,"
1055                                                 " nb_queue_pools must be %d or %d.",
1056                                                 ETH_16_POOLS, ETH_32_POOLS);
1057                                 return -EINVAL;
1058                         }
1059                 }
1060                 if (dev_conf->txmode.mq_mode == ETH_MQ_TX_VMDQ_DCB) {
1061                         const struct rte_eth_vmdq_dcb_tx_conf *conf;
1062
1063                         if (nb_tx_q != TXGBE_VMDQ_DCB_NB_QUEUES) {
1064                                 PMD_INIT_LOG(ERR, "VMDQ+DCB, nb_tx_q != %d",
1065                                                  TXGBE_VMDQ_DCB_NB_QUEUES);
1066                                 return -EINVAL;
1067                         }
1068                         conf = &dev_conf->tx_adv_conf.vmdq_dcb_tx_conf;
1069                         if (!(conf->nb_queue_pools == ETH_16_POOLS ||
1070                                conf->nb_queue_pools == ETH_32_POOLS)) {
1071                                 PMD_INIT_LOG(ERR, "VMDQ+DCB selected,"
1072                                                 " nb_queue_pools != %d and"
1073                                                 " nb_queue_pools != %d.",
1074                                                 ETH_16_POOLS, ETH_32_POOLS);
1075                                 return -EINVAL;
1076                         }
1077                 }
1078
1079                 /* For DCB mode check our configuration before we go further */
1080                 if (dev_conf->rxmode.mq_mode == ETH_MQ_RX_DCB) {
1081                         const struct rte_eth_dcb_rx_conf *conf;
1082
1083                         conf = &dev_conf->rx_adv_conf.dcb_rx_conf;
1084                         if (!(conf->nb_tcs == ETH_4_TCS ||
1085                                conf->nb_tcs == ETH_8_TCS)) {
1086                                 PMD_INIT_LOG(ERR, "DCB selected, nb_tcs != %d"
1087                                                 " and nb_tcs != %d.",
1088                                                 ETH_4_TCS, ETH_8_TCS);
1089                                 return -EINVAL;
1090                         }
1091                 }
1092
1093                 if (dev_conf->txmode.mq_mode == ETH_MQ_TX_DCB) {
1094                         const struct rte_eth_dcb_tx_conf *conf;
1095
1096                         conf = &dev_conf->tx_adv_conf.dcb_tx_conf;
1097                         if (!(conf->nb_tcs == ETH_4_TCS ||
1098                                conf->nb_tcs == ETH_8_TCS)) {
1099                                 PMD_INIT_LOG(ERR, "DCB selected, nb_tcs != %d"
1100                                                 " and nb_tcs != %d.",
1101                                                 ETH_4_TCS, ETH_8_TCS);
1102                                 return -EINVAL;
1103                         }
1104                 }
1105         }
1106         return 0;
1107 }
1108
1109 static int
1110 txgbe_dev_configure(struct rte_eth_dev *dev)
1111 {
1112         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
1113         struct txgbe_adapter *adapter = TXGBE_DEV_ADAPTER(dev);
1114         int ret;
1115
1116         PMD_INIT_FUNC_TRACE();
1117
1118         if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
1119                 dev->data->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
1120
1121         /* multiple queue mode checking */
1122         ret  = txgbe_check_mq_mode(dev);
1123         if (ret != 0) {
1124                 PMD_DRV_LOG(ERR, "txgbe_check_mq_mode fails with %d.",
1125                             ret);
1126                 return ret;
1127         }
1128
1129         /* set flag to update link status after init */
1130         intr->flags |= TXGBE_FLAG_NEED_LINK_UPDATE;
1131
1132         /*
1133          * Initialize to TRUE. If any of Rx queues doesn't meet the bulk
1134          * allocation Rx preconditions we will reset it.
1135          */
1136         adapter->rx_bulk_alloc_allowed = true;
1137
1138         return 0;
1139 }
1140
1141 static void
1142 txgbe_dev_phy_intr_setup(struct rte_eth_dev *dev)
1143 {
1144         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1145         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
1146         uint32_t gpie;
1147
1148         gpie = rd32(hw, TXGBE_GPIOINTEN);
1149         gpie |= TXGBE_GPIOBIT_6;
1150         wr32(hw, TXGBE_GPIOINTEN, gpie);
1151         intr->mask_misc |= TXGBE_ICRMISC_GPIO;
1152 }
1153
1154 int
1155 txgbe_set_vf_rate_limit(struct rte_eth_dev *dev, uint16_t vf,
1156                         uint16_t tx_rate, uint64_t q_msk)
1157 {
1158         struct txgbe_hw *hw;
1159         struct txgbe_vf_info *vfinfo;
1160         struct rte_eth_link link;
1161         uint8_t  nb_q_per_pool;
1162         uint32_t queue_stride;
1163         uint32_t queue_idx, idx = 0, vf_idx;
1164         uint32_t queue_end;
1165         uint16_t total_rate = 0;
1166         struct rte_pci_device *pci_dev;
1167         int ret;
1168
1169         pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1170         ret = rte_eth_link_get_nowait(dev->data->port_id, &link);
1171         if (ret < 0)
1172                 return ret;
1173
1174         if (vf >= pci_dev->max_vfs)
1175                 return -EINVAL;
1176
1177         if (tx_rate > link.link_speed)
1178                 return -EINVAL;
1179
1180         if (q_msk == 0)
1181                 return 0;
1182
1183         hw = TXGBE_DEV_HW(dev);
1184         vfinfo = *(TXGBE_DEV_VFDATA(dev));
1185         nb_q_per_pool = RTE_ETH_DEV_SRIOV(dev).nb_q_per_pool;
1186         queue_stride = TXGBE_MAX_RX_QUEUE_NUM / RTE_ETH_DEV_SRIOV(dev).active;
1187         queue_idx = vf * queue_stride;
1188         queue_end = queue_idx + nb_q_per_pool - 1;
1189         if (queue_end >= hw->mac.max_tx_queues)
1190                 return -EINVAL;
1191
1192         if (vfinfo) {
1193                 for (vf_idx = 0; vf_idx < pci_dev->max_vfs; vf_idx++) {
1194                         if (vf_idx == vf)
1195                                 continue;
1196                         for (idx = 0; idx < RTE_DIM(vfinfo[vf_idx].tx_rate);
1197                                 idx++)
1198                                 total_rate += vfinfo[vf_idx].tx_rate[idx];
1199                 }
1200         } else {
1201                 return -EINVAL;
1202         }
1203
1204         /* Store tx_rate for this vf. */
1205         for (idx = 0; idx < nb_q_per_pool; idx++) {
1206                 if (((uint64_t)0x1 << idx) & q_msk) {
1207                         if (vfinfo[vf].tx_rate[idx] != tx_rate)
1208                                 vfinfo[vf].tx_rate[idx] = tx_rate;
1209                         total_rate += tx_rate;
1210                 }
1211         }
1212
1213         if (total_rate > dev->data->dev_link.link_speed) {
1214                 /* Reset stored TX rate of the VF if it causes exceed
1215                  * link speed.
1216                  */
1217                 memset(vfinfo[vf].tx_rate, 0, sizeof(vfinfo[vf].tx_rate));
1218                 return -EINVAL;
1219         }
1220
1221         /* Set ARBTXRATE of each queue/pool for vf X  */
1222         for (; queue_idx <= queue_end; queue_idx++) {
1223                 if (0x1 & q_msk)
1224                         txgbe_set_queue_rate_limit(dev, queue_idx, tx_rate);
1225                 q_msk = q_msk >> 1;
1226         }
1227
1228         return 0;
1229 }
1230
1231 /*
1232  * Configure device link speed and setup link.
1233  * It returns 0 on success.
1234  */
1235 static int
1236 txgbe_dev_start(struct rte_eth_dev *dev)
1237 {
1238         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1239         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
1240         struct txgbe_vf_info *vfinfo = *TXGBE_DEV_VFDATA(dev);
1241         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1242         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1243         uint32_t intr_vector = 0;
1244         int err;
1245         bool link_up = false, negotiate = 0;
1246         uint32_t speed = 0;
1247         uint32_t allowed_speeds = 0;
1248         int mask = 0;
1249         int status;
1250         uint16_t vf, idx;
1251         uint32_t *link_speeds;
1252
1253         PMD_INIT_FUNC_TRACE();
1254
1255         /* TXGBE devices don't support:
1256          *    - half duplex (checked afterwards for valid speeds)
1257          *    - fixed speed: TODO implement
1258          */
1259         if (dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED) {
1260                 PMD_INIT_LOG(ERR,
1261                 "Invalid link_speeds for port %u, fix speed not supported",
1262                                 dev->data->port_id);
1263                 return -EINVAL;
1264         }
1265
1266         /* Stop the link setup handler before resetting the HW. */
1267         rte_eal_alarm_cancel(txgbe_dev_setup_link_alarm_handler, dev);
1268
1269         /* disable uio/vfio intr/eventfd mapping */
1270         rte_intr_disable(intr_handle);
1271
1272         /* stop adapter */
1273         hw->adapter_stopped = 0;
1274         txgbe_stop_hw(hw);
1275
1276         /* reinitialize adapter
1277          * this calls reset and start
1278          */
1279         hw->nb_rx_queues = dev->data->nb_rx_queues;
1280         hw->nb_tx_queues = dev->data->nb_tx_queues;
1281         status = txgbe_pf_reset_hw(hw);
1282         if (status != 0)
1283                 return -1;
1284         hw->mac.start_hw(hw);
1285         hw->mac.get_link_status = true;
1286
1287         /* configure PF module if SRIOV enabled */
1288         txgbe_pf_host_configure(dev);
1289
1290         txgbe_dev_phy_intr_setup(dev);
1291
1292         /* check and configure queue intr-vector mapping */
1293         if ((rte_intr_cap_multiple(intr_handle) ||
1294              !RTE_ETH_DEV_SRIOV(dev).active) &&
1295             dev->data->dev_conf.intr_conf.rxq != 0) {
1296                 intr_vector = dev->data->nb_rx_queues;
1297                 if (rte_intr_efd_enable(intr_handle, intr_vector))
1298                         return -1;
1299         }
1300
1301         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
1302                 intr_handle->intr_vec =
1303                         rte_zmalloc("intr_vec",
1304                                     dev->data->nb_rx_queues * sizeof(int), 0);
1305                 if (intr_handle->intr_vec == NULL) {
1306                         PMD_INIT_LOG(ERR, "Failed to allocate %d rx_queues"
1307                                      " intr_vec", dev->data->nb_rx_queues);
1308                         return -ENOMEM;
1309                 }
1310         }
1311
1312         /* confiugre msix for sleep until rx interrupt */
1313         txgbe_configure_msix(dev);
1314
1315         /* initialize transmission unit */
1316         txgbe_dev_tx_init(dev);
1317
1318         /* This can fail when allocating mbufs for descriptor rings */
1319         err = txgbe_dev_rx_init(dev);
1320         if (err) {
1321                 PMD_INIT_LOG(ERR, "Unable to initialize RX hardware");
1322                 goto error;
1323         }
1324
1325         mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK |
1326                 ETH_VLAN_EXTEND_MASK;
1327         err = txgbe_vlan_offload_config(dev, mask);
1328         if (err) {
1329                 PMD_INIT_LOG(ERR, "Unable to set VLAN offload");
1330                 goto error;
1331         }
1332
1333         /* Restore vf rate limit */
1334         if (vfinfo != NULL) {
1335                 for (vf = 0; vf < pci_dev->max_vfs; vf++)
1336                         for (idx = 0; idx < TXGBE_MAX_QUEUE_NUM_PER_VF; idx++)
1337                                 if (vfinfo[vf].tx_rate[idx] != 0)
1338                                         txgbe_set_vf_rate_limit(dev, vf,
1339                                                 vfinfo[vf].tx_rate[idx],
1340                                                 1 << idx);
1341         }
1342
1343         err = txgbe_dev_rxtx_start(dev);
1344         if (err < 0) {
1345                 PMD_INIT_LOG(ERR, "Unable to start rxtx queues");
1346                 goto error;
1347         }
1348
1349         /* Skip link setup if loopback mode is enabled. */
1350         if (hw->mac.type == txgbe_mac_raptor &&
1351             dev->data->dev_conf.lpbk_mode)
1352                 goto skip_link_setup;
1353
1354         if (txgbe_is_sfp(hw) && hw->phy.multispeed_fiber) {
1355                 err = hw->mac.setup_sfp(hw);
1356                 if (err)
1357                         goto error;
1358         }
1359
1360         if (hw->phy.media_type == txgbe_media_type_copper) {
1361                 /* Turn on the copper */
1362                 hw->phy.set_phy_power(hw, true);
1363         } else {
1364                 /* Turn on the laser */
1365                 hw->mac.enable_tx_laser(hw);
1366         }
1367
1368         err = hw->mac.check_link(hw, &speed, &link_up, 0);
1369         if (err)
1370                 goto error;
1371         dev->data->dev_link.link_status = link_up;
1372
1373         err = hw->mac.get_link_capabilities(hw, &speed, &negotiate);
1374         if (err)
1375                 goto error;
1376
1377         allowed_speeds = ETH_LINK_SPEED_100M | ETH_LINK_SPEED_1G |
1378                         ETH_LINK_SPEED_10G;
1379
1380         link_speeds = &dev->data->dev_conf.link_speeds;
1381         if (*link_speeds & ~allowed_speeds) {
1382                 PMD_INIT_LOG(ERR, "Invalid link setting");
1383                 goto error;
1384         }
1385
1386         speed = 0x0;
1387         if (*link_speeds == ETH_LINK_SPEED_AUTONEG) {
1388                 speed = (TXGBE_LINK_SPEED_100M_FULL |
1389                          TXGBE_LINK_SPEED_1GB_FULL |
1390                          TXGBE_LINK_SPEED_10GB_FULL);
1391         } else {
1392                 if (*link_speeds & ETH_LINK_SPEED_10G)
1393                         speed |= TXGBE_LINK_SPEED_10GB_FULL;
1394                 if (*link_speeds & ETH_LINK_SPEED_5G)
1395                         speed |= TXGBE_LINK_SPEED_5GB_FULL;
1396                 if (*link_speeds & ETH_LINK_SPEED_2_5G)
1397                         speed |= TXGBE_LINK_SPEED_2_5GB_FULL;
1398                 if (*link_speeds & ETH_LINK_SPEED_1G)
1399                         speed |= TXGBE_LINK_SPEED_1GB_FULL;
1400                 if (*link_speeds & ETH_LINK_SPEED_100M)
1401                         speed |= TXGBE_LINK_SPEED_100M_FULL;
1402         }
1403
1404         err = hw->mac.setup_link(hw, speed, link_up);
1405         if (err)
1406                 goto error;
1407
1408 skip_link_setup:
1409
1410         if (rte_intr_allow_others(intr_handle)) {
1411                 /* check if lsc interrupt is enabled */
1412                 if (dev->data->dev_conf.intr_conf.lsc != 0)
1413                         txgbe_dev_lsc_interrupt_setup(dev, TRUE);
1414                 else
1415                         txgbe_dev_lsc_interrupt_setup(dev, FALSE);
1416                 txgbe_dev_macsec_interrupt_setup(dev);
1417                 txgbe_set_ivar_map(hw, -1, 1, TXGBE_MISC_VEC_ID);
1418         } else {
1419                 rte_intr_callback_unregister(intr_handle,
1420                                              txgbe_dev_interrupt_handler, dev);
1421                 if (dev->data->dev_conf.intr_conf.lsc != 0)
1422                         PMD_INIT_LOG(INFO, "lsc won't enable because of"
1423                                      " no intr multiplex");
1424         }
1425
1426         /* check if rxq interrupt is enabled */
1427         if (dev->data->dev_conf.intr_conf.rxq != 0 &&
1428             rte_intr_dp_is_en(intr_handle))
1429                 txgbe_dev_rxq_interrupt_setup(dev);
1430
1431         /* enable uio/vfio intr/eventfd mapping */
1432         rte_intr_enable(intr_handle);
1433
1434         /* resume enabled intr since hw reset */
1435         txgbe_enable_intr(dev);
1436
1437         /*
1438          * Update link status right before return, because it may
1439          * start link configuration process in a separate thread.
1440          */
1441         txgbe_dev_link_update(dev, 0);
1442
1443         wr32m(hw, TXGBE_LEDCTL, 0xFFFFFFFF, TXGBE_LEDCTL_ORD_MASK);
1444
1445         txgbe_read_stats_registers(hw, hw_stats);
1446         hw->offset_loaded = 1;
1447
1448         return 0;
1449
1450 error:
1451         PMD_INIT_LOG(ERR, "failure in dev start: %d", err);
1452         txgbe_dev_clear_queues(dev);
1453         return -EIO;
1454 }
1455
1456 /*
1457  * Stop device: disable rx and tx functions to allow for reconfiguring.
1458  */
1459 static int
1460 txgbe_dev_stop(struct rte_eth_dev *dev)
1461 {
1462         struct rte_eth_link link;
1463         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1464         struct txgbe_vf_info *vfinfo = *TXGBE_DEV_VFDATA(dev);
1465         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1466         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1467         int vf;
1468
1469         if (hw->adapter_stopped)
1470                 return 0;
1471
1472         PMD_INIT_FUNC_TRACE();
1473
1474         rte_eal_alarm_cancel(txgbe_dev_setup_link_alarm_handler, dev);
1475
1476         /* disable interrupts */
1477         txgbe_disable_intr(hw);
1478
1479         /* reset the NIC */
1480         txgbe_pf_reset_hw(hw);
1481         hw->adapter_stopped = 0;
1482
1483         /* stop adapter */
1484         txgbe_stop_hw(hw);
1485
1486         for (vf = 0; vfinfo != NULL && vf < pci_dev->max_vfs; vf++)
1487                 vfinfo[vf].clear_to_send = false;
1488
1489         if (hw->phy.media_type == txgbe_media_type_copper) {
1490                 /* Turn off the copper */
1491                 hw->phy.set_phy_power(hw, false);
1492         } else {
1493                 /* Turn off the laser */
1494                 hw->mac.disable_tx_laser(hw);
1495         }
1496
1497         txgbe_dev_clear_queues(dev);
1498
1499         /* Clear stored conf */
1500         dev->data->scattered_rx = 0;
1501         dev->data->lro = 0;
1502
1503         /* Clear recorded link status */
1504         memset(&link, 0, sizeof(link));
1505         rte_eth_linkstatus_set(dev, &link);
1506
1507         if (!rte_intr_allow_others(intr_handle))
1508                 /* resume to the default handler */
1509                 rte_intr_callback_register(intr_handle,
1510                                            txgbe_dev_interrupt_handler,
1511                                            (void *)dev);
1512
1513         /* Clean datapath event and queue/vec mapping */
1514         rte_intr_efd_disable(intr_handle);
1515         if (intr_handle->intr_vec != NULL) {
1516                 rte_free(intr_handle->intr_vec);
1517                 intr_handle->intr_vec = NULL;
1518         }
1519
1520         wr32m(hw, TXGBE_LEDCTL, 0xFFFFFFFF, TXGBE_LEDCTL_SEL_MASK);
1521
1522         hw->adapter_stopped = true;
1523         dev->data->dev_started = 0;
1524
1525         return 0;
1526 }
1527
1528 /*
1529  * Set device link up: enable tx.
1530  */
1531 static int
1532 txgbe_dev_set_link_up(struct rte_eth_dev *dev)
1533 {
1534         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1535
1536         if (hw->phy.media_type == txgbe_media_type_copper) {
1537                 /* Turn on the copper */
1538                 hw->phy.set_phy_power(hw, true);
1539         } else {
1540                 /* Turn on the laser */
1541                 hw->mac.enable_tx_laser(hw);
1542                 txgbe_dev_link_update(dev, 0);
1543         }
1544
1545         return 0;
1546 }
1547
1548 /*
1549  * Set device link down: disable tx.
1550  */
1551 static int
1552 txgbe_dev_set_link_down(struct rte_eth_dev *dev)
1553 {
1554         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1555
1556         if (hw->phy.media_type == txgbe_media_type_copper) {
1557                 /* Turn off the copper */
1558                 hw->phy.set_phy_power(hw, false);
1559         } else {
1560                 /* Turn off the laser */
1561                 hw->mac.disable_tx_laser(hw);
1562                 txgbe_dev_link_update(dev, 0);
1563         }
1564
1565         return 0;
1566 }
1567
1568 /*
1569  * Reset and stop device.
1570  */
1571 static int
1572 txgbe_dev_close(struct rte_eth_dev *dev)
1573 {
1574         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1575         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1576         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1577         int retries = 0;
1578         int ret;
1579
1580         PMD_INIT_FUNC_TRACE();
1581
1582         txgbe_pf_reset_hw(hw);
1583
1584         ret = txgbe_dev_stop(dev);
1585
1586         txgbe_dev_free_queues(dev);
1587
1588         /* reprogram the RAR[0] in case user changed it. */
1589         txgbe_set_rar(hw, 0, hw->mac.addr, 0, true);
1590
1591         /* Unlock any pending hardware semaphore */
1592         txgbe_swfw_lock_reset(hw);
1593
1594         /* disable uio intr before callback unregister */
1595         rte_intr_disable(intr_handle);
1596
1597         do {
1598                 ret = rte_intr_callback_unregister(intr_handle,
1599                                 txgbe_dev_interrupt_handler, dev);
1600                 if (ret >= 0 || ret == -ENOENT) {
1601                         break;
1602                 } else if (ret != -EAGAIN) {
1603                         PMD_INIT_LOG(ERR,
1604                                 "intr callback unregister failed: %d",
1605                                 ret);
1606                 }
1607                 rte_delay_ms(100);
1608         } while (retries++ < (10 + TXGBE_LINK_UP_TIME));
1609
1610         /* cancel the delay handler before remove dev */
1611         rte_eal_alarm_cancel(txgbe_dev_interrupt_delayed_handler, dev);
1612
1613         /* uninitialize PF if max_vfs not zero */
1614         txgbe_pf_host_uninit(dev);
1615
1616         rte_free(dev->data->mac_addrs);
1617         dev->data->mac_addrs = NULL;
1618
1619         rte_free(dev->data->hash_mac_addrs);
1620         dev->data->hash_mac_addrs = NULL;
1621
1622         return ret;
1623 }
1624
1625 /*
1626  * Reset PF device.
1627  */
1628 static int
1629 txgbe_dev_reset(struct rte_eth_dev *dev)
1630 {
1631         int ret;
1632
1633         /* When a DPDK PMD PF begin to reset PF port, it should notify all
1634          * its VF to make them align with it. The detailed notification
1635          * mechanism is PMD specific. As to txgbe PF, it is rather complex.
1636          * To avoid unexpected behavior in VF, currently reset of PF with
1637          * SR-IOV activation is not supported. It might be supported later.
1638          */
1639         if (dev->data->sriov.active)
1640                 return -ENOTSUP;
1641
1642         ret = eth_txgbe_dev_uninit(dev);
1643         if (ret)
1644                 return ret;
1645
1646         ret = eth_txgbe_dev_init(dev, NULL);
1647
1648         return ret;
1649 }
1650
1651 #define UPDATE_QP_COUNTER_32bit(reg, last_counter, counter)     \
1652         {                                                       \
1653                 uint32_t current_counter = rd32(hw, reg);       \
1654                 if (current_counter < last_counter)             \
1655                         current_counter += 0x100000000LL;       \
1656                 if (!hw->offset_loaded)                         \
1657                         last_counter = current_counter;         \
1658                 counter = current_counter - last_counter;       \
1659                 counter &= 0xFFFFFFFFLL;                        \
1660         }
1661
1662 #define UPDATE_QP_COUNTER_36bit(reg_lsb, reg_msb, last_counter, counter) \
1663         {                                                                \
1664                 uint64_t current_counter_lsb = rd32(hw, reg_lsb);        \
1665                 uint64_t current_counter_msb = rd32(hw, reg_msb);        \
1666                 uint64_t current_counter = (current_counter_msb << 32) | \
1667                         current_counter_lsb;                             \
1668                 if (current_counter < last_counter)                      \
1669                         current_counter += 0x1000000000LL;               \
1670                 if (!hw->offset_loaded)                                  \
1671                         last_counter = current_counter;                  \
1672                 counter = current_counter - last_counter;                \
1673                 counter &= 0xFFFFFFFFFLL;                                \
1674         }
1675
1676 void
1677 txgbe_read_stats_registers(struct txgbe_hw *hw,
1678                            struct txgbe_hw_stats *hw_stats)
1679 {
1680         unsigned int i;
1681
1682         /* QP Stats */
1683         for (i = 0; i < hw->nb_rx_queues; i++) {
1684                 UPDATE_QP_COUNTER_32bit(TXGBE_QPRXPKT(i),
1685                         hw->qp_last[i].rx_qp_packets,
1686                         hw_stats->qp[i].rx_qp_packets);
1687                 UPDATE_QP_COUNTER_36bit(TXGBE_QPRXOCTL(i), TXGBE_QPRXOCTH(i),
1688                         hw->qp_last[i].rx_qp_bytes,
1689                         hw_stats->qp[i].rx_qp_bytes);
1690                 UPDATE_QP_COUNTER_32bit(TXGBE_QPRXMPKT(i),
1691                         hw->qp_last[i].rx_qp_mc_packets,
1692                         hw_stats->qp[i].rx_qp_mc_packets);
1693         }
1694
1695         for (i = 0; i < hw->nb_tx_queues; i++) {
1696                 UPDATE_QP_COUNTER_32bit(TXGBE_QPTXPKT(i),
1697                         hw->qp_last[i].tx_qp_packets,
1698                         hw_stats->qp[i].tx_qp_packets);
1699                 UPDATE_QP_COUNTER_36bit(TXGBE_QPTXOCTL(i), TXGBE_QPTXOCTH(i),
1700                         hw->qp_last[i].tx_qp_bytes,
1701                         hw_stats->qp[i].tx_qp_bytes);
1702         }
1703         /* PB Stats */
1704         for (i = 0; i < TXGBE_MAX_UP; i++) {
1705                 hw_stats->up[i].rx_up_xon_packets +=
1706                                 rd32(hw, TXGBE_PBRXUPXON(i));
1707                 hw_stats->up[i].rx_up_xoff_packets +=
1708                                 rd32(hw, TXGBE_PBRXUPXOFF(i));
1709                 hw_stats->up[i].tx_up_xon_packets +=
1710                                 rd32(hw, TXGBE_PBTXUPXON(i));
1711                 hw_stats->up[i].tx_up_xoff_packets +=
1712                                 rd32(hw, TXGBE_PBTXUPXOFF(i));
1713                 hw_stats->up[i].tx_up_xon2off_packets +=
1714                                 rd32(hw, TXGBE_PBTXUPOFF(i));
1715                 hw_stats->up[i].rx_up_dropped +=
1716                                 rd32(hw, TXGBE_PBRXMISS(i));
1717         }
1718         hw_stats->rx_xon_packets += rd32(hw, TXGBE_PBRXLNKXON);
1719         hw_stats->rx_xoff_packets += rd32(hw, TXGBE_PBRXLNKXOFF);
1720         hw_stats->tx_xon_packets += rd32(hw, TXGBE_PBTXLNKXON);
1721         hw_stats->tx_xoff_packets += rd32(hw, TXGBE_PBTXLNKXOFF);
1722
1723         /* DMA Stats */
1724         hw_stats->rx_packets += rd32(hw, TXGBE_DMARXPKT);
1725         hw_stats->tx_packets += rd32(hw, TXGBE_DMATXPKT);
1726
1727         hw_stats->rx_bytes += rd64(hw, TXGBE_DMARXOCTL);
1728         hw_stats->tx_bytes += rd64(hw, TXGBE_DMATXOCTL);
1729         hw_stats->rx_drop_packets += rd32(hw, TXGBE_PBRXDROP);
1730
1731         /* MAC Stats */
1732         hw_stats->rx_crc_errors += rd64(hw, TXGBE_MACRXERRCRCL);
1733         hw_stats->rx_multicast_packets += rd64(hw, TXGBE_MACRXMPKTL);
1734         hw_stats->tx_multicast_packets += rd64(hw, TXGBE_MACTXMPKTL);
1735
1736         hw_stats->rx_total_packets += rd64(hw, TXGBE_MACRXPKTL);
1737         hw_stats->tx_total_packets += rd64(hw, TXGBE_MACTXPKTL);
1738         hw_stats->rx_total_bytes += rd64(hw, TXGBE_MACRXGBOCTL);
1739
1740         hw_stats->rx_broadcast_packets += rd64(hw, TXGBE_MACRXOCTL);
1741         hw_stats->tx_broadcast_packets += rd32(hw, TXGBE_MACTXOCTL);
1742
1743         hw_stats->rx_size_64_packets += rd64(hw, TXGBE_MACRX1TO64L);
1744         hw_stats->rx_size_65_to_127_packets += rd64(hw, TXGBE_MACRX65TO127L);
1745         hw_stats->rx_size_128_to_255_packets += rd64(hw, TXGBE_MACRX128TO255L);
1746         hw_stats->rx_size_256_to_511_packets += rd64(hw, TXGBE_MACRX256TO511L);
1747         hw_stats->rx_size_512_to_1023_packets +=
1748                         rd64(hw, TXGBE_MACRX512TO1023L);
1749         hw_stats->rx_size_1024_to_max_packets +=
1750                         rd64(hw, TXGBE_MACRX1024TOMAXL);
1751         hw_stats->tx_size_64_packets += rd64(hw, TXGBE_MACTX1TO64L);
1752         hw_stats->tx_size_65_to_127_packets += rd64(hw, TXGBE_MACTX65TO127L);
1753         hw_stats->tx_size_128_to_255_packets += rd64(hw, TXGBE_MACTX128TO255L);
1754         hw_stats->tx_size_256_to_511_packets += rd64(hw, TXGBE_MACTX256TO511L);
1755         hw_stats->tx_size_512_to_1023_packets +=
1756                         rd64(hw, TXGBE_MACTX512TO1023L);
1757         hw_stats->tx_size_1024_to_max_packets +=
1758                         rd64(hw, TXGBE_MACTX1024TOMAXL);
1759
1760         hw_stats->rx_undersize_errors += rd64(hw, TXGBE_MACRXERRLENL);
1761         hw_stats->rx_oversize_errors += rd32(hw, TXGBE_MACRXOVERSIZE);
1762         hw_stats->rx_jabber_errors += rd32(hw, TXGBE_MACRXJABBER);
1763
1764         /* MNG Stats */
1765         hw_stats->mng_bmc2host_packets = rd32(hw, TXGBE_MNGBMC2OS);
1766         hw_stats->mng_host2bmc_packets = rd32(hw, TXGBE_MNGOS2BMC);
1767         hw_stats->rx_management_packets = rd32(hw, TXGBE_DMARXMNG);
1768         hw_stats->tx_management_packets = rd32(hw, TXGBE_DMATXMNG);
1769
1770         /* FCoE Stats */
1771         hw_stats->rx_fcoe_crc_errors += rd32(hw, TXGBE_FCOECRC);
1772         hw_stats->rx_fcoe_mbuf_allocation_errors += rd32(hw, TXGBE_FCOELAST);
1773         hw_stats->rx_fcoe_dropped += rd32(hw, TXGBE_FCOERPDC);
1774         hw_stats->rx_fcoe_packets += rd32(hw, TXGBE_FCOEPRC);
1775         hw_stats->tx_fcoe_packets += rd32(hw, TXGBE_FCOEPTC);
1776         hw_stats->rx_fcoe_bytes += rd32(hw, TXGBE_FCOEDWRC);
1777         hw_stats->tx_fcoe_bytes += rd32(hw, TXGBE_FCOEDWTC);
1778
1779         /* Flow Director Stats */
1780         hw_stats->flow_director_matched_filters += rd32(hw, TXGBE_FDIRMATCH);
1781         hw_stats->flow_director_missed_filters += rd32(hw, TXGBE_FDIRMISS);
1782         hw_stats->flow_director_added_filters +=
1783                 TXGBE_FDIRUSED_ADD(rd32(hw, TXGBE_FDIRUSED));
1784         hw_stats->flow_director_removed_filters +=
1785                 TXGBE_FDIRUSED_REM(rd32(hw, TXGBE_FDIRUSED));
1786         hw_stats->flow_director_filter_add_errors +=
1787                 TXGBE_FDIRFAIL_ADD(rd32(hw, TXGBE_FDIRFAIL));
1788         hw_stats->flow_director_filter_remove_errors +=
1789                 TXGBE_FDIRFAIL_REM(rd32(hw, TXGBE_FDIRFAIL));
1790
1791         /* MACsec Stats */
1792         hw_stats->tx_macsec_pkts_untagged += rd32(hw, TXGBE_LSECTX_UTPKT);
1793         hw_stats->tx_macsec_pkts_encrypted +=
1794                         rd32(hw, TXGBE_LSECTX_ENCPKT);
1795         hw_stats->tx_macsec_pkts_protected +=
1796                         rd32(hw, TXGBE_LSECTX_PROTPKT);
1797         hw_stats->tx_macsec_octets_encrypted +=
1798                         rd32(hw, TXGBE_LSECTX_ENCOCT);
1799         hw_stats->tx_macsec_octets_protected +=
1800                         rd32(hw, TXGBE_LSECTX_PROTOCT);
1801         hw_stats->rx_macsec_pkts_untagged += rd32(hw, TXGBE_LSECRX_UTPKT);
1802         hw_stats->rx_macsec_pkts_badtag += rd32(hw, TXGBE_LSECRX_BTPKT);
1803         hw_stats->rx_macsec_pkts_nosci += rd32(hw, TXGBE_LSECRX_NOSCIPKT);
1804         hw_stats->rx_macsec_pkts_unknownsci += rd32(hw, TXGBE_LSECRX_UNSCIPKT);
1805         hw_stats->rx_macsec_octets_decrypted += rd32(hw, TXGBE_LSECRX_DECOCT);
1806         hw_stats->rx_macsec_octets_validated += rd32(hw, TXGBE_LSECRX_VLDOCT);
1807         hw_stats->rx_macsec_sc_pkts_unchecked +=
1808                         rd32(hw, TXGBE_LSECRX_UNCHKPKT);
1809         hw_stats->rx_macsec_sc_pkts_delayed += rd32(hw, TXGBE_LSECRX_DLYPKT);
1810         hw_stats->rx_macsec_sc_pkts_late += rd32(hw, TXGBE_LSECRX_LATEPKT);
1811         for (i = 0; i < 2; i++) {
1812                 hw_stats->rx_macsec_sa_pkts_ok +=
1813                         rd32(hw, TXGBE_LSECRX_OKPKT(i));
1814                 hw_stats->rx_macsec_sa_pkts_invalid +=
1815                         rd32(hw, TXGBE_LSECRX_INVPKT(i));
1816                 hw_stats->rx_macsec_sa_pkts_notvalid +=
1817                         rd32(hw, TXGBE_LSECRX_BADPKT(i));
1818         }
1819         hw_stats->rx_macsec_sa_pkts_unusedsa +=
1820                         rd32(hw, TXGBE_LSECRX_INVSAPKT);
1821         hw_stats->rx_macsec_sa_pkts_notusingsa +=
1822                         rd32(hw, TXGBE_LSECRX_BADSAPKT);
1823
1824         hw_stats->rx_total_missed_packets = 0;
1825         for (i = 0; i < TXGBE_MAX_UP; i++) {
1826                 hw_stats->rx_total_missed_packets +=
1827                         hw_stats->up[i].rx_up_dropped;
1828         }
1829 }
1830
1831 static int
1832 txgbe_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1833 {
1834         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1835         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
1836         struct txgbe_stat_mappings *stat_mappings =
1837                         TXGBE_DEV_STAT_MAPPINGS(dev);
1838         uint32_t i, j;
1839
1840         txgbe_read_stats_registers(hw, hw_stats);
1841
1842         if (stats == NULL)
1843                 return -EINVAL;
1844
1845         /* Fill out the rte_eth_stats statistics structure */
1846         stats->ipackets = hw_stats->rx_packets;
1847         stats->ibytes = hw_stats->rx_bytes;
1848         stats->opackets = hw_stats->tx_packets;
1849         stats->obytes = hw_stats->tx_bytes;
1850
1851         memset(&stats->q_ipackets, 0, sizeof(stats->q_ipackets));
1852         memset(&stats->q_opackets, 0, sizeof(stats->q_opackets));
1853         memset(&stats->q_ibytes, 0, sizeof(stats->q_ibytes));
1854         memset(&stats->q_obytes, 0, sizeof(stats->q_obytes));
1855         memset(&stats->q_errors, 0, sizeof(stats->q_errors));
1856         for (i = 0; i < TXGBE_MAX_QP; i++) {
1857                 uint32_t n = i / NB_QMAP_FIELDS_PER_QSM_REG;
1858                 uint32_t offset = (i % NB_QMAP_FIELDS_PER_QSM_REG) * 8;
1859                 uint32_t q_map;
1860
1861                 q_map = (stat_mappings->rqsm[n] >> offset)
1862                                 & QMAP_FIELD_RESERVED_BITS_MASK;
1863                 j = (q_map < RTE_ETHDEV_QUEUE_STAT_CNTRS
1864                      ? q_map : q_map % RTE_ETHDEV_QUEUE_STAT_CNTRS);
1865                 stats->q_ipackets[j] += hw_stats->qp[i].rx_qp_packets;
1866                 stats->q_ibytes[j] += hw_stats->qp[i].rx_qp_bytes;
1867
1868                 q_map = (stat_mappings->tqsm[n] >> offset)
1869                                 & QMAP_FIELD_RESERVED_BITS_MASK;
1870                 j = (q_map < RTE_ETHDEV_QUEUE_STAT_CNTRS
1871                      ? q_map : q_map % RTE_ETHDEV_QUEUE_STAT_CNTRS);
1872                 stats->q_opackets[j] += hw_stats->qp[i].tx_qp_packets;
1873                 stats->q_obytes[j] += hw_stats->qp[i].tx_qp_bytes;
1874         }
1875
1876         /* Rx Errors */
1877         stats->imissed  = hw_stats->rx_total_missed_packets;
1878         stats->ierrors  = hw_stats->rx_crc_errors +
1879                           hw_stats->rx_mac_short_packet_dropped +
1880                           hw_stats->rx_length_errors +
1881                           hw_stats->rx_undersize_errors +
1882                           hw_stats->rx_oversize_errors +
1883                           hw_stats->rx_drop_packets +
1884                           hw_stats->rx_illegal_byte_errors +
1885                           hw_stats->rx_error_bytes +
1886                           hw_stats->rx_fragment_errors +
1887                           hw_stats->rx_fcoe_crc_errors +
1888                           hw_stats->rx_fcoe_mbuf_allocation_errors;
1889
1890         /* Tx Errors */
1891         stats->oerrors  = 0;
1892         return 0;
1893 }
1894
1895 static int
1896 txgbe_dev_stats_reset(struct rte_eth_dev *dev)
1897 {
1898         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
1899         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
1900
1901         /* HW registers are cleared on read */
1902         hw->offset_loaded = 0;
1903         txgbe_dev_stats_get(dev, NULL);
1904         hw->offset_loaded = 1;
1905
1906         /* Reset software totals */
1907         memset(hw_stats, 0, sizeof(*hw_stats));
1908
1909         return 0;
1910 }
1911
1912 /* This function calculates the number of xstats based on the current config */
1913 static unsigned
1914 txgbe_xstats_calc_num(struct rte_eth_dev *dev)
1915 {
1916         int nb_queues = max(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
1917         return TXGBE_NB_HW_STATS +
1918                TXGBE_NB_UP_STATS * TXGBE_MAX_UP +
1919                TXGBE_NB_QP_STATS * nb_queues;
1920 }
1921
1922 static inline int
1923 txgbe_get_name_by_id(uint32_t id, char *name, uint32_t size)
1924 {
1925         int nb, st;
1926
1927         /* Extended stats from txgbe_hw_stats */
1928         if (id < TXGBE_NB_HW_STATS) {
1929                 snprintf(name, size, "[hw]%s",
1930                         rte_txgbe_stats_strings[id].name);
1931                 return 0;
1932         }
1933         id -= TXGBE_NB_HW_STATS;
1934
1935         /* Priority Stats */
1936         if (id < TXGBE_NB_UP_STATS * TXGBE_MAX_UP) {
1937                 nb = id / TXGBE_NB_UP_STATS;
1938                 st = id % TXGBE_NB_UP_STATS;
1939                 snprintf(name, size, "[p%u]%s", nb,
1940                         rte_txgbe_up_strings[st].name);
1941                 return 0;
1942         }
1943         id -= TXGBE_NB_UP_STATS * TXGBE_MAX_UP;
1944
1945         /* Queue Stats */
1946         if (id < TXGBE_NB_QP_STATS * TXGBE_MAX_QP) {
1947                 nb = id / TXGBE_NB_QP_STATS;
1948                 st = id % TXGBE_NB_QP_STATS;
1949                 snprintf(name, size, "[q%u]%s", nb,
1950                         rte_txgbe_qp_strings[st].name);
1951                 return 0;
1952         }
1953         id -= TXGBE_NB_QP_STATS * TXGBE_MAX_QP;
1954
1955         return -(int)(id + 1);
1956 }
1957
1958 static inline int
1959 txgbe_get_offset_by_id(uint32_t id, uint32_t *offset)
1960 {
1961         int nb, st;
1962
1963         /* Extended stats from txgbe_hw_stats */
1964         if (id < TXGBE_NB_HW_STATS) {
1965                 *offset = rte_txgbe_stats_strings[id].offset;
1966                 return 0;
1967         }
1968         id -= TXGBE_NB_HW_STATS;
1969
1970         /* Priority Stats */
1971         if (id < TXGBE_NB_UP_STATS * TXGBE_MAX_UP) {
1972                 nb = id / TXGBE_NB_UP_STATS;
1973                 st = id % TXGBE_NB_UP_STATS;
1974                 *offset = rte_txgbe_up_strings[st].offset +
1975                         nb * (TXGBE_NB_UP_STATS * sizeof(uint64_t));
1976                 return 0;
1977         }
1978         id -= TXGBE_NB_UP_STATS * TXGBE_MAX_UP;
1979
1980         /* Queue Stats */
1981         if (id < TXGBE_NB_QP_STATS * TXGBE_MAX_QP) {
1982                 nb = id / TXGBE_NB_QP_STATS;
1983                 st = id % TXGBE_NB_QP_STATS;
1984                 *offset = rte_txgbe_qp_strings[st].offset +
1985                         nb * (TXGBE_NB_QP_STATS * sizeof(uint64_t));
1986                 return 0;
1987         }
1988         id -= TXGBE_NB_QP_STATS * TXGBE_MAX_QP;
1989
1990         return -(int)(id + 1);
1991 }
1992
1993 static int txgbe_dev_xstats_get_names(struct rte_eth_dev *dev,
1994         struct rte_eth_xstat_name *xstats_names, unsigned int limit)
1995 {
1996         unsigned int i, count;
1997
1998         count = txgbe_xstats_calc_num(dev);
1999         if (xstats_names == NULL)
2000                 return count;
2001
2002         /* Note: limit >= cnt_stats checked upstream
2003          * in rte_eth_xstats_names()
2004          */
2005         limit = min(limit, count);
2006
2007         /* Extended stats from txgbe_hw_stats */
2008         for (i = 0; i < limit; i++) {
2009                 if (txgbe_get_name_by_id(i, xstats_names[i].name,
2010                         sizeof(xstats_names[i].name))) {
2011                         PMD_INIT_LOG(WARNING, "id value %d isn't valid", i);
2012                         break;
2013                 }
2014         }
2015
2016         return i;
2017 }
2018
2019 static int txgbe_dev_xstats_get_names_by_id(struct rte_eth_dev *dev,
2020         struct rte_eth_xstat_name *xstats_names,
2021         const uint64_t *ids,
2022         unsigned int limit)
2023 {
2024         unsigned int i;
2025
2026         if (ids == NULL)
2027                 return txgbe_dev_xstats_get_names(dev, xstats_names, limit);
2028
2029         for (i = 0; i < limit; i++) {
2030                 if (txgbe_get_name_by_id(ids[i], xstats_names[i].name,
2031                                 sizeof(xstats_names[i].name))) {
2032                         PMD_INIT_LOG(WARNING, "id value %d isn't valid", i);
2033                         return -1;
2034                 }
2035         }
2036
2037         return i;
2038 }
2039
2040 static int
2041 txgbe_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
2042                                          unsigned int limit)
2043 {
2044         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2045         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
2046         unsigned int i, count;
2047
2048         txgbe_read_stats_registers(hw, hw_stats);
2049
2050         /* If this is a reset xstats is NULL, and we have cleared the
2051          * registers by reading them.
2052          */
2053         count = txgbe_xstats_calc_num(dev);
2054         if (xstats == NULL)
2055                 return count;
2056
2057         limit = min(limit, txgbe_xstats_calc_num(dev));
2058
2059         /* Extended stats from txgbe_hw_stats */
2060         for (i = 0; i < limit; i++) {
2061                 uint32_t offset = 0;
2062
2063                 if (txgbe_get_offset_by_id(i, &offset)) {
2064                         PMD_INIT_LOG(WARNING, "id value %d isn't valid", i);
2065                         break;
2066                 }
2067                 xstats[i].value = *(uint64_t *)(((char *)hw_stats) + offset);
2068                 xstats[i].id = i;
2069         }
2070
2071         return i;
2072 }
2073
2074 static int
2075 txgbe_dev_xstats_get_(struct rte_eth_dev *dev, uint64_t *values,
2076                                          unsigned int limit)
2077 {
2078         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2079         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
2080         unsigned int i, count;
2081
2082         txgbe_read_stats_registers(hw, hw_stats);
2083
2084         /* If this is a reset xstats is NULL, and we have cleared the
2085          * registers by reading them.
2086          */
2087         count = txgbe_xstats_calc_num(dev);
2088         if (values == NULL)
2089                 return count;
2090
2091         limit = min(limit, txgbe_xstats_calc_num(dev));
2092
2093         /* Extended stats from txgbe_hw_stats */
2094         for (i = 0; i < limit; i++) {
2095                 uint32_t offset;
2096
2097                 if (txgbe_get_offset_by_id(i, &offset)) {
2098                         PMD_INIT_LOG(WARNING, "id value %d isn't valid", i);
2099                         break;
2100                 }
2101                 values[i] = *(uint64_t *)(((char *)hw_stats) + offset);
2102         }
2103
2104         return i;
2105 }
2106
2107 static int
2108 txgbe_dev_xstats_get_by_id(struct rte_eth_dev *dev, const uint64_t *ids,
2109                 uint64_t *values, unsigned int limit)
2110 {
2111         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
2112         unsigned int i;
2113
2114         if (ids == NULL)
2115                 return txgbe_dev_xstats_get_(dev, values, limit);
2116
2117         for (i = 0; i < limit; i++) {
2118                 uint32_t offset;
2119
2120                 if (txgbe_get_offset_by_id(ids[i], &offset)) {
2121                         PMD_INIT_LOG(WARNING, "id value %d isn't valid", i);
2122                         break;
2123                 }
2124                 values[i] = *(uint64_t *)(((char *)hw_stats) + offset);
2125         }
2126
2127         return i;
2128 }
2129
2130 static int
2131 txgbe_dev_xstats_reset(struct rte_eth_dev *dev)
2132 {
2133         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2134         struct txgbe_hw_stats *hw_stats = TXGBE_DEV_STATS(dev);
2135
2136         /* HW registers are cleared on read */
2137         hw->offset_loaded = 0;
2138         txgbe_read_stats_registers(hw, hw_stats);
2139         hw->offset_loaded = 1;
2140
2141         /* Reset software totals */
2142         memset(hw_stats, 0, sizeof(*hw_stats));
2143
2144         return 0;
2145 }
2146
2147 static int
2148 txgbe_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
2149 {
2150         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2151         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2152
2153         dev_info->max_rx_queues = (uint16_t)hw->mac.max_rx_queues;
2154         dev_info->max_tx_queues = (uint16_t)hw->mac.max_tx_queues;
2155         dev_info->min_rx_bufsize = 1024;
2156         dev_info->max_rx_pktlen = 15872;
2157         dev_info->max_mac_addrs = hw->mac.num_rar_entries;
2158         dev_info->max_hash_mac_addrs = TXGBE_VMDQ_NUM_UC_MAC;
2159         dev_info->max_vfs = pci_dev->max_vfs;
2160         dev_info->max_vmdq_pools = ETH_64_POOLS;
2161         dev_info->vmdq_queue_num = dev_info->max_rx_queues;
2162         dev_info->rx_queue_offload_capa = txgbe_get_rx_queue_offloads(dev);
2163         dev_info->rx_offload_capa = (txgbe_get_rx_port_offloads(dev) |
2164                                      dev_info->rx_queue_offload_capa);
2165         dev_info->tx_queue_offload_capa = txgbe_get_tx_queue_offloads(dev);
2166         dev_info->tx_offload_capa = txgbe_get_tx_port_offloads(dev);
2167
2168         dev_info->default_rxconf = (struct rte_eth_rxconf) {
2169                 .rx_thresh = {
2170                         .pthresh = TXGBE_DEFAULT_RX_PTHRESH,
2171                         .hthresh = TXGBE_DEFAULT_RX_HTHRESH,
2172                         .wthresh = TXGBE_DEFAULT_RX_WTHRESH,
2173                 },
2174                 .rx_free_thresh = TXGBE_DEFAULT_RX_FREE_THRESH,
2175                 .rx_drop_en = 0,
2176                 .offloads = 0,
2177         };
2178
2179         dev_info->default_txconf = (struct rte_eth_txconf) {
2180                 .tx_thresh = {
2181                         .pthresh = TXGBE_DEFAULT_TX_PTHRESH,
2182                         .hthresh = TXGBE_DEFAULT_TX_HTHRESH,
2183                         .wthresh = TXGBE_DEFAULT_TX_WTHRESH,
2184                 },
2185                 .tx_free_thresh = TXGBE_DEFAULT_TX_FREE_THRESH,
2186                 .offloads = 0,
2187         };
2188
2189         dev_info->rx_desc_lim = rx_desc_lim;
2190         dev_info->tx_desc_lim = tx_desc_lim;
2191
2192         dev_info->hash_key_size = TXGBE_HKEY_MAX_INDEX * sizeof(uint32_t);
2193         dev_info->reta_size = ETH_RSS_RETA_SIZE_128;
2194         dev_info->flow_type_rss_offloads = TXGBE_RSS_OFFLOAD_ALL;
2195
2196         dev_info->speed_capa = ETH_LINK_SPEED_1G | ETH_LINK_SPEED_10G;
2197         dev_info->speed_capa |= ETH_LINK_SPEED_100M;
2198
2199         /* Driver-preferred Rx/Tx parameters */
2200         dev_info->default_rxportconf.burst_size = 32;
2201         dev_info->default_txportconf.burst_size = 32;
2202         dev_info->default_rxportconf.nb_queues = 1;
2203         dev_info->default_txportconf.nb_queues = 1;
2204         dev_info->default_rxportconf.ring_size = 256;
2205         dev_info->default_txportconf.ring_size = 256;
2206
2207         return 0;
2208 }
2209
2210 const uint32_t *
2211 txgbe_dev_supported_ptypes_get(struct rte_eth_dev *dev)
2212 {
2213         if (dev->rx_pkt_burst == txgbe_recv_pkts ||
2214             dev->rx_pkt_burst == txgbe_recv_pkts_lro_single_alloc ||
2215             dev->rx_pkt_burst == txgbe_recv_pkts_lro_bulk_alloc ||
2216             dev->rx_pkt_burst == txgbe_recv_pkts_bulk_alloc)
2217                 return txgbe_get_supported_ptypes();
2218
2219         return NULL;
2220 }
2221
2222 void
2223 txgbe_dev_setup_link_alarm_handler(void *param)
2224 {
2225         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2226         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2227         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2228         u32 speed;
2229         bool autoneg = false;
2230
2231         speed = hw->phy.autoneg_advertised;
2232         if (!speed)
2233                 hw->mac.get_link_capabilities(hw, &speed, &autoneg);
2234
2235         hw->mac.setup_link(hw, speed, true);
2236
2237         intr->flags &= ~TXGBE_FLAG_NEED_LINK_CONFIG;
2238 }
2239
2240 /* return 0 means link status changed, -1 means not changed */
2241 int
2242 txgbe_dev_link_update_share(struct rte_eth_dev *dev,
2243                             int wait_to_complete)
2244 {
2245         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2246         struct rte_eth_link link;
2247         u32 link_speed = TXGBE_LINK_SPEED_UNKNOWN;
2248         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2249         bool link_up;
2250         int err;
2251         int wait = 1;
2252
2253         memset(&link, 0, sizeof(link));
2254         link.link_status = ETH_LINK_DOWN;
2255         link.link_speed = ETH_SPEED_NUM_NONE;
2256         link.link_duplex = ETH_LINK_HALF_DUPLEX;
2257         link.link_autoneg = ETH_LINK_AUTONEG;
2258
2259         hw->mac.get_link_status = true;
2260
2261         if (intr->flags & TXGBE_FLAG_NEED_LINK_CONFIG)
2262                 return rte_eth_linkstatus_set(dev, &link);
2263
2264         /* check if it needs to wait to complete, if lsc interrupt is enabled */
2265         if (wait_to_complete == 0 || dev->data->dev_conf.intr_conf.lsc != 0)
2266                 wait = 0;
2267
2268         err = hw->mac.check_link(hw, &link_speed, &link_up, wait);
2269
2270         if (err != 0) {
2271                 link.link_speed = ETH_SPEED_NUM_100M;
2272                 link.link_duplex = ETH_LINK_FULL_DUPLEX;
2273                 return rte_eth_linkstatus_set(dev, &link);
2274         }
2275
2276         if (link_up == 0) {
2277                 if (hw->phy.media_type == txgbe_media_type_fiber) {
2278                         intr->flags |= TXGBE_FLAG_NEED_LINK_CONFIG;
2279                         rte_eal_alarm_set(10,
2280                                 txgbe_dev_setup_link_alarm_handler, dev);
2281                 }
2282                 return rte_eth_linkstatus_set(dev, &link);
2283         }
2284
2285         intr->flags &= ~TXGBE_FLAG_NEED_LINK_CONFIG;
2286         link.link_status = ETH_LINK_UP;
2287         link.link_duplex = ETH_LINK_FULL_DUPLEX;
2288
2289         switch (link_speed) {
2290         default:
2291         case TXGBE_LINK_SPEED_UNKNOWN:
2292                 link.link_duplex = ETH_LINK_FULL_DUPLEX;
2293                 link.link_speed = ETH_SPEED_NUM_100M;
2294                 break;
2295
2296         case TXGBE_LINK_SPEED_100M_FULL:
2297                 link.link_speed = ETH_SPEED_NUM_100M;
2298                 break;
2299
2300         case TXGBE_LINK_SPEED_1GB_FULL:
2301                 link.link_speed = ETH_SPEED_NUM_1G;
2302                 break;
2303
2304         case TXGBE_LINK_SPEED_2_5GB_FULL:
2305                 link.link_speed = ETH_SPEED_NUM_2_5G;
2306                 break;
2307
2308         case TXGBE_LINK_SPEED_5GB_FULL:
2309                 link.link_speed = ETH_SPEED_NUM_5G;
2310                 break;
2311
2312         case TXGBE_LINK_SPEED_10GB_FULL:
2313                 link.link_speed = ETH_SPEED_NUM_10G;
2314                 break;
2315         }
2316
2317         return rte_eth_linkstatus_set(dev, &link);
2318 }
2319
2320 static int
2321 txgbe_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
2322 {
2323         return txgbe_dev_link_update_share(dev, wait_to_complete);
2324 }
2325
2326 /**
2327  * It clears the interrupt causes and enables the interrupt.
2328  * It will be called once only during nic initialized.
2329  *
2330  * @param dev
2331  *  Pointer to struct rte_eth_dev.
2332  * @param on
2333  *  Enable or Disable.
2334  *
2335  * @return
2336  *  - On success, zero.
2337  *  - On failure, a negative value.
2338  */
2339 static int
2340 txgbe_dev_lsc_interrupt_setup(struct rte_eth_dev *dev, uint8_t on)
2341 {
2342         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2343
2344         txgbe_dev_link_status_print(dev);
2345         if (on)
2346                 intr->mask_misc |= TXGBE_ICRMISC_LSC;
2347         else
2348                 intr->mask_misc &= ~TXGBE_ICRMISC_LSC;
2349
2350         return 0;
2351 }
2352
2353 /**
2354  * It clears the interrupt causes and enables the interrupt.
2355  * It will be called once only during nic initialized.
2356  *
2357  * @param dev
2358  *  Pointer to struct rte_eth_dev.
2359  *
2360  * @return
2361  *  - On success, zero.
2362  *  - On failure, a negative value.
2363  */
2364 static int
2365 txgbe_dev_rxq_interrupt_setup(struct rte_eth_dev *dev)
2366 {
2367         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2368
2369         intr->mask[0] |= TXGBE_ICR_MASK;
2370         intr->mask[1] |= TXGBE_ICR_MASK;
2371
2372         return 0;
2373 }
2374
2375 /**
2376  * It clears the interrupt causes and enables the interrupt.
2377  * It will be called once only during nic initialized.
2378  *
2379  * @param dev
2380  *  Pointer to struct rte_eth_dev.
2381  *
2382  * @return
2383  *  - On success, zero.
2384  *  - On failure, a negative value.
2385  */
2386 static int
2387 txgbe_dev_macsec_interrupt_setup(struct rte_eth_dev *dev)
2388 {
2389         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2390
2391         intr->mask_misc |= TXGBE_ICRMISC_LNKSEC;
2392
2393         return 0;
2394 }
2395
2396 /*
2397  * It reads ICR and sets flag (TXGBE_ICRMISC_LSC) for the link_update.
2398  *
2399  * @param dev
2400  *  Pointer to struct rte_eth_dev.
2401  *
2402  * @return
2403  *  - On success, zero.
2404  *  - On failure, a negative value.
2405  */
2406 static int
2407 txgbe_dev_interrupt_get_status(struct rte_eth_dev *dev)
2408 {
2409         uint32_t eicr;
2410         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2411         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2412
2413         /* clear all cause mask */
2414         txgbe_disable_intr(hw);
2415
2416         /* read-on-clear nic registers here */
2417         eicr = ((u32 *)hw->isb_mem)[TXGBE_ISB_MISC];
2418         PMD_DRV_LOG(DEBUG, "eicr %x", eicr);
2419
2420         intr->flags = 0;
2421
2422         /* set flag for async link update */
2423         if (eicr & TXGBE_ICRMISC_LSC)
2424                 intr->flags |= TXGBE_FLAG_NEED_LINK_UPDATE;
2425
2426         if (eicr & TXGBE_ICRMISC_VFMBX)
2427                 intr->flags |= TXGBE_FLAG_MAILBOX;
2428
2429         if (eicr & TXGBE_ICRMISC_LNKSEC)
2430                 intr->flags |= TXGBE_FLAG_MACSEC;
2431
2432         if (eicr & TXGBE_ICRMISC_GPIO)
2433                 intr->flags |= TXGBE_FLAG_PHY_INTERRUPT;
2434
2435         return 0;
2436 }
2437
2438 /**
2439  * It gets and then prints the link status.
2440  *
2441  * @param dev
2442  *  Pointer to struct rte_eth_dev.
2443  *
2444  * @return
2445  *  - On success, zero.
2446  *  - On failure, a negative value.
2447  */
2448 static void
2449 txgbe_dev_link_status_print(struct rte_eth_dev *dev)
2450 {
2451         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2452         struct rte_eth_link link;
2453
2454         rte_eth_linkstatus_get(dev, &link);
2455
2456         if (link.link_status) {
2457                 PMD_INIT_LOG(INFO, "Port %d: Link Up - speed %u Mbps - %s",
2458                                         (int)(dev->data->port_id),
2459                                         (unsigned int)link.link_speed,
2460                         link.link_duplex == ETH_LINK_FULL_DUPLEX ?
2461                                         "full-duplex" : "half-duplex");
2462         } else {
2463                 PMD_INIT_LOG(INFO, " Port %d: Link Down",
2464                                 (int)(dev->data->port_id));
2465         }
2466         PMD_INIT_LOG(DEBUG, "PCI Address: " PCI_PRI_FMT,
2467                                 pci_dev->addr.domain,
2468                                 pci_dev->addr.bus,
2469                                 pci_dev->addr.devid,
2470                                 pci_dev->addr.function);
2471 }
2472
2473 /*
2474  * It executes link_update after knowing an interrupt occurred.
2475  *
2476  * @param dev
2477  *  Pointer to struct rte_eth_dev.
2478  *
2479  * @return
2480  *  - On success, zero.
2481  *  - On failure, a negative value.
2482  */
2483 static int
2484 txgbe_dev_interrupt_action(struct rte_eth_dev *dev,
2485                            struct rte_intr_handle *intr_handle)
2486 {
2487         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2488         int64_t timeout;
2489         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2490
2491         PMD_DRV_LOG(DEBUG, "intr action type %d", intr->flags);
2492
2493         if (intr->flags & TXGBE_FLAG_MAILBOX) {
2494                 txgbe_pf_mbx_process(dev);
2495                 intr->flags &= ~TXGBE_FLAG_MAILBOX;
2496         }
2497
2498         if (intr->flags & TXGBE_FLAG_PHY_INTERRUPT) {
2499                 hw->phy.handle_lasi(hw);
2500                 intr->flags &= ~TXGBE_FLAG_PHY_INTERRUPT;
2501         }
2502
2503         if (intr->flags & TXGBE_FLAG_NEED_LINK_UPDATE) {
2504                 struct rte_eth_link link;
2505
2506                 /*get the link status before link update, for predicting later*/
2507                 rte_eth_linkstatus_get(dev, &link);
2508
2509                 txgbe_dev_link_update(dev, 0);
2510
2511                 /* likely to up */
2512                 if (!link.link_status)
2513                         /* handle it 1 sec later, wait it being stable */
2514                         timeout = TXGBE_LINK_UP_CHECK_TIMEOUT;
2515                 /* likely to down */
2516                 else
2517                         /* handle it 4 sec later, wait it being stable */
2518                         timeout = TXGBE_LINK_DOWN_CHECK_TIMEOUT;
2519
2520                 txgbe_dev_link_status_print(dev);
2521                 if (rte_eal_alarm_set(timeout * 1000,
2522                                       txgbe_dev_interrupt_delayed_handler,
2523                                       (void *)dev) < 0) {
2524                         PMD_DRV_LOG(ERR, "Error setting alarm");
2525                 } else {
2526                         /* remember original mask */
2527                         intr->mask_misc_orig = intr->mask_misc;
2528                         /* only disable lsc interrupt */
2529                         intr->mask_misc &= ~TXGBE_ICRMISC_LSC;
2530                 }
2531         }
2532
2533         PMD_DRV_LOG(DEBUG, "enable intr immediately");
2534         txgbe_enable_intr(dev);
2535         rte_intr_enable(intr_handle);
2536
2537         return 0;
2538 }
2539
2540 /**
2541  * Interrupt handler which shall be registered for alarm callback for delayed
2542  * handling specific interrupt to wait for the stable nic state. As the
2543  * NIC interrupt state is not stable for txgbe after link is just down,
2544  * it needs to wait 4 seconds to get the stable status.
2545  *
2546  * @param handle
2547  *  Pointer to interrupt handle.
2548  * @param param
2549  *  The address of parameter (struct rte_eth_dev *) registered before.
2550  *
2551  * @return
2552  *  void
2553  */
2554 static void
2555 txgbe_dev_interrupt_delayed_handler(void *param)
2556 {
2557         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2558         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2559         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2560         struct txgbe_interrupt *intr = TXGBE_DEV_INTR(dev);
2561         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2562         uint32_t eicr;
2563
2564         txgbe_disable_intr(hw);
2565
2566         eicr = ((u32 *)hw->isb_mem)[TXGBE_ISB_MISC];
2567         if (eicr & TXGBE_ICRMISC_VFMBX)
2568                 txgbe_pf_mbx_process(dev);
2569
2570         if (intr->flags & TXGBE_FLAG_PHY_INTERRUPT) {
2571                 hw->phy.handle_lasi(hw);
2572                 intr->flags &= ~TXGBE_FLAG_PHY_INTERRUPT;
2573         }
2574
2575         if (intr->flags & TXGBE_FLAG_NEED_LINK_UPDATE) {
2576                 txgbe_dev_link_update(dev, 0);
2577                 intr->flags &= ~TXGBE_FLAG_NEED_LINK_UPDATE;
2578                 txgbe_dev_link_status_print(dev);
2579                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC,
2580                                               NULL);
2581         }
2582
2583         if (intr->flags & TXGBE_FLAG_MACSEC) {
2584                 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_MACSEC,
2585                                               NULL);
2586                 intr->flags &= ~TXGBE_FLAG_MACSEC;
2587         }
2588
2589         /* restore original mask */
2590         intr->mask_misc = intr->mask_misc_orig;
2591         intr->mask_misc_orig = 0;
2592
2593         PMD_DRV_LOG(DEBUG, "enable intr in delayed handler S[%08x]", eicr);
2594         txgbe_enable_intr(dev);
2595         rte_intr_enable(intr_handle);
2596 }
2597
2598 /**
2599  * Interrupt handler triggered by NIC  for handling
2600  * specific interrupt.
2601  *
2602  * @param handle
2603  *  Pointer to interrupt handle.
2604  * @param param
2605  *  The address of parameter (struct rte_eth_dev *) registered before.
2606  *
2607  * @return
2608  *  void
2609  */
2610 static void
2611 txgbe_dev_interrupt_handler(void *param)
2612 {
2613         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
2614
2615         txgbe_dev_interrupt_get_status(dev);
2616         txgbe_dev_interrupt_action(dev, dev->intr_handle);
2617 }
2618
2619 static int
2620 txgbe_add_rar(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
2621                                 uint32_t index, uint32_t pool)
2622 {
2623         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2624         uint32_t enable_addr = 1;
2625
2626         return txgbe_set_rar(hw, index, mac_addr->addr_bytes,
2627                              pool, enable_addr);
2628 }
2629
2630 static void
2631 txgbe_remove_rar(struct rte_eth_dev *dev, uint32_t index)
2632 {
2633         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2634
2635         txgbe_clear_rar(hw, index);
2636 }
2637
2638 static int
2639 txgbe_set_default_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *addr)
2640 {
2641         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2642
2643         txgbe_remove_rar(dev, 0);
2644         txgbe_add_rar(dev, addr, 0, pci_dev->max_vfs);
2645
2646         return 0;
2647 }
2648
2649 static uint32_t
2650 txgbe_uta_vector(struct txgbe_hw *hw, struct rte_ether_addr *uc_addr)
2651 {
2652         uint32_t vector = 0;
2653
2654         switch (hw->mac.mc_filter_type) {
2655         case 0:   /* use bits [47:36] of the address */
2656                 vector = ((uc_addr->addr_bytes[4] >> 4) |
2657                         (((uint16_t)uc_addr->addr_bytes[5]) << 4));
2658                 break;
2659         case 1:   /* use bits [46:35] of the address */
2660                 vector = ((uc_addr->addr_bytes[4] >> 3) |
2661                         (((uint16_t)uc_addr->addr_bytes[5]) << 5));
2662                 break;
2663         case 2:   /* use bits [45:34] of the address */
2664                 vector = ((uc_addr->addr_bytes[4] >> 2) |
2665                         (((uint16_t)uc_addr->addr_bytes[5]) << 6));
2666                 break;
2667         case 3:   /* use bits [43:32] of the address */
2668                 vector = ((uc_addr->addr_bytes[4]) |
2669                         (((uint16_t)uc_addr->addr_bytes[5]) << 8));
2670                 break;
2671         default:  /* Invalid mc_filter_type */
2672                 break;
2673         }
2674
2675         /* vector can only be 12-bits or boundary will be exceeded */
2676         vector &= 0xFFF;
2677         return vector;
2678 }
2679
2680 static int
2681 txgbe_uc_hash_table_set(struct rte_eth_dev *dev,
2682                         struct rte_ether_addr *mac_addr, uint8_t on)
2683 {
2684         uint32_t vector;
2685         uint32_t uta_idx;
2686         uint32_t reg_val;
2687         uint32_t uta_mask;
2688         uint32_t psrctl;
2689
2690         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2691         struct txgbe_uta_info *uta_info = TXGBE_DEV_UTA_INFO(dev);
2692
2693         /* The UTA table only exists on pf hardware */
2694         if (hw->mac.type < txgbe_mac_raptor)
2695                 return -ENOTSUP;
2696
2697         vector = txgbe_uta_vector(hw, mac_addr);
2698         uta_idx = (vector >> 5) & 0x7F;
2699         uta_mask = 0x1UL << (vector & 0x1F);
2700
2701         if (!!on == !!(uta_info->uta_shadow[uta_idx] & uta_mask))
2702                 return 0;
2703
2704         reg_val = rd32(hw, TXGBE_UCADDRTBL(uta_idx));
2705         if (on) {
2706                 uta_info->uta_in_use++;
2707                 reg_val |= uta_mask;
2708                 uta_info->uta_shadow[uta_idx] |= uta_mask;
2709         } else {
2710                 uta_info->uta_in_use--;
2711                 reg_val &= ~uta_mask;
2712                 uta_info->uta_shadow[uta_idx] &= ~uta_mask;
2713         }
2714
2715         wr32(hw, TXGBE_UCADDRTBL(uta_idx), reg_val);
2716
2717         psrctl = rd32(hw, TXGBE_PSRCTL);
2718         if (uta_info->uta_in_use > 0)
2719                 psrctl |= TXGBE_PSRCTL_UCHFENA;
2720         else
2721                 psrctl &= ~TXGBE_PSRCTL_UCHFENA;
2722
2723         psrctl &= ~TXGBE_PSRCTL_ADHF12_MASK;
2724         psrctl |= TXGBE_PSRCTL_ADHF12(hw->mac.mc_filter_type);
2725         wr32(hw, TXGBE_PSRCTL, psrctl);
2726
2727         return 0;
2728 }
2729
2730 static int
2731 txgbe_uc_all_hash_table_set(struct rte_eth_dev *dev, uint8_t on)
2732 {
2733         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2734         struct txgbe_uta_info *uta_info = TXGBE_DEV_UTA_INFO(dev);
2735         uint32_t psrctl;
2736         int i;
2737
2738         /* The UTA table only exists on pf hardware */
2739         if (hw->mac.type < txgbe_mac_raptor)
2740                 return -ENOTSUP;
2741
2742         if (on) {
2743                 for (i = 0; i < ETH_VMDQ_NUM_UC_HASH_ARRAY; i++) {
2744                         uta_info->uta_shadow[i] = ~0;
2745                         wr32(hw, TXGBE_UCADDRTBL(i), ~0);
2746                 }
2747         } else {
2748                 for (i = 0; i < ETH_VMDQ_NUM_UC_HASH_ARRAY; i++) {
2749                         uta_info->uta_shadow[i] = 0;
2750                         wr32(hw, TXGBE_UCADDRTBL(i), 0);
2751                 }
2752         }
2753
2754         psrctl = rd32(hw, TXGBE_PSRCTL);
2755         if (on)
2756                 psrctl |= TXGBE_PSRCTL_UCHFENA;
2757         else
2758                 psrctl &= ~TXGBE_PSRCTL_UCHFENA;
2759
2760         psrctl &= ~TXGBE_PSRCTL_ADHF12_MASK;
2761         psrctl |= TXGBE_PSRCTL_ADHF12(hw->mac.mc_filter_type);
2762         wr32(hw, TXGBE_PSRCTL, psrctl);
2763
2764         return 0;
2765 }
2766
2767 static int
2768 txgbe_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
2769 {
2770         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2771         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2772         uint32_t mask;
2773         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2774
2775         if (queue_id < 32) {
2776                 mask = rd32(hw, TXGBE_IMS(0));
2777                 mask &= (1 << queue_id);
2778                 wr32(hw, TXGBE_IMS(0), mask);
2779         } else if (queue_id < 64) {
2780                 mask = rd32(hw, TXGBE_IMS(1));
2781                 mask &= (1 << (queue_id - 32));
2782                 wr32(hw, TXGBE_IMS(1), mask);
2783         }
2784         rte_intr_enable(intr_handle);
2785
2786         return 0;
2787 }
2788
2789 static int
2790 txgbe_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
2791 {
2792         uint32_t mask;
2793         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2794
2795         if (queue_id < 32) {
2796                 mask = rd32(hw, TXGBE_IMS(0));
2797                 mask &= ~(1 << queue_id);
2798                 wr32(hw, TXGBE_IMS(0), mask);
2799         } else if (queue_id < 64) {
2800                 mask = rd32(hw, TXGBE_IMS(1));
2801                 mask &= ~(1 << (queue_id - 32));
2802                 wr32(hw, TXGBE_IMS(1), mask);
2803         }
2804
2805         return 0;
2806 }
2807
2808 /**
2809  * set the IVAR registers, mapping interrupt causes to vectors
2810  * @param hw
2811  *  pointer to txgbe_hw struct
2812  * @direction
2813  *  0 for Rx, 1 for Tx, -1 for other causes
2814  * @queue
2815  *  queue to map the corresponding interrupt to
2816  * @msix_vector
2817  *  the vector to map to the corresponding queue
2818  */
2819 void
2820 txgbe_set_ivar_map(struct txgbe_hw *hw, int8_t direction,
2821                    uint8_t queue, uint8_t msix_vector)
2822 {
2823         uint32_t tmp, idx;
2824
2825         if (direction == -1) {
2826                 /* other causes */
2827                 msix_vector |= TXGBE_IVARMISC_VLD;
2828                 idx = 0;
2829                 tmp = rd32(hw, TXGBE_IVARMISC);
2830                 tmp &= ~(0xFF << idx);
2831                 tmp |= (msix_vector << idx);
2832                 wr32(hw, TXGBE_IVARMISC, tmp);
2833         } else {
2834                 /* rx or tx causes */
2835                 /* Workround for ICR lost */
2836                 idx = ((16 * (queue & 1)) + (8 * direction));
2837                 tmp = rd32(hw, TXGBE_IVAR(queue >> 1));
2838                 tmp &= ~(0xFF << idx);
2839                 tmp |= (msix_vector << idx);
2840                 wr32(hw, TXGBE_IVAR(queue >> 1), tmp);
2841         }
2842 }
2843
2844 /**
2845  * Sets up the hardware to properly generate MSI-X interrupts
2846  * @hw
2847  *  board private structure
2848  */
2849 static void
2850 txgbe_configure_msix(struct rte_eth_dev *dev)
2851 {
2852         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2853         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2854         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2855         uint32_t queue_id, base = TXGBE_MISC_VEC_ID;
2856         uint32_t vec = TXGBE_MISC_VEC_ID;
2857         uint32_t gpie;
2858
2859         /* won't configure msix register if no mapping is done
2860          * between intr vector and event fd
2861          * but if misx has been enabled already, need to configure
2862          * auto clean, auto mask and throttling.
2863          */
2864         gpie = rd32(hw, TXGBE_GPIE);
2865         if (!rte_intr_dp_is_en(intr_handle) &&
2866             !(gpie & TXGBE_GPIE_MSIX))
2867                 return;
2868
2869         if (rte_intr_allow_others(intr_handle)) {
2870                 base = TXGBE_RX_VEC_START;
2871                 vec = base;
2872         }
2873
2874         /* setup GPIE for MSI-x mode */
2875         gpie = rd32(hw, TXGBE_GPIE);
2876         gpie |= TXGBE_GPIE_MSIX;
2877         wr32(hw, TXGBE_GPIE, gpie);
2878
2879         /* Populate the IVAR table and set the ITR values to the
2880          * corresponding register.
2881          */
2882         if (rte_intr_dp_is_en(intr_handle)) {
2883                 for (queue_id = 0; queue_id < dev->data->nb_rx_queues;
2884                         queue_id++) {
2885                         /* by default, 1:1 mapping */
2886                         txgbe_set_ivar_map(hw, 0, queue_id, vec);
2887                         intr_handle->intr_vec[queue_id] = vec;
2888                         if (vec < base + intr_handle->nb_efd - 1)
2889                                 vec++;
2890                 }
2891
2892                 txgbe_set_ivar_map(hw, -1, 1, TXGBE_MISC_VEC_ID);
2893         }
2894         wr32(hw, TXGBE_ITR(TXGBE_MISC_VEC_ID),
2895                         TXGBE_ITR_IVAL_10G(TXGBE_QUEUE_ITR_INTERVAL_DEFAULT)
2896                         | TXGBE_ITR_WRDSA);
2897 }
2898
2899 int
2900 txgbe_set_queue_rate_limit(struct rte_eth_dev *dev,
2901                            uint16_t queue_idx, uint16_t tx_rate)
2902 {
2903         struct txgbe_hw *hw = TXGBE_DEV_HW(dev);
2904         uint32_t bcnrc_val;
2905
2906         if (queue_idx >= hw->mac.max_tx_queues)
2907                 return -EINVAL;
2908
2909         if (tx_rate != 0) {
2910                 bcnrc_val = TXGBE_ARBTXRATE_MAX(tx_rate);
2911                 bcnrc_val |= TXGBE_ARBTXRATE_MIN(tx_rate / 2);
2912         } else {
2913                 bcnrc_val = 0;
2914         }
2915
2916         /*
2917          * Set global transmit compensation time to the MMW_SIZE in ARBTXMMW
2918          * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported.
2919          */
2920         wr32(hw, TXGBE_ARBTXMMW, 0x14);
2921
2922         /* Set ARBTXRATE of queue X */
2923         wr32(hw, TXGBE_ARBPOOLIDX, queue_idx);
2924         wr32(hw, TXGBE_ARBTXRATE, bcnrc_val);
2925         txgbe_flush(hw);
2926
2927         return 0;
2928 }
2929
2930 static u8 *
2931 txgbe_dev_addr_list_itr(__rte_unused struct txgbe_hw *hw,
2932                         u8 **mc_addr_ptr, u32 *vmdq)
2933 {
2934         u8 *mc_addr;
2935
2936         *vmdq = 0;
2937         mc_addr = *mc_addr_ptr;
2938         *mc_addr_ptr = (mc_addr + sizeof(struct rte_ether_addr));
2939         return mc_addr;
2940 }
2941
2942 int
2943 txgbe_dev_set_mc_addr_list(struct rte_eth_dev *dev,
2944                           struct rte_ether_addr *mc_addr_set,
2945                           uint32_t nb_mc_addr)
2946 {
2947         struct txgbe_hw *hw;
2948         u8 *mc_addr_list;
2949
2950         hw = TXGBE_DEV_HW(dev);
2951         mc_addr_list = (u8 *)mc_addr_set;
2952         return txgbe_update_mc_addr_list(hw, mc_addr_list, nb_mc_addr,
2953                                          txgbe_dev_addr_list_itr, TRUE);
2954 }
2955
2956 static const struct eth_dev_ops txgbe_eth_dev_ops = {
2957         .dev_configure              = txgbe_dev_configure,
2958         .dev_infos_get              = txgbe_dev_info_get,
2959         .dev_start                  = txgbe_dev_start,
2960         .dev_stop                   = txgbe_dev_stop,
2961         .dev_set_link_up            = txgbe_dev_set_link_up,
2962         .dev_set_link_down          = txgbe_dev_set_link_down,
2963         .dev_close                  = txgbe_dev_close,
2964         .dev_reset                  = txgbe_dev_reset,
2965         .link_update                = txgbe_dev_link_update,
2966         .stats_get                  = txgbe_dev_stats_get,
2967         .xstats_get                 = txgbe_dev_xstats_get,
2968         .xstats_get_by_id           = txgbe_dev_xstats_get_by_id,
2969         .stats_reset                = txgbe_dev_stats_reset,
2970         .xstats_reset               = txgbe_dev_xstats_reset,
2971         .xstats_get_names           = txgbe_dev_xstats_get_names,
2972         .xstats_get_names_by_id     = txgbe_dev_xstats_get_names_by_id,
2973         .queue_stats_mapping_set    = txgbe_dev_queue_stats_mapping_set,
2974         .dev_supported_ptypes_get   = txgbe_dev_supported_ptypes_get,
2975         .vlan_filter_set            = txgbe_vlan_filter_set,
2976         .vlan_tpid_set              = txgbe_vlan_tpid_set,
2977         .vlan_offload_set           = txgbe_vlan_offload_set,
2978         .vlan_strip_queue_set       = txgbe_vlan_strip_queue_set,
2979         .rx_queue_start             = txgbe_dev_rx_queue_start,
2980         .rx_queue_stop              = txgbe_dev_rx_queue_stop,
2981         .tx_queue_start             = txgbe_dev_tx_queue_start,
2982         .tx_queue_stop              = txgbe_dev_tx_queue_stop,
2983         .rx_queue_setup             = txgbe_dev_rx_queue_setup,
2984         .rx_queue_intr_enable       = txgbe_dev_rx_queue_intr_enable,
2985         .rx_queue_intr_disable      = txgbe_dev_rx_queue_intr_disable,
2986         .rx_queue_release           = txgbe_dev_rx_queue_release,
2987         .tx_queue_setup             = txgbe_dev_tx_queue_setup,
2988         .tx_queue_release           = txgbe_dev_tx_queue_release,
2989         .mac_addr_add               = txgbe_add_rar,
2990         .mac_addr_remove            = txgbe_remove_rar,
2991         .mac_addr_set               = txgbe_set_default_mac_addr,
2992         .uc_hash_table_set          = txgbe_uc_hash_table_set,
2993         .uc_all_hash_table_set      = txgbe_uc_all_hash_table_set,
2994         .set_queue_rate_limit       = txgbe_set_queue_rate_limit,
2995         .set_mc_addr_list           = txgbe_dev_set_mc_addr_list,
2996         .rxq_info_get               = txgbe_rxq_info_get,
2997         .txq_info_get               = txgbe_txq_info_get,
2998 };
2999
3000 RTE_PMD_REGISTER_PCI(net_txgbe, rte_txgbe_pmd);
3001 RTE_PMD_REGISTER_PCI_TABLE(net_txgbe, pci_id_txgbe_map);
3002 RTE_PMD_REGISTER_KMOD_DEP(net_txgbe, "* igb_uio | uio_pci_generic | vfio-pci");
3003
3004 RTE_LOG_REGISTER(txgbe_logtype_init, pmd.net.txgbe.init, NOTICE);
3005 RTE_LOG_REGISTER(txgbe_logtype_driver, pmd.net.txgbe.driver, NOTICE);
3006
3007 #ifdef RTE_LIBRTE_TXGBE_DEBUG_RX
3008         RTE_LOG_REGISTER(txgbe_logtype_rx, pmd.net.txgbe.rx, DEBUG);
3009 #endif
3010 #ifdef RTE_LIBRTE_TXGBE_DEBUG_TX
3011         RTE_LOG_REGISTER(txgbe_logtype_tx, pmd.net.txgbe.tx, DEBUG);
3012 #endif
3013
3014 #ifdef RTE_LIBRTE_TXGBE_DEBUG_TX_FREE
3015         RTE_LOG_REGISTER(txgbe_logtype_tx_free, pmd.net.txgbe.tx_free, DEBUG);
3016 #endif