063a9c6a6f7f77efd2e6b5b978d012180793f87b
[dpdk.git] / drivers / net / ipn3ke / ipn3ke_representor.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <unistd.h>
7
8 #include <rte_bus_pci.h>
9 #include <rte_ethdev.h>
10 #include <rte_pci.h>
11 #include <rte_malloc.h>
12
13 #include <rte_mbuf.h>
14 #include <rte_sched.h>
15 #include <ethdev_driver.h>
16 #include <rte_spinlock.h>
17
18 #include <rte_io.h>
19 #include <rte_rawdev.h>
20 #include <rte_rawdev_pmd.h>
21 #include <rte_bus_ifpga.h>
22 #include <ifpga_logs.h>
23
24 #include "ipn3ke_rawdev_api.h"
25 #include "ipn3ke_flow.h"
26 #include "ipn3ke_logs.h"
27 #include "ipn3ke_ethdev.h"
28
29 static int ipn3ke_rpst_scan_num;
30 static pthread_t ipn3ke_rpst_scan_thread;
31
32 /** Double linked list of representor port. */
33 TAILQ_HEAD(ipn3ke_rpst_list, ipn3ke_rpst);
34
35 static struct ipn3ke_rpst_list ipn3ke_rpst_list =
36         TAILQ_HEAD_INITIALIZER(ipn3ke_rpst_list);
37
38 static rte_spinlock_t ipn3ke_link_notify_list_lk = RTE_SPINLOCK_INITIALIZER;
39
40 static int
41 ipn3ke_rpst_link_check(struct ipn3ke_rpst *rpst);
42
43 static int
44 ipn3ke_rpst_dev_infos_get(struct rte_eth_dev *ethdev,
45         struct rte_eth_dev_info *dev_info)
46 {
47         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(ethdev);
48         struct ipn3ke_hw *hw = IPN3KE_DEV_PRIVATE_TO_HW(ethdev);
49
50         dev_info->speed_capa =
51                 (hw->retimer.mac_type ==
52                         IFPGA_RAWDEV_RETIMER_MAC_TYPE_10GE_XFI) ?
53                 ETH_LINK_SPEED_10G :
54                 ((hw->retimer.mac_type ==
55                         IFPGA_RAWDEV_RETIMER_MAC_TYPE_25GE_25GAUI) ?
56                 ETH_LINK_SPEED_25G :
57                 ETH_LINK_SPEED_AUTONEG);
58
59         dev_info->max_rx_queues  = 1;
60         dev_info->max_tx_queues  = 1;
61         dev_info->min_rx_bufsize = IPN3KE_AFU_BUF_SIZE_MIN;
62         dev_info->max_rx_pktlen  = IPN3KE_AFU_FRAME_SIZE_MAX;
63         dev_info->max_mac_addrs  = hw->port_num;
64         dev_info->max_vfs = 0;
65         dev_info->default_txconf = (struct rte_eth_txconf) {
66                 .offloads = 0,
67         };
68         dev_info->rx_queue_offload_capa = 0;
69         dev_info->rx_offload_capa =
70                 DEV_RX_OFFLOAD_VLAN_STRIP |
71                 DEV_RX_OFFLOAD_QINQ_STRIP |
72                 DEV_RX_OFFLOAD_IPV4_CKSUM |
73                 DEV_RX_OFFLOAD_UDP_CKSUM |
74                 DEV_RX_OFFLOAD_TCP_CKSUM |
75                 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
76                 DEV_RX_OFFLOAD_VLAN_EXTEND |
77                 DEV_RX_OFFLOAD_VLAN_FILTER;
78
79         dev_info->tx_queue_offload_capa = DEV_TX_OFFLOAD_MBUF_FAST_FREE;
80         dev_info->tx_offload_capa =
81                 DEV_TX_OFFLOAD_VLAN_INSERT |
82                 DEV_TX_OFFLOAD_QINQ_INSERT |
83                 DEV_TX_OFFLOAD_IPV4_CKSUM |
84                 DEV_TX_OFFLOAD_UDP_CKSUM |
85                 DEV_TX_OFFLOAD_TCP_CKSUM |
86                 DEV_TX_OFFLOAD_SCTP_CKSUM |
87                 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
88                 DEV_TX_OFFLOAD_TCP_TSO |
89                 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
90                 DEV_TX_OFFLOAD_GRE_TNL_TSO |
91                 DEV_TX_OFFLOAD_IPIP_TNL_TSO |
92                 DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
93                 DEV_TX_OFFLOAD_MULTI_SEGS |
94                 dev_info->tx_queue_offload_capa;
95
96         dev_info->dev_capa =
97                 RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP |
98                 RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP;
99
100         dev_info->switch_info.name = ethdev->device->name;
101         dev_info->switch_info.domain_id = rpst->switch_domain_id;
102         dev_info->switch_info.port_id = rpst->port_id;
103
104         return 0;
105 }
106
107 static int
108 ipn3ke_rpst_dev_configure(__rte_unused struct rte_eth_dev *dev)
109 {
110         return 0;
111 }
112
113 static int
114 ipn3ke_rpst_dev_start(struct rte_eth_dev *dev)
115 {
116         struct ipn3ke_hw *hw = IPN3KE_DEV_PRIVATE_TO_HW(dev);
117         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(dev);
118         struct rte_rawdev *rawdev;
119         uint64_t base_mac;
120         uint32_t val;
121         char attr_name[IPN3KE_RAWDEV_ATTR_LEN_MAX];
122
123         rawdev = hw->rawdev;
124
125         memset(attr_name, 0, sizeof(attr_name));
126         snprintf(attr_name, IPN3KE_RAWDEV_ATTR_LEN_MAX, "%s",
127                         "LineSideBaseMAC");
128         rawdev->dev_ops->attr_get(rawdev, attr_name, &base_mac);
129         rte_ether_addr_copy((struct rte_ether_addr *)&base_mac,
130                         &rpst->mac_addr);
131
132         rte_ether_addr_copy(&rpst->mac_addr, &dev->data->mac_addrs[0]);
133         dev->data->mac_addrs->addr_bytes[RTE_ETHER_ADDR_LEN - 1] =
134                 (uint8_t)rpst->port_id + 1;
135
136         if (hw->retimer.mac_type == IFPGA_RAWDEV_RETIMER_MAC_TYPE_10GE_XFI) {
137                 /* Set mac address */
138                 rte_memcpy(((char *)(&val)),
139                         (char *)&dev->data->mac_addrs->addr_bytes[0],
140                         sizeof(uint32_t));
141                 (*hw->f_mac_write)(hw,
142                                 val,
143                                 IPN3KE_MAC_PRIMARY_MAC_ADDR0,
144                                 rpst->port_id,
145                                 0);
146                 rte_memcpy(((char *)(&val)),
147                         (char *)&dev->data->mac_addrs->addr_bytes[4],
148                         sizeof(uint16_t));
149                 (*hw->f_mac_write)(hw,
150                                 val,
151                                 IPN3KE_MAC_PRIMARY_MAC_ADDR1,
152                                 rpst->port_id,
153                                 0);
154
155                 /* Enable the TX path */
156                 ipn3ke_xmac_tx_enable(hw, rpst->port_id, 0);
157
158                 /* Disables source address override */
159                 ipn3ke_xmac_smac_ovd_dis(hw, rpst->port_id, 0);
160
161                 /* Enable the RX path */
162                 ipn3ke_xmac_rx_enable(hw, rpst->port_id, 0);
163
164                 /* Clear line side TX statistics counters */
165                 ipn3ke_xmac_tx_clr_10G_stcs(hw, rpst->port_id, 0);
166
167                 /* Clear line side RX statistics counters */
168                 ipn3ke_xmac_rx_clr_10G_stcs(hw, rpst->port_id, 0);
169
170                 /* Clear NIC side TX statistics counters */
171                 ipn3ke_xmac_tx_clr_10G_stcs(hw, rpst->port_id, 1);
172
173                 /* Clear NIC side RX statistics counters */
174                 ipn3ke_xmac_rx_clr_10G_stcs(hw, rpst->port_id, 1);
175         } else if (hw->retimer.mac_type ==
176                                 IFPGA_RAWDEV_RETIMER_MAC_TYPE_25GE_25GAUI) {
177                 /* Clear line side TX statistics counters */
178                 ipn3ke_xmac_tx_clr_25G_stcs(hw, rpst->port_id, 0);
179
180                 /* Clear line side RX statistics counters */
181                 ipn3ke_xmac_rx_clr_25G_stcs(hw, rpst->port_id, 0);
182
183                 /* Clear NIC side TX statistics counters */
184                 ipn3ke_xmac_tx_clr_25G_stcs(hw, rpst->port_id, 1);
185
186                 /* Clear NIC side RX statistics counters */
187                 ipn3ke_xmac_rx_clr_25G_stcs(hw, rpst->port_id, 1);
188         }
189
190         ipn3ke_rpst_link_update(dev, 0);
191
192         return 0;
193 }
194
195 static int
196 ipn3ke_rpst_dev_stop(struct rte_eth_dev *dev)
197 {
198         struct ipn3ke_hw *hw = IPN3KE_DEV_PRIVATE_TO_HW(dev);
199         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(dev);
200
201         if (hw->retimer.mac_type == IFPGA_RAWDEV_RETIMER_MAC_TYPE_10GE_XFI) {
202                 /* Disable the TX path */
203                 ipn3ke_xmac_tx_disable(hw, rpst->port_id, 0);
204
205                 /* Disable the RX path */
206                 ipn3ke_xmac_rx_disable(hw, rpst->port_id, 0);
207         }
208
209         return 0;
210 }
211
212 static int
213 ipn3ke_rpst_dev_close(struct rte_eth_dev *dev)
214 {
215         struct ipn3ke_hw *hw = IPN3KE_DEV_PRIVATE_TO_HW(dev);
216         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(dev);
217
218         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
219                 return 0;
220
221         if (hw->retimer.mac_type == IFPGA_RAWDEV_RETIMER_MAC_TYPE_10GE_XFI) {
222                 /* Disable the TX path */
223                 ipn3ke_xmac_tx_disable(hw, rpst->port_id, 0);
224
225                 /* Disable the RX path */
226                 ipn3ke_xmac_rx_disable(hw, rpst->port_id, 0);
227         }
228
229         return 0;
230 }
231
232 /*
233  * Reset PF device only to re-initialize resources in PMD layer
234  */
235 static int
236 ipn3ke_rpst_dev_reset(struct rte_eth_dev *dev)
237 {
238         struct ipn3ke_hw *hw = IPN3KE_DEV_PRIVATE_TO_HW(dev);
239         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(dev);
240
241         if (hw->retimer.mac_type == IFPGA_RAWDEV_RETIMER_MAC_TYPE_10GE_XFI) {
242                 /* Disable the TX path */
243                 ipn3ke_xmac_tx_disable(hw, rpst->port_id, 0);
244
245                 /* Disable the RX path */
246                 ipn3ke_xmac_rx_disable(hw, rpst->port_id, 0);
247         }
248
249         return 0;
250 }
251
252 static int
253 ipn3ke_rpst_rx_queue_start(__rte_unused struct rte_eth_dev *dev,
254         __rte_unused uint16_t rx_queue_id)
255 {
256         return 0;
257 }
258
259 static int
260 ipn3ke_rpst_rx_queue_stop(__rte_unused struct rte_eth_dev *dev,
261         __rte_unused uint16_t rx_queue_id)
262 {
263         return 0;
264 }
265
266 static int
267 ipn3ke_rpst_tx_queue_start(__rte_unused struct rte_eth_dev *dev,
268         __rte_unused uint16_t tx_queue_id)
269 {
270         return 0;
271 }
272
273 static int
274 ipn3ke_rpst_tx_queue_stop(__rte_unused struct rte_eth_dev *dev,
275         __rte_unused uint16_t tx_queue_id)
276 {
277         return 0;
278 }
279
280 static int
281 ipn3ke_rpst_rx_queue_setup(__rte_unused struct rte_eth_dev *dev,
282         __rte_unused uint16_t queue_idx, __rte_unused uint16_t nb_desc,
283         __rte_unused unsigned int socket_id,
284         __rte_unused const struct rte_eth_rxconf *rx_conf,
285         __rte_unused struct rte_mempool *mp)
286 {
287         return 0;
288 }
289
290 static int
291 ipn3ke_rpst_tx_queue_setup(__rte_unused struct rte_eth_dev *dev,
292         __rte_unused uint16_t queue_idx, __rte_unused uint16_t nb_desc,
293         __rte_unused unsigned int socket_id,
294         __rte_unused const struct rte_eth_txconf *tx_conf)
295 {
296         return 0;
297 }
298
299 /* Statistics collected by each port, VSI, VEB, and S-channel */
300 struct ipn3ke_rpst_eth_stats {
301         uint64_t tx_bytes;               /* gotc */
302         uint64_t tx_multicast;           /* mptc */
303         uint64_t tx_broadcast;           /* bptc */
304         uint64_t tx_unicast;             /* uptc */
305         uint64_t tx_discards;            /* tdpc */
306         uint64_t tx_errors;              /* tepc */
307         uint64_t rx_bytes;               /* gorc */
308         uint64_t rx_multicast;           /* mprc */
309         uint64_t rx_broadcast;           /* bprc */
310         uint64_t rx_unicast;             /* uprc */
311         uint64_t rx_discards;            /* rdpc */
312         uint64_t rx_unknown_protocol;    /* rupp */
313 };
314
315 /* store statistics names and its offset in stats structure */
316 struct ipn3ke_rpst_xstats_name_offset {
317         char name[RTE_ETH_XSTATS_NAME_SIZE];
318         unsigned int offset;
319 };
320
321 static const struct ipn3ke_rpst_xstats_name_offset
322 ipn3ke_rpst_stats_strings[] = {
323         {"tx_multicast_packets",          offsetof(struct ipn3ke_rpst_eth_stats,
324                                                         tx_multicast)},
325         {"tx_broadcast_packets",          offsetof(struct ipn3ke_rpst_eth_stats,
326                                                         tx_broadcast)},
327         {"tx_unicast_packets",            offsetof(struct ipn3ke_rpst_eth_stats,
328                                                         tx_unicast)},
329         {"tx_dropped_packets",            offsetof(struct ipn3ke_rpst_eth_stats,
330                                                         tx_discards)},
331         {"rx_multicast_packets",          offsetof(struct ipn3ke_rpst_eth_stats,
332                                                         rx_multicast)},
333         {"rx_broadcast_packets",          offsetof(struct ipn3ke_rpst_eth_stats,
334                                                         rx_broadcast)},
335         {"rx_unicast_packets",            offsetof(struct ipn3ke_rpst_eth_stats,
336                                                         rx_unicast)},
337         {"rx_dropped_packets",            offsetof(struct ipn3ke_rpst_eth_stats,
338                                                         rx_discards)},
339         {"rx_unknown_protocol_packets", offsetof(struct ipn3ke_rpst_eth_stats,
340                                                         rx_unknown_protocol)},
341 };
342
343 #define IPN3KE_RPST_ETH_XSTATS_CNT (sizeof(ipn3ke_rpst_stats_strings) / \
344                 sizeof(ipn3ke_rpst_stats_strings[0]))
345
346 #define IPN3KE_RPST_PRIO_XSTATS_CNT    8
347
348 /* Statistics collected by the MAC */
349 struct ipn3ke_rpst_hw_port_stats {
350         /* eth stats collected by the port */
351         struct ipn3ke_rpst_eth_stats eth;
352
353         /* additional port specific stats */
354         uint64_t tx_dropped_link_down;
355         uint64_t crc_errors;
356         uint64_t illegal_bytes;
357         uint64_t error_bytes;
358         uint64_t mac_local_faults;
359         uint64_t mac_remote_faults;
360         uint64_t rx_length_errors;
361         uint64_t link_xon_rx;
362         uint64_t link_xoff_rx;
363         uint64_t priority_xon_rx[IPN3KE_RPST_PRIO_XSTATS_CNT];
364         uint64_t priority_xoff_rx[IPN3KE_RPST_PRIO_XSTATS_CNT];
365         uint64_t link_xon_tx;
366         uint64_t link_xoff_tx;
367         uint64_t priority_xon_tx[IPN3KE_RPST_PRIO_XSTATS_CNT];
368         uint64_t priority_xoff_tx[IPN3KE_RPST_PRIO_XSTATS_CNT];
369         uint64_t priority_xon_2_xoff[IPN3KE_RPST_PRIO_XSTATS_CNT];
370         uint64_t rx_size_64;
371         uint64_t rx_size_65_127;
372         uint64_t rx_size_128_255;
373         uint64_t rx_size_256_511;
374         uint64_t rx_size_512_1023;
375         uint64_t rx_size_1024_1518;
376         uint64_t rx_size_big;
377         uint64_t rx_undersize;
378         uint64_t rx_fragments;
379         uint64_t rx_oversize;
380         uint64_t rx_jabber;
381         uint64_t tx_size_64;
382         uint64_t tx_size_65_127;
383         uint64_t tx_size_128_255;
384         uint64_t tx_size_256_511;
385         uint64_t tx_size_512_1023;
386         uint64_t tx_size_1024_1518;
387         uint64_t tx_size_1519_to_max;
388         uint64_t mac_short_packet_dropped;
389         uint64_t checksum_error;
390         /* flow director stats */
391         uint64_t fd_atr_match;
392         uint64_t fd_sb_match;
393         uint64_t fd_atr_tunnel_match;
394         uint32_t fd_atr_status;
395         uint32_t fd_sb_status;
396         /* EEE LPI */
397         uint32_t tx_lpi_status;
398         uint32_t rx_lpi_status;
399         uint64_t tx_lpi_count;
400         uint64_t rx_lpi_count;
401 };
402
403 static const struct ipn3ke_rpst_xstats_name_offset
404 ipn3ke_rpst_hw_port_strings[] = {
405         {"tx_link_down_dropped",      offsetof(struct ipn3ke_rpst_hw_port_stats,
406                                                 tx_dropped_link_down)},
407         {"rx_crc_errors",             offsetof(struct ipn3ke_rpst_hw_port_stats,
408                                                 crc_errors)},
409         {"rx_illegal_byte_errors",    offsetof(struct ipn3ke_rpst_hw_port_stats,
410                                                 illegal_bytes)},
411         {"rx_error_bytes",            offsetof(struct ipn3ke_rpst_hw_port_stats,
412                                                 error_bytes)},
413         {"mac_local_errors",          offsetof(struct ipn3ke_rpst_hw_port_stats,
414                                                 mac_local_faults)},
415         {"mac_remote_errors",         offsetof(struct ipn3ke_rpst_hw_port_stats,
416                                                 mac_remote_faults)},
417         {"rx_length_errors",          offsetof(struct ipn3ke_rpst_hw_port_stats,
418                                                 rx_length_errors)},
419         {"tx_xon_packets",            offsetof(struct ipn3ke_rpst_hw_port_stats,
420                                                 link_xon_tx)},
421         {"rx_xon_packets",            offsetof(struct ipn3ke_rpst_hw_port_stats,
422                                                 link_xon_rx)},
423         {"tx_xoff_packets",           offsetof(struct ipn3ke_rpst_hw_port_stats,
424                                                 link_xoff_tx)},
425         {"rx_xoff_packets",           offsetof(struct ipn3ke_rpst_hw_port_stats,
426                                                 link_xoff_rx)},
427         {"rx_size_64_packets",        offsetof(struct ipn3ke_rpst_hw_port_stats,
428                                                 rx_size_64)},
429         {"rx_size_65_to_127_packets", offsetof(struct ipn3ke_rpst_hw_port_stats,
430                                                 rx_size_65_127)},
431         {"rx_size_128_to_255_packets",
432                                 offsetof(struct ipn3ke_rpst_hw_port_stats,
433                                          rx_size_128_255)},
434         {"rx_size_256_to_511_packets",
435                                 offsetof(struct ipn3ke_rpst_hw_port_stats,
436                                          rx_size_256_511)},
437         {"rx_size_512_to_1023_packets",
438                                 offsetof(struct ipn3ke_rpst_hw_port_stats,
439                                          rx_size_512_1023)},
440         {"rx_size_1024_to_1518_packets",
441                                 offsetof(struct ipn3ke_rpst_hw_port_stats,
442                                          rx_size_1024_1518)},
443         {"rx_size_1519_to_max_packets",
444                                 offsetof(struct ipn3ke_rpst_hw_port_stats,
445                                          rx_size_big)},
446         {"rx_undersized_errors",      offsetof(struct ipn3ke_rpst_hw_port_stats,
447                                                rx_undersize)},
448         {"rx_oversize_errors",        offsetof(struct ipn3ke_rpst_hw_port_stats,
449                                                rx_oversize)},
450         {"rx_mac_short_dropped",      offsetof(struct ipn3ke_rpst_hw_port_stats,
451                                                mac_short_packet_dropped)},
452         {"rx_fragmented_errors",      offsetof(struct ipn3ke_rpst_hw_port_stats,
453                                                rx_fragments)},
454         {"rx_jabber_errors",          offsetof(struct ipn3ke_rpst_hw_port_stats,
455                                                rx_jabber)},
456         {"tx_size_64_packets",        offsetof(struct ipn3ke_rpst_hw_port_stats,
457                                                tx_size_64)},
458         {"tx_size_65_to_127_packets",
459                                 offsetof(struct ipn3ke_rpst_hw_port_stats,
460                                          tx_size_65_127)},
461         {"tx_size_128_to_255_packets",
462                                 offsetof(struct ipn3ke_rpst_hw_port_stats,
463                                          tx_size_128_255)},
464         {"tx_size_256_to_511_packets",
465                                 offsetof(struct ipn3ke_rpst_hw_port_stats,
466                                          tx_size_256_511)},
467         {"tx_size_512_to_1023_packets",
468                                 offsetof(struct ipn3ke_rpst_hw_port_stats,
469                                          tx_size_512_1023)},
470         {"tx_size_1024_to_1518_packets",
471                                 offsetof(struct ipn3ke_rpst_hw_port_stats,
472                                          tx_size_1024_1518)},
473         {"tx_size_1519_to_max_packets",
474                                 offsetof(struct ipn3ke_rpst_hw_port_stats,
475                                          tx_size_1519_to_max)},
476         {"rx_flow_director_atr_match_packets",
477                                 offsetof(struct ipn3ke_rpst_hw_port_stats,
478                                          fd_atr_match)},
479         {"rx_flow_director_sb_match_packets",
480                                 offsetof(struct ipn3ke_rpst_hw_port_stats,
481                                          fd_sb_match)},
482         {"tx_low_power_idle_status",  offsetof(struct ipn3ke_rpst_hw_port_stats,
483                                                tx_lpi_status)},
484         {"rx_low_power_idle_status",  offsetof(struct ipn3ke_rpst_hw_port_stats,
485                                                rx_lpi_status)},
486         {"tx_low_power_idle_count",   offsetof(struct ipn3ke_rpst_hw_port_stats,
487                                                tx_lpi_count)},
488         {"rx_low_power_idle_count",   offsetof(struct ipn3ke_rpst_hw_port_stats,
489                                                rx_lpi_count)},
490 };
491
492 #define IPN3KE_RPST_HW_PORT_XSTATS_CNT (sizeof(ipn3ke_rpst_hw_port_strings) \
493                 / sizeof(ipn3ke_rpst_hw_port_strings[0]))
494
495 static const struct ipn3ke_rpst_xstats_name_offset
496 ipn3ke_rpst_rxq_prio_strings[] = {
497         {"xon_packets",               offsetof(struct ipn3ke_rpst_hw_port_stats,
498                                                priority_xon_rx)},
499         {"xoff_packets",              offsetof(struct ipn3ke_rpst_hw_port_stats,
500                                                priority_xoff_rx)},
501 };
502
503 #define IPN3KE_RPST_RXQ_PRIO_XSTATS_CNT (sizeof(ipn3ke_rpst_rxq_prio_strings) \
504                 / sizeof(ipn3ke_rpst_rxq_prio_strings[0]))
505
506 static const struct ipn3ke_rpst_xstats_name_offset
507 ipn3ke_rpst_txq_prio_strings[] = {
508         {"xon_packets",               offsetof(struct ipn3ke_rpst_hw_port_stats,
509                                                priority_xon_tx)},
510         {"xoff_packets",              offsetof(struct ipn3ke_rpst_hw_port_stats,
511                                                priority_xoff_tx)},
512         {"xon_to_xoff_packets",       offsetof(struct ipn3ke_rpst_hw_port_stats,
513                                                priority_xon_2_xoff)},
514 };
515
516 #define IPN3KE_RPST_TXQ_PRIO_XSTATS_CNT (sizeof(ipn3ke_rpst_txq_prio_strings) \
517                 / sizeof(ipn3ke_rpst_txq_prio_strings[0]))
518
519 static uint32_t
520 ipn3ke_rpst_xstats_calc_num(void)
521 {
522         return IPN3KE_RPST_ETH_XSTATS_CNT
523                 + IPN3KE_RPST_HW_PORT_XSTATS_CNT
524                 + (IPN3KE_RPST_RXQ_PRIO_XSTATS_CNT
525                         * IPN3KE_RPST_PRIO_XSTATS_CNT)
526                 + (IPN3KE_RPST_TXQ_PRIO_XSTATS_CNT
527                         * IPN3KE_RPST_PRIO_XSTATS_CNT);
528 }
529
530 static void
531 ipn3ke_rpst_25g_nic_side_tx_stats_reset(struct ipn3ke_hw *hw,
532 uint16_t port_id)
533 {
534         uint32_t tmp = 0x00000001;
535         /* Bit[0]: Software can set this bit to the value of 1
536          * to reset all of the TX statistics registers at the same time.
537          * This bit is selfclearing.
538          */
539         (*hw->f_mac_write)(hw,
540                         tmp,
541                         IPN3KE_25G_TX_STATISTICS_CONFIG,
542                         port_id,
543                         1);
544
545         while (tmp & 0x00000001) {
546                 tmp = 0x00000000;
547                 (*hw->f_mac_read)(hw,
548                                 &tmp,
549                                 IPN3KE_25G_TX_STATISTICS_CONFIG,
550                                 port_id,
551                                 1);
552                 if (tmp & 0x00000001)
553                         usleep(5);
554                 else
555                         return;
556         }
557 }
558
559 static void
560 ipn3ke_rpst_25g_nic_side_rx_stats_reset(struct ipn3ke_hw *hw,
561 uint16_t port_id)
562 {
563         uint32_t tmp = 0x00000001;
564         /* Bit[0]: Software can set this bit to the value of 1
565          * to reset all of the RX statistics registers at the same time.
566          * This bit is selfclearing.
567          */
568         (*hw->f_mac_write)(hw,
569                         tmp,
570                         IPN3KE_25G_RX_STATISTICS_CONFIG,
571                         port_id,
572                         1);
573
574         while (tmp & 0x00000001) {
575                 tmp = 0x00000000;
576                 (*hw->f_mac_read)(hw,
577                                 &tmp,
578                                 IPN3KE_25G_RX_STATISTICS_CONFIG,
579                                 port_id,
580                                 1);
581                 if (tmp & 0x00000001)
582                         usleep(5);
583                 else
584                         return;
585         }
586 }
587
588 static void
589 ipn3ke_rpst_10g_nic_side_tx_stats_reset(struct ipn3ke_hw *hw,
590 uint16_t port_id)
591 {
592         uint32_t tmp;
593
594         /*Bit [0]: Set this register to 1 to clear all TX statistics
595          *counters.
596          *The IP core clears this bit when all counters are cleared.
597          *Bits [31:1]: Reserved.
598          */
599         tmp = 0x00000000;
600         (*hw->f_mac_read)(hw,
601                 &tmp,
602                 IPN3KE_10G_TX_STATS_CLR,
603                 port_id,
604                 1);
605         tmp |= 0x00000001;
606         (*hw->f_mac_write)(hw,
607                 tmp,
608                 IPN3KE_10G_TX_STATS_CLR,
609                 port_id,
610                 1);
611 }
612
613 static void
614 ipn3ke_rpst_10g_nic_side_rx_stats_reset(struct ipn3ke_hw *hw,
615 uint16_t port_id)
616 {
617         uint32_t tmp;
618
619         /*Bit [0]: Set this register to 1 to clear all RX statistics
620          *counters.
621          *The IP core clears this bit when all counters are cleared.
622          *Bits [31:1]: Reserved
623          */
624         tmp = 0x00000000;
625         (*hw->f_mac_read)(hw,
626                 &tmp,
627                 IPN3KE_10G_RX_STATS_CLR,
628                 port_id,
629                 1);
630         tmp |= 0x00000001;
631         (*hw->f_mac_write)(hw,
632                 tmp,
633                 IPN3KE_10G_RX_STATS_CLR,
634                 port_id,
635                 1);
636 }
637
638 static uint64_t
639 ipn3ke_rpst_read_64bits_statistics_register(uint32_t addr_lo,
640 uint32_t addr_hi, struct ipn3ke_hw *hw, uint16_t port_id)
641 {
642         uint32_t statistics_lo = 0x00000000;
643         uint32_t statistics_hi = 0x00000000;
644         uint64_t statistics = 0x0000000000000000;
645
646         (*hw->f_mac_read)(hw,
647                         &statistics_lo,
648                         addr_lo,
649                         port_id,
650                         0);
651
652         (*hw->f_mac_read)(hw,
653                         &statistics_hi,
654                         addr_hi,
655                         port_id,
656                         0);
657
658         statistics += statistics_hi;
659         statistics = statistics << IPN3KE_REGISTER_WIDTH;
660         statistics += statistics_lo;
661         return statistics;
662
663 }
664
665 static int
666 ipn3ke_rpst_read_25g_lineside_stats_registers
667 (struct ipn3ke_hw *hw,
668 uint16_t port_id,
669 struct ipn3ke_rpst_hw_port_stats *hw_stats)
670 {
671         uint32_t tmp;
672         uint64_t statistics;
673
674         memset(hw_stats, 0, sizeof(*hw_stats));
675
676         /*check Tx statistics is real time.
677          *if statistics has been paused, make it real time.
678          */
679         tmp = 0x00000000;
680         (*hw->f_mac_read)(hw,
681                         &tmp,
682                         IPN3KE_25G_TX_STATISTICS_CONFIG,
683                         port_id,
684                         0);
685
686         if (tmp & IPN3KE_25G_TX_STATISTICS_CONFIG_SHADOW_REQUEST_MASK) {
687                 tmp &= 0xfffffffb;
688                 (*hw->f_mac_write)(hw,
689                         tmp,
690                         IPN3KE_25G_TX_STATISTICS_CONFIG,
691                         port_id,
692                         0);
693         }
694
695         tmp = 0x00000000;
696         (*hw->f_mac_read)(hw,
697                 &tmp,
698                 IPN3KE_25G_TX_STATISTICS_STATUS,
699                 port_id,
700                 0);
701         if (tmp & IPN3KE_25G_TX_STATISTICS_STATUS_SHADOW_REQUEST_MASK) {
702                 tmp = 0x00000000;
703                 (*hw->f_mac_read)(hw,
704                         &tmp,
705                         IPN3KE_25G_TX_STATISTICS_CONFIG,
706                         port_id,
707                         0);
708                 tmp &= 0xfffffffb;
709                 (*hw->f_mac_write)(hw,
710                         tmp,
711                         IPN3KE_25G_TX_STATISTICS_CONFIG,
712                         port_id,
713                         0);
714         }
715
716         /*check Rx statistics is real time.
717          *if statistics has been paused, make it real time.
718          */
719         tmp = 0x00000000;
720         (*hw->f_mac_read)(hw,
721                         &tmp,
722                         IPN3KE_25G_RX_STATISTICS_CONFIG,
723                         port_id,
724                         0);
725         if (tmp & IPN3KE_25G_RX_STATISTICS_CONFIG_SHADOW_REQUEST_MASK) {
726                 tmp &= 0xfffffffb;
727                 (*hw->f_mac_write)(hw,
728                         tmp,
729                         IPN3KE_25G_RX_STATISTICS_CONFIG,
730                         port_id,
731                         0);
732         }
733
734         tmp = 0x00000000;
735         (*hw->f_mac_read)(hw,
736                 &tmp,
737                 IPN3KE_25G_RX_STATISTICS_STATUS,
738                 port_id,
739                 0);
740
741         if (tmp & IPN3KE_25G_RX_STATISTICS_STATUS_SHADOW_REQUEST_MASK) {
742                 tmp = 0x00000000;
743                 (*hw->f_mac_read)(hw,
744                         &tmp,
745                         IPN3KE_25G_RX_STATISTICS_CONFIG,
746                         port_id,
747                         0);
748                 tmp &= 0xfffffffb;
749                 (*hw->f_mac_write)(hw,
750                         tmp,
751                         IPN3KE_25G_RX_STATISTICS_CONFIG,
752                         port_id,
753                         0);
754         }
755
756         /* pause Tx counter to read the statistics */
757         tmp = 0x00000000;
758         (*hw->f_mac_read)(hw,
759                 &tmp,
760                 IPN3KE_25G_TX_STATISTICS_CONFIG,
761                 port_id,
762                 0);
763         tmp |= 0x00000004;
764         (*hw->f_mac_write)(hw,
765                 tmp,
766                 IPN3KE_25G_TX_STATISTICS_CONFIG,
767                 port_id,
768                 0);
769
770         /* pause Rx counter to read the statistics */
771         tmp = 0x00000000;
772         (*hw->f_mac_read)(hw,
773                 &tmp,
774                 IPN3KE_25G_RX_STATISTICS_CONFIG,
775                 port_id,
776                 0);
777         tmp |= 0x00000004;
778         (*hw->f_mac_write)(hw,
779                 tmp,
780                 IPN3KE_25G_RX_STATISTICS_CONFIG,
781                 port_id,
782                 0);
783
784         /*Number of transmitted frames less than 64 bytes
785          *and reporting a CRC error
786          */
787         statistics = ipn3ke_rpst_read_64bits_statistics_register(
788                 IPN3KE_25G_CNTR_TX_FRAGMENTS_LO,
789                 IPN3KE_25G_CNTR_TX_FRAGMENTS_HI,
790                 hw, port_id);
791         hw_stats->eth.tx_errors += statistics;
792         hw_stats->crc_errors += statistics;
793
794         /*Number of transmitted oversized frames reporting a CRC error*/
795         statistics = ipn3ke_rpst_read_64bits_statistics_register(
796                 IPN3KE_25G_CNTR_TX_JABBERS_LO,
797                 IPN3KE_25G_CNTR_TX_JABBERS_HI,
798                 hw, port_id);
799         hw_stats->eth.tx_errors += statistics;
800         hw_stats->crc_errors += statistics;
801
802         /* Number of transmitted packets with FCS errors */
803         statistics = ipn3ke_rpst_read_64bits_statistics_register(
804                 IPN3KE_25G_CNTR_TX_FCS_LO,
805                 IPN3KE_25G_CNTR_TX_FCS_HI,
806                 hw, port_id);
807         hw_stats->eth.tx_errors += statistics;
808         hw_stats->checksum_error += statistics;
809
810         /*Number of transmitted frames with a frame of length at
811          *least 64 reporting a CRC error
812          */
813         statistics = ipn3ke_rpst_read_64bits_statistics_register(
814                 IPN3KE_25G_CNTR_TX_CRCERR_LO,
815                 IPN3KE_25G_CNTR_TX_CRCERR_HI,
816                 hw, port_id);
817         hw_stats->eth.tx_errors += statistics;
818         hw_stats->crc_errors += statistics;
819
820         /*Number of errored multicast frames transmitted,
821          *excluding control frames
822          */
823         statistics = ipn3ke_rpst_read_64bits_statistics_register(
824                 IPN3KE_25G_CNTR_TX_MCAST_DATA_ERR_LO,
825                 IPN3KE_25G_CNTR_TX_MCAST_DATA_ERR_HI,
826                 hw, port_id);
827         hw_stats->eth.tx_errors += statistics;
828
829         /*Number of errored broadcast frames transmitted,
830          *excluding control frames
831          */
832         statistics = ipn3ke_rpst_read_64bits_statistics_register(
833                 IPN3KE_25G_CNTR_TX_BCAST_DATA_ERR_LO,
834                 IPN3KE_25G_CNTR_TX_BCAST_DATA_ERR_HI,
835                 hw, port_id);
836         hw_stats->eth.tx_errors += statistics;
837
838         /*Number of errored unicast frames transmitted,
839          *excluding control frames
840          */
841         statistics = ipn3ke_rpst_read_64bits_statistics_register(
842                 IPN3KE_25G_CNTR_TX_UCAST_DATA_ERR_LO,
843                 IPN3KE_25G_CNTR_TX_UCAST_DATA_ERR_HI,
844                 hw, port_id);
845         hw_stats->eth.tx_errors += statistics;
846
847         /* Number of errored multicast control frames transmitted */
848         statistics = ipn3ke_rpst_read_64bits_statistics_register(
849                 IPN3KE_25G_CNTR_TX_MCAST_CTRL_ERR_LO,
850                 IPN3KE_25G_CNTR_TX_MCAST_CTRL_ERR_HI,
851                 hw, port_id);
852         hw_stats->eth.tx_errors += statistics;
853
854         /* Number of errored broadcast control frames transmitted */
855         statistics = ipn3ke_rpst_read_64bits_statistics_register(
856                 IPN3KE_25G_CNTR_TX_BCAST_CTRL_ERR_LO,
857                 IPN3KE_25G_CNTR_TX_BCAST_CTRL_ERR_HI,
858                 hw, port_id);
859         hw_stats->eth.tx_errors += statistics;
860
861         /* Number of errored unicast control frames transmitted */
862         statistics = ipn3ke_rpst_read_64bits_statistics_register(
863                 IPN3KE_25G_CNTR_TX_UCAST_CTRL_ERR_LO,
864                 IPN3KE_25G_CNTR_TX_UCAST_CTRL_ERR_HI,
865                 hw, port_id);
866         hw_stats->eth.tx_errors += statistics;
867
868         /* Number of errored pause frames transmitted */
869         statistics = ipn3ke_rpst_read_64bits_statistics_register(
870                 IPN3KE_25G_CNTR_TX_PAUSE_ERR_LO,
871                 IPN3KE_25G_CNTR_TX_PAUSE_ERR_HI,
872                 hw, port_id);
873         hw_stats->eth.tx_errors += statistics;
874
875         /*Number of 64-byte transmitted frames,
876          *including the CRC field but excluding the preamble
877          *and SFD bytes
878          */
879         statistics = ipn3ke_rpst_read_64bits_statistics_register(
880                 IPN3KE_25G_CNTR_TX_64B_LO,
881                 IPN3KE_25G_CNTR_TX_64B_HI,
882                 hw, port_id);
883         hw_stats->tx_size_64 += statistics;
884
885         /* Number of transmitted frames between 65 and 127 bytes */
886         statistics = ipn3ke_rpst_read_64bits_statistics_register(
887                 IPN3KE_25G_CNTR_TX_65_127B_LO,
888                 IPN3KE_25G_CNTR_TX_65_127B_HI,
889                 hw, port_id);
890         hw_stats->tx_size_65_127 += statistics;
891
892         /* Number of transmitted frames between 128 and 255 bytes */
893         statistics = ipn3ke_rpst_read_64bits_statistics_register(
894                 IPN3KE_25G_CNTR_TX_128_255B_LO,
895                 IPN3KE_25G_CNTR_TX_128_255B_HI,
896                 hw, port_id);
897         hw_stats->tx_size_128_255 += statistics;
898
899         /* Number of transmitted frames between 256 and 511 bytes */
900         statistics = ipn3ke_rpst_read_64bits_statistics_register(
901                 IPN3KE_25G_CNTR_TX_256_511B_LO,
902                 IPN3KE_25G_CNTR_TX_256_511B_HI,
903                 hw, port_id);
904         hw_stats->tx_size_256_511 += statistics;
905
906         /* Number of transmitted frames between 512 and 1023 bytes */
907         statistics = ipn3ke_rpst_read_64bits_statistics_register(
908                 IPN3KE_25G_CNTR_TX_512_1023B_LO,
909                 IPN3KE_25G_CNTR_TX_512_1023B_HI,
910                 hw, port_id);
911         hw_stats->tx_size_512_1023 += statistics;
912
913         /* Number of transmitted frames between 1024 and 1518 bytes */
914         statistics = ipn3ke_rpst_read_64bits_statistics_register(
915                 IPN3KE_25G_CNTR_TX_1024_1518B_LO,
916                 IPN3KE_25G_CNTR_TX_1024_1518B_HI,
917                 hw, port_id);
918         hw_stats->tx_size_1024_1518 += statistics;
919
920         /*Number of transmitted frames of size between 1519 bytes
921          *and the number of bytes specified in the MAX_TX_SIZE_CONFIG
922          *register
923          */
924         statistics = ipn3ke_rpst_read_64bits_statistics_register(
925                 IPN3KE_25G_CNTR_TX_1519_MAXB_LO,
926                 IPN3KE_25G_CNTR_TX_1519_MAXB_HI,
927                 hw, port_id);
928         hw_stats->tx_size_1519_to_max += statistics;
929
930         /*Number of oversized frames (frames with more bytes than the
931          *number specified in the MAX_TX_SIZE_CONFIG register)
932          *transmitted
933          */
934         statistics = ipn3ke_rpst_read_64bits_statistics_register(
935                 IPN3KE_25G_CNTR_TX_OVERSIZE_LO,
936                 IPN3KE_25G_CNTR_TX_OVERSIZE_HI,
937                 hw, port_id);
938
939         /*Number of valid multicast frames transmitted,
940          *excluding control frames
941          */
942         statistics = ipn3ke_rpst_read_64bits_statistics_register(
943                 IPN3KE_25G_CNTR_TX_MCAST_DATA_OK_LO,
944                 IPN3KE_25G_CNTR_TX_MCAST_DATA_OK_HI,
945                 hw, port_id);
946         hw_stats->eth.tx_multicast += statistics;
947
948         /*Number of valid broadcast frames transmitted,
949          *excluding control frames
950          */
951         statistics = ipn3ke_rpst_read_64bits_statistics_register(
952                 IPN3KE_25G_CNTR_TX_BCAST_DATA_OK_LO,
953                 IPN3KE_25G_CNTR_TX_BCAST_DATA_OK_HI,
954                 hw, port_id);
955         hw_stats->eth.tx_broadcast += statistics;
956
957         /*Number of valid unicast frames transmitted,
958          *excluding control frames
959          */
960         statistics = ipn3ke_rpst_read_64bits_statistics_register(
961                 IPN3KE_25G_CNTR_TX_UCAST_DATA_OK_LO,
962                 IPN3KE_25G_CNTR_TX_UCAST_DATA_OK_HI,
963                 hw, port_id);
964         hw_stats->eth.tx_unicast += statistics;
965
966         /*Number of valid multicast frames transmitted,
967          *excluding data frames
968          */
969         statistics = ipn3ke_rpst_read_64bits_statistics_register(
970                 IPN3KE_25G_CNTR_TX_MCAST_CTRL_LO,
971                 IPN3KE_25G_CNTR_TX_MCAST_CTRL_HI,
972                 hw, port_id);
973         hw_stats->eth.tx_multicast += statistics;
974
975         /*Number of valid broadcast frames transmitted,
976          *excluding data frames
977          */
978         statistics = ipn3ke_rpst_read_64bits_statistics_register(
979                 IPN3KE_25G_CNTR_TX_BCAST_CTRL_LO,
980                 IPN3KE_25G_CNTR_TX_BCAST_CTRL_HI,
981                 hw, port_id);
982         hw_stats->eth.tx_broadcast += statistics;
983
984         /*Number of valid unicast frames transmitted,
985          *excluding data frames
986          */
987         statistics = ipn3ke_rpst_read_64bits_statistics_register(
988                 IPN3KE_25G_CNTR_TX_UCAST_CTRL_LO,
989                 IPN3KE_25G_CNTR_TX_UCAST_CTRL_HI,
990                 hw, port_id);
991         hw_stats->eth.tx_unicast += statistics;
992
993         /* Number of valid pause frames transmitted */
994         statistics = ipn3ke_rpst_read_64bits_statistics_register(
995                 IPN3KE_25G_CNTR_TX_PAUSE_LO,
996                 IPN3KE_25G_CNTR_TX_PAUSE_HI,
997                 hw, port_id);
998
999         /*Number of transmitted runt packets. The IP core does not
1000          *transmit frames of length less than nine bytes.
1001          *The IP core pads frames of length nine bytes to 64 bytes to
1002          *extend them to 64 bytes. Therefore, this counter does not
1003          *increment in normal operating conditions.
1004          */
1005         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1006                 IPN3KE_25G_CNTR_TX_RUNT_LO,
1007                 IPN3KE_25G_CNTR_TX_RUNT_HI,
1008                 hw, port_id);
1009
1010         /*Number of transmitted payload bytes in frames with no FCS,
1011          *undersized, oversized, or payload length errors.
1012          *If VLAN detection is turned off for the TX MAC (bit[1]
1013          *of the TX_MAC_CONTROL register at offset 0x40A has
1014          *the value of 1), the IP core counts the VLAN header bytes
1015          *(4 bytes for VLAN and 8 bytes for stacked VLAN)
1016          *as payload bytes. This register is compliant with
1017          *the requirements for aOctetsTransmittedOK in section
1018          *5.2.2.1.8 of the IEEE Standard 802.3-2008.
1019          */
1020         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1021                 IPN3KE_25G_TX_PAYLOAD_OCTETS_OK_LO,
1022                 IPN3KE_25G_TX_PAYLOAD_OCTETS_OK_HI,
1023                 hw, port_id);
1024         hw_stats->eth.tx_bytes += statistics;
1025
1026         /*Number of transmitted bytes in frames with no FCS, undersized,
1027          *oversized, or payload length errors. This register is
1028          *compliant with the requirements for ifOutOctets in RFC3635
1029          *(Managed Objects for Ethernet-like Interface Types)
1030          *and TX etherStatsOctets in RFC2819(Remote Network Monitoring
1031          *Management Information Base (RMON)).
1032          */
1033         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1034                 IPN3KE_25G_TX_FRAME_OCTETS_OK_LO,
1035                 IPN3KE_25G_TX_FRAME_OCTETS_OK_HI,
1036                 hw, port_id);
1037
1038         /*Number of received frames less than 64 bytes
1039          *and reporting a CRC error
1040          */
1041         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1042                 IPN3KE_25G_CNTR_RX_FRAGMENTS_LO,
1043                 IPN3KE_25G_CNTR_RX_FRAGMENTS_HI,
1044                 hw, port_id);
1045         hw_stats->eth.rx_discards += statistics;
1046         hw_stats->crc_errors += statistics;
1047         hw_stats->rx_length_errors += statistics;
1048
1049         /* Number of received oversized frames reporting a CRC error */
1050         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1051                 IPN3KE_25G_CNTR_RX_JABBERS_LO,
1052                 IPN3KE_25G_CNTR_RX_JABBERS_HI,
1053                 hw, port_id);
1054         hw_stats->eth.rx_discards += statistics;
1055         hw_stats->crc_errors += statistics;
1056         hw_stats->rx_length_errors += statistics;
1057
1058         /*Number of received packets with FCS errors.
1059          *This register maintains a count of the number of pulses
1060          *on the "l<n>_rx_fcs_error" or "rx_fcs_error" output signal
1061          */
1062         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1063                 IPN3KE_25G_CNTR_RX_FCS_LO,
1064                 IPN3KE_25G_CNTR_RX_FCS_HI,
1065                 hw, port_id);
1066         hw_stats->eth.rx_discards += statistics;
1067         hw_stats->checksum_error += statistics;
1068
1069         /*Number of received frames with a frame of length at least 64
1070          *with CRC error
1071          */
1072         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1073                 IPN3KE_25G_CNTR_RX_CRCERR_LO,
1074                 IPN3KE_25G_CNTR_RX_CRCERR_HI,
1075                 hw, port_id);
1076         hw_stats->eth.rx_discards += statistics;
1077         hw_stats->crc_errors += statistics;
1078
1079         /*Number of errored multicast frames received,
1080          *excluding control frames
1081          */
1082         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1083                 IPN3KE_25G_CNTR_RX_MCAST_DATA_ERR_LO,
1084                 IPN3KE_25G_CNTR_RX_MCAST_DATA_ERR_HI,
1085                 hw, port_id);
1086         hw_stats->eth.rx_discards += statistics;
1087
1088         /*Number of errored broadcast frames received,
1089          *excluding control frames
1090          */
1091         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1092                 IPN3KE_25G_CNTR_RX_BCAST_DATA_ERR_LO,
1093                 IPN3KE_25G_CNTR_RX_BCAST_DATA_ERR_HI,
1094                 hw, port_id);
1095         hw_stats->eth.rx_discards += statistics;
1096
1097         /*Number of errored unicast frames received,
1098          *excluding control frames
1099          */
1100         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1101                 IPN3KE_25G_CNTR_RX_UCAST_DATA_ERR_LO,
1102                 IPN3KE_25G_CNTR_RX_UCAST_DATA_ERR_HI,
1103                 hw, port_id);
1104         hw_stats->eth.rx_discards += statistics;
1105
1106         /* Number of errored multicast control frames received */
1107         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1108                 IPN3KE_25G_CNTR_RX_MCAST_CTRL_ERR_LO,
1109                 IPN3KE_25G_CNTR_RX_MCAST_CTRL_ERR_HI,
1110                 hw, port_id);
1111         hw_stats->eth.rx_discards += statistics;
1112
1113         /* Number of errored broadcast control frames received */
1114         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1115                 IPN3KE_25G_CNTR_RX_BCAST_CTRL_ERR_LO,
1116                 IPN3KE_25G_CNTR_RX_BCAST_CTRL_ERR_HI,
1117                 hw, port_id);
1118         hw_stats->eth.rx_discards += statistics;
1119
1120         /* Number of errored unicast control frames received */
1121         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1122                 IPN3KE_25G_CNTR_RX_UCAST_CTRL_ERR_LO,
1123                 IPN3KE_25G_CNTR_RX_UCAST_CTRL_ERR_HI,
1124                 hw, port_id);
1125         hw_stats->eth.rx_discards += statistics;
1126
1127         /* Number of errored pause frames received */
1128         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1129                 IPN3KE_25G_CNTR_RX_PAUSE_ERR_LO,
1130                 IPN3KE_25G_CNTR_RX_PAUSE_ERR_HI,
1131                 hw, port_id);
1132         hw_stats->eth.rx_discards += statistics;
1133
1134         /*Number of 64-byte received frames,
1135          *including the CRC field but excluding the preamble
1136          *and SFD bytes
1137          */
1138         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1139                 IPN3KE_25G_CNTR_RX_64B_LO,
1140                 IPN3KE_25G_CNTR_RX_64B_HI,
1141                 hw, port_id);
1142         hw_stats->rx_size_64 += statistics;
1143
1144         /*Number of received frames between 65 and 127 bytes */
1145         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1146                 IPN3KE_25G_CNTR_RX_65_127B_LO,
1147                 IPN3KE_25G_CNTR_RX_65_127B_HI,
1148                 hw, port_id);
1149         hw_stats->rx_size_65_127 += statistics;
1150
1151         /*Number of received frames between 128 and 255 bytes
1152          */
1153         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1154                 IPN3KE_25G_CNTR_RX_128_255B_LO,
1155                 IPN3KE_25G_CNTR_RX_128_255B_HI,
1156                 hw, port_id);
1157         hw_stats->rx_size_128_255 += statistics;
1158
1159         /*Number of received frames between 256 and 511 bytes
1160          */
1161         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1162                 IPN3KE_25G_CNTR_RX_256_511B_LO,
1163                 IPN3KE_25G_CNTR_RX_256_511B_HI,
1164                 hw, port_id);
1165         hw_stats->rx_size_256_511 += statistics;
1166
1167         /*Number of received frames between 512 and 1023 bytes
1168          */
1169         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1170                 IPN3KE_25G_CNTR_RX_512_1023B_LO,
1171                 IPN3KE_25G_CNTR_RX_512_1023B_HI,
1172                 hw, port_id);
1173         hw_stats->rx_size_512_1023 += statistics;
1174
1175         /*Number of received frames between 1024 and 1518 bytes
1176          */
1177         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1178                 IPN3KE_25G_CNTR_RX_1024_1518B_LO,
1179                 IPN3KE_25G_CNTR_RX_1024_1518B_HI,
1180                 hw, port_id);
1181         hw_stats->rx_size_1024_1518 += statistics;
1182
1183         /*Number of received frames of size between 1519 bytes
1184          *and the number of bytes specified in the MAX_TX_SIZE_CONFIG
1185          *register
1186          */
1187         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1188                 IPN3KE_25G_CNTR_RX_1519_MAXB_LO,
1189                 IPN3KE_25G_CNTR_RX_1519_MAXB_HI,
1190                 hw, port_id);
1191         hw_stats->rx_size_big += statistics;
1192
1193         /*Number of oversized frames (frames with more bytes
1194          *than the number specified in the MAX_TX_SIZE_CONFIG register)
1195          *received
1196          */
1197         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1198                 IPN3KE_25G_CNTR_RX_OVERSIZE_LO,
1199                 IPN3KE_25G_CNTR_RX_OVERSIZE_HI,
1200                 hw, port_id);
1201         hw_stats->rx_jabber += statistics;
1202
1203         /*Number of valid multicast frames received,
1204          *excluding control frames
1205          */
1206         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1207                 IPN3KE_25G_CNTR_RX_MCAST_DATA_OK_LO,
1208                 IPN3KE_25G_CNTR_RX_MCAST_DATA_OK_HI,
1209                 hw, port_id);
1210         hw_stats->eth.rx_multicast += statistics;
1211
1212         /*Number of valid broadcast frames received,
1213          *excluding control frames
1214          */
1215         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1216                 IPN3KE_25G_CNTR_RX_BCAST_DATA_OK_LO,
1217                 IPN3KE_25G_CNTR_RX_BCAST_DATA_OK_HI,
1218                 hw, port_id);
1219         hw_stats->eth.rx_broadcast += statistics;
1220
1221         /*Number of valid unicast frames received,
1222          *excluding control frames
1223          */
1224         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1225                 IPN3KE_25G_CNTR_RX_UCAST_DATA_OK_LO,
1226                 IPN3KE_25G_CNTR_RX_UCAST_DATA_OK_HI,
1227                 hw, port_id);
1228         hw_stats->eth.rx_unicast += statistics;
1229
1230         /*Number of valid multicast frames received,
1231          *excluding data frames
1232          */
1233         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1234                 IPN3KE_25G_CNTR_RX_MCAST_CTRL_LO,
1235                 IPN3KE_25G_CNTR_RX_MCAST_CTRL_HI,
1236                 hw, port_id);
1237         hw_stats->eth.rx_multicast += statistics;
1238
1239         /*Number of valid broadcast frames received,
1240          *excluding data frames
1241          */
1242         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1243                 IPN3KE_25G_CNTR_RX_BCAST_CTRL_LO,
1244                 IPN3KE_25G_CNTR_RX_BCAST_CTRL_HI,
1245                 hw, port_id);
1246         hw_stats->eth.rx_broadcast += statistics;
1247
1248         /*Number of valid unicast frames received,
1249          *excluding data frames
1250          */
1251         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1252                 IPN3KE_25G_CNTR_RX_UCAST_CTRL_LO,
1253                 IPN3KE_25G_CNTR_RX_UCAST_CTRL_HI,
1254                 hw, port_id);
1255         hw_stats->eth.rx_unicast += statistics;
1256
1257         /*Number of received pause frames, with or without error
1258          */
1259         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1260                 IPN3KE_25G_CNTR_RX_PAUSE_LO,
1261                 IPN3KE_25G_CNTR_RX_PAUSE_HI,
1262                 hw, port_id);
1263
1264         /*Number of received runt packets. A runt is a packet of size
1265          *less than 64 bytes but greater than eight bytes.
1266          *If a packet is eight bytes or smaller, it is considered
1267          *a decoding error and not a runt frame, and the IP core
1268          *does not flag it nor count it as a runt.
1269          */
1270         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1271                 IPN3KE_25G_CNTR_RX_RUNT_LO,
1272                 IPN3KE_25G_CNTR_RX_RUNT_HI,
1273                 hw, port_id);
1274
1275         /*Number of received payload bytes in frames with no FCS,
1276          *undersized, oversized, or payload length errors.
1277          *If VLAN detection is turned off for the RX MAC (bit [1] of the
1278          *"RXMAC_CONTROL" register at offset 0x50A has the value of 1),
1279          *the IP core counts the VLAN header bytes (4 bytes for VLAN and
1280          *8 bytes for stacked VLAN) as payload bytes.
1281          *This register is compliant with the requirements for
1282          *aOctetsReceivedOK in section 5.2.2.1.14 of the IEEE Standard
1283          *802.3-2008
1284          */
1285         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1286                 IPN3KE_25G_RX_PAYLOAD_OCTETS_OK_LO,
1287                 IPN3KE_25G_RX_PAYLOAD_OCTETS_OK_HI,
1288                 hw, port_id);
1289         hw_stats->eth.rx_bytes += statistics;
1290
1291         /*Number of received bytes in frames with no FCS, undersized,
1292          *oversized, or payload length errors.
1293          *This register is compliant with the requirements for
1294          *ifInOctets in RFC3635 (Managed Objects for Ethernet-like
1295          *Interface Types) and RX etherStatsOctets in RFC2819
1296          *(Remote Network Monitoring Management Information Base
1297          *(RMON)).
1298          */
1299         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1300                 IPN3KE_25G_RX_FRAME_OCTETS_OK_LO,
1301                 IPN3KE_25G_RX_FRAME_OCTETS_OK_HI,
1302                 hw, port_id);
1303
1304         /*resume Tx counter to real time
1305          */
1306         tmp = 0x00000000;
1307         (*hw->f_mac_read)(hw,
1308                         &tmp,
1309                         IPN3KE_25G_TX_STATISTICS_CONFIG,
1310                         port_id,
1311                         0);
1312         tmp &= 0xfffffffb;
1313         (*hw->f_mac_write)(hw,
1314                         tmp,
1315                         IPN3KE_25G_TX_STATISTICS_CONFIG,
1316                         port_id,
1317                         0);
1318
1319         /*resume Rx counter to real time
1320          */
1321         tmp = 0x00000000;
1322         (*hw->f_mac_read)(hw,
1323                         &tmp,
1324                         IPN3KE_25G_RX_STATISTICS_CONFIG,
1325                         port_id,
1326                         0);
1327         tmp &= 0xfffffffb;
1328         (*hw->f_mac_write)(hw,
1329                         tmp,
1330                         IPN3KE_25G_RX_STATISTICS_CONFIG,
1331                         port_id,
1332                         0);
1333
1334         return 0;
1335 }
1336
1337 static void
1338 ipn3ke_rpst_25g_lineside_tx_stats_reset(struct ipn3ke_hw *hw,
1339 uint16_t port_id)
1340 {
1341         uint32_t tmp = 0x00000001;
1342         /* Bit[0]: Software can set this bit to the value of 1
1343          * to reset all of the TX statistics registers at the same time.
1344          * This bit is selfclearing.
1345          */
1346         (*hw->f_mac_write)(hw,
1347                         tmp,
1348                         IPN3KE_25G_TX_STATISTICS_CONFIG,
1349                         port_id,
1350                         0);
1351
1352         while (tmp & 0x00000001) {
1353                 tmp = 0x00000000;
1354                 (*hw->f_mac_read)(hw,
1355                                 &tmp,
1356                                 IPN3KE_25G_TX_STATISTICS_CONFIG,
1357                                 port_id,
1358                                 0);
1359                 if (tmp & 0x00000001)
1360                         usleep(5);
1361                 else
1362                         return;
1363         }
1364 }
1365
1366 static void
1367 ipn3ke_rpst_25g_lineside_rx_stats_reset(struct ipn3ke_hw *hw,
1368 uint16_t port_id)
1369 {
1370         uint32_t tmp = 0x00000001;
1371         /* Bit[0]: Software can set this bit to the value of 1
1372          * to reset all of the RX statistics registers at the same time.
1373          * This bit is selfclearing.
1374          */
1375         (*hw->f_mac_write)(hw,
1376                         tmp,
1377                         IPN3KE_25G_RX_STATISTICS_CONFIG,
1378                         port_id,
1379                         0);
1380
1381         while (tmp & 0x00000001) {
1382                 tmp = 0x00000000;
1383                 (*hw->f_mac_read)(hw,
1384                                 &tmp,
1385                                 IPN3KE_25G_RX_STATISTICS_CONFIG,
1386                                 port_id,
1387                                 0);
1388                 if (tmp & 0x00000001)
1389                         usleep(5);
1390                 else
1391                         return;
1392         }
1393 }
1394
1395 static uint64_t
1396 ipn3ke_rpst_read_36bits_statistics_register(uint32_t addr_lo,
1397 uint32_t addr_hi, struct ipn3ke_hw *hw, uint16_t port_id)
1398 {
1399         uint32_t statistics_lo = 0x00000000;
1400         uint32_t statistics_hi = 0x00000000;
1401         uint64_t statistics = 0x0000000000000000;
1402
1403         (*hw->f_mac_read)(hw,
1404                         &statistics_lo,
1405                         addr_lo,
1406                         port_id,
1407                         0);
1408         (*hw->f_mac_read)(hw,
1409                         &statistics_hi,
1410                         addr_hi,
1411                         port_id,
1412                         0);
1413         statistics_hi &= IPN3KE_10G_STATS_HI_VALID_MASK;
1414         statistics += statistics_hi;
1415         statistics = statistics << IPN3KE_REGISTER_WIDTH;
1416         statistics += statistics_lo;
1417         return statistics;
1418 }
1419
1420 static int
1421 ipn3ke_rpst_read_10g_lineside_stats_registers
1422 (struct ipn3ke_hw *hw,
1423 uint16_t port_id,
1424 struct ipn3ke_rpst_hw_port_stats *hw_stats,
1425 struct rte_eth_stats *stats)
1426 {
1427         uint64_t statistics = 0;
1428
1429         memset(hw_stats, 0, sizeof(*hw_stats));
1430         memset(stats, 0, sizeof(*stats));
1431
1432         /*36-bit statistics counter that collects the number of frames
1433          *that are successfully transmitted, including control frames.
1434          */
1435         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1436                 IPN3KE_10G_TX_STATS_FRAME_OK_LO,
1437                 IPN3KE_10G_TX_STATS_FRAME_OK_HI,
1438                 hw, port_id);
1439         stats->opackets = statistics;
1440
1441         /*36-bit statistics counter that collects the number of frames
1442          *that are successfully received, including control frames.
1443          */
1444         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1445                 IPN3KE_10G_RX_STATS_FRAME_OK_LO,
1446                 IPN3KE_10G_RX_STATS_FRAME_OK_HI,
1447                 hw, port_id);
1448         stats->ipackets = statistics;
1449
1450         /*36-bit statistics counter that collects the number of frames
1451          *transmitted with error, including control frames.
1452          */
1453         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1454                 IPN3KE_10G_TX_STATS_FRAME_ERR_LO,
1455                 IPN3KE_10G_TX_STATS_FRAME_ERR_HI,
1456                 hw, port_id);
1457         stats->oerrors = statistics;
1458         hw_stats->eth.tx_errors = statistics;
1459
1460         /*36-bit statistics counter that collects the number of frames
1461          *received with error, including control frames.
1462          */
1463         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1464                 IPN3KE_10G_RX_STATS_FRAME_ERR_LO,
1465                 IPN3KE_10G_RX_STATS_FRAME_ERR_HI,
1466                 hw, port_id);
1467         stats->ierrors = statistics;
1468         hw_stats->eth.rx_discards = statistics;
1469
1470         /*36-bit statistics counter that collects the number
1471          *of RX frames with CRC error.
1472          */
1473         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1474                 IPN3KE_10G_RX_STATS_FRAME_CRC_ERR_LO,
1475                 IPN3KE_10G_RX_STATS_FRAME_CRC_ERR_HI,
1476                 hw, port_id);
1477         hw_stats->crc_errors = statistics;
1478
1479         /*64-bit statistics counter that collects the payload length,
1480          *including the bytes in control frames.
1481          *The payload length is the number of data and padding bytes
1482          *transmitted.
1483          *If the tx_vlan_detection[0] register bit is set to 1,
1484          *the VLAN and stacked VLAN tags are counted as part of
1485          *the TX payload.
1486          */
1487         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1488                 IPN3KE_10G_TX_STATS_OCTETS_OK_LO,
1489                 IPN3KE_10G_TX_STATS_OCTETS_OK_HI,
1490                 hw, port_id);
1491         stats->obytes = statistics;
1492         hw_stats->eth.tx_bytes = statistics;
1493
1494         /*64-bit statistics counter that collects the payload length,
1495          *including the bytes in control frames.
1496          *The payload length is the number of data and padding bytes
1497          *received.
1498          *If the rx_vlan_detection[0] register bit is set to 1,
1499          *the VLAN and stacked VLAN tags are counted as part of
1500          *the RX payload.
1501          */
1502         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1503                 IPN3KE_10G_RX_STATS_OCTETS_OK_LO,
1504                 IPN3KE_10G_RX_STATS_OCTETS_OK_HI,
1505                 hw, port_id);
1506         stats->ibytes = statistics;
1507         hw_stats->eth.rx_bytes = statistics;
1508
1509         /*36-bit statistics counter that collects the number of
1510          *valid pause frames transmitted.
1511          */
1512         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1513                 IPN3KE_10G_TX_STATS_PAUSE_MAC_CTRL_FRAMES_LO,
1514                 IPN3KE_10G_TX_STATS_PAUSE_MAC_CTRL_FRAMES_HI,
1515                 hw, port_id);
1516
1517         /*36-bit statistics counter that collects the number of
1518          *valid pause frames received.
1519          */
1520         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1521                 IPN3KE_10G_RX_STATS_PAUSE_MAC_CTRL_FRAMES_LO,
1522                 IPN3KE_10G_RX_STATS_PAUSE_MAC_CTRL_FRAMES_HI,
1523                 hw, port_id);
1524
1525         /*36-bit statistics counter that collects the number of frames
1526          *transmitted that are invalid and with error.
1527          */
1528         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1529                 IPN3KE_10G_TX_STATS_IF_ERRORS_LO,
1530                 IPN3KE_10G_TX_STATS_IF_ERRORS_HI,
1531                 hw, port_id);
1532
1533         /*36-bit statistics counter that collects the number of frames
1534          *received that are invalid and with error.
1535          */
1536         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1537                 IPN3KE_10G_RX_STATS_IF_ERRORS_LO,
1538                 IPN3KE_10G_RX_STATS_IF_ERRORS_HI,
1539                 hw, port_id);
1540
1541         /*36-bit statistics counter that collects the number of
1542          *good unicast frames transmitted,
1543          *excluding control frames.
1544          */
1545         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1546                 IPN3KE_10G_TX_STATS_UNICAST_FRAME_OK_LO,
1547                 IPN3KE_10G_TX_STATS_UNICAST_FRAME_OK_HI,
1548                 hw, port_id);
1549         hw_stats->eth.tx_unicast = statistics;
1550
1551         /*36-bit statistics counter that collects the number of
1552          *good unicast frames received,
1553          *excluding control frames.
1554          */
1555         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1556                 IPN3KE_10G_RX_STATS_UNICAST_FRAME_OK_LO,
1557                 IPN3KE_10G_RX_STATS_UNICAST_FRAME_OK_HI,
1558                 hw, port_id);
1559         hw_stats->eth.rx_unicast = statistics;
1560
1561         /*36-bit statistics counter that collects the number of
1562          *unicast frames transmitted with error,
1563          *excluding control frames.
1564          */
1565         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1566                 IPN3KE_10G_TX_STATS_UNICAST_FRAME_ERR_LO,
1567                 IPN3KE_10G_TX_STATS_UNICAST_FRAME_ERR_HI,
1568                 hw, port_id);
1569
1570         /*36-bit statistics counter that collects the number of
1571          *unicast frames received with error,
1572          *excluding control frames.
1573          */
1574         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1575                 IPN3KE_10G_RX_STATS_UNICAST_FRAME_ERR_LO,
1576                 IPN3KE_10G_RX_STATS_UNICAST_FRAME_ERR_HI,
1577                 hw, port_id);
1578
1579         /*36-bit statistics counter that collects the number of
1580          *good multicast frames transmitted,
1581          *excluding control frames.
1582          */
1583         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1584                 IPN3KE_10G_TX_STATS_MULTICAST_FRAME_OK_LO,
1585                 IPN3KE_10G_TX_STATS_MULTICAST_FRAME_OK_HI,
1586                 hw, port_id);
1587         hw_stats->eth.tx_multicast = statistics;
1588
1589         /*36-bit statistics counter that collects the number of
1590          *good multicast frames received,
1591          *excluding control frames.
1592          */
1593         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1594                 IPN3KE_10G_RX_STATS_MULTICAST_FRAME_OK_LO,
1595                 IPN3KE_10G_RX_STATS_MULTICAST_FRAME_OK_HI,
1596                 hw, port_id);
1597         hw_stats->eth.rx_multicast = statistics;
1598
1599         /*36-bit statistics counter that collects the number of
1600          *multicast frames transmitted with error,
1601          *excluding control frames.
1602          */
1603         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1604                 IPN3KE_10G_TX_STATS_MULTICAST_FRAME_ERR_LO,
1605                 IPN3KE_10G_TX_STATS_MULTICAST_FRAME_ERR_HI,
1606                 hw, port_id);
1607
1608         /*36-bit statistics counter that collects the number
1609          *of multicast frames received with error,
1610          *excluding control frames.
1611          */
1612         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1613                 IPN3KE_10G_RX_STATS_MULTICAST_FRAME_ERR_LO,
1614                 IPN3KE_10G_RX_STATS_MULTICAST_FRAME_ERR_HI,
1615                 hw, port_id);
1616
1617         /*36-bit statistics counter that collects the number of
1618          *good broadcast frames transmitted,
1619          *excluding control frames.
1620          */
1621         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1622                 IPN3KE_10G_TX_STATS_BROADCAST_FRAME_OK_LO,
1623                 IPN3KE_10G_TX_STATS_BROADCAST_FRAME_OK_HI,
1624                 hw, port_id);
1625         hw_stats->eth.tx_broadcast = statistics;
1626
1627         /*36-bit statistics counter that collects the number of
1628          *good broadcast frames received,
1629          *excluding control frames.
1630          */
1631         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1632                 IPN3KE_10G_RX_STATS_BROADCAST_FRAME_OK_LO,
1633                 IPN3KE_10G_RX_STATS_BROADCAST_FRAME_OK_HI,
1634                 hw, port_id);
1635         hw_stats->eth.rx_broadcast = statistics;
1636
1637         /*36-bit statistics counter that collects the number
1638          *of broadcast frames transmitted with error,
1639          *excluding control frames.
1640          */
1641         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1642                 IPN3KE_10G_TX_STATS_BROADCAST_FRAME_ERR_LO,
1643                 IPN3KE_10G_TX_STATS_BROADCAST_FRAME_ERR_HI,
1644                 hw, port_id);
1645
1646         /*36-bit statistics counter that collects the number of
1647          *broadcast frames received with error,
1648          *excluding control frames.
1649          */
1650         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1651                 IPN3KE_10G_RX_STATS_BROADCAST_FRAME_ERR_LO,
1652                 IPN3KE_10G_RX_STATS_BROADCAST_FRAME_ERR_HI,
1653                 hw, port_id);
1654
1655         /*64-bit statistics counter that collects the total number of
1656          *octets transmitted.
1657          *This count includes good, errored, and invalid frames.
1658          */
1659         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1660                 IPN3KE_10G_TX_STATS_ETHER_STATS_OCTETS_LO,
1661                 IPN3KE_10G_TX_STATS_ETHER_STATS_OCTETS_HI,
1662                 hw, port_id);
1663
1664         /*64-bit statistics counter that collects the total number of
1665          *octets received.
1666          *This count includes good, errored, and invalid frames.
1667          */
1668         statistics = ipn3ke_rpst_read_64bits_statistics_register(
1669                 IPN3KE_10G_RX_STATS_ETHER_STATS_OCTETS_LO,
1670                 IPN3KE_10G_RX_STATS_ETHER_STATS_OCTETS_HI,
1671                 hw, port_id);
1672
1673         /*36-bit statistics counter that collects the total number of
1674          *good, errored, and invalid frames transmitted.
1675          */
1676         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1677                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_LO,
1678                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_HI,
1679                 hw, port_id);
1680
1681         /*36-bit statistics counter that collects the total number of
1682          *good, errored, and invalid frames received.
1683          */
1684         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1685                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_LO,
1686                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_HI,
1687                 hw, port_id);
1688
1689         /*36-bit statistics counter that collects the number of
1690          *undersized TX frames.
1691          */
1692         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1693                 IPN3KE_10G_TX_STATS_ETHER_STATS_UNDER_SIZE_PKTS_LO,
1694                 IPN3KE_10G_TX_STATS_ETHER_STATS_UNDER_SIZE_PKTS_HI,
1695                 hw, port_id);
1696
1697         /*36-bit statistics counter that collects the number of
1698          *undersized RX frames.
1699          */
1700         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1701                 IPN3KE_10G_RX_STATS_ETHER_STATS_UNDER_SIZE_PKTS_LO,
1702                 IPN3KE_10G_RX_STATS_ETHER_STATS_UNDER_SIZE_PKTS_HI,
1703                 hw, port_id);
1704         hw_stats->rx_undersize = statistics;
1705
1706         /*36-bit statistics counter that collects the number of
1707          *TX frames whose length exceeds the maximum frame length
1708          *specified.
1709          */
1710         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1711                 IPN3KE_10G_TX_STATS_ETHER_STATS_OVER_SIZE_PKTS_LO,
1712                 IPN3KE_10G_TX_STATS_ETHER_STATS_OVER_SIZE_PKTS_HI,
1713                 hw, port_id);
1714
1715         /*36-bit statistics counter that collects the number of
1716          *RX frames whose length exceeds the maximum frame length
1717          *specified.
1718          */
1719         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1720                 IPN3KE_10G_RX_STATS_ETHER_STATS_OVER_SIZE_PKTS_LO,
1721                 IPN3KE_10G_RX_STATS_ETHER_STATS_OVER_SIZE_PKTS_HI,
1722                 hw, port_id);
1723         hw_stats->rx_oversize = statistics;
1724
1725         /*36-bit statistics counter that collects the number of
1726          *64-byte TX frames,
1727          *including the CRC field
1728          *but excluding the preamble and SFD bytes.
1729          *This count includes good, errored, and invalid frames.
1730          */
1731         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1732                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_64_OCTETS_LO,
1733                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_64_OCTETS_HI,
1734                 hw, port_id);
1735         hw_stats->tx_size_64 = statistics;
1736
1737         /*36-bit statistics counter that collects the number of
1738          *64-byte RX frames,
1739          *including the CRC field
1740          *but excluding the preamble and SFD bytes.
1741          *This count includes good, errored, and invalid frames.
1742          */
1743         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1744                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_64_OCTETS_LO,
1745                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_64_OCTETS_HI,
1746                 hw, port_id);
1747         hw_stats->rx_size_64 = statistics;
1748
1749         /*36-bit statistics counter that collects the number of
1750          *TX frames between the length of 65 and 127 bytes,
1751          *including the CRC field
1752          *but excluding the preamble and SFD bytes.
1753          *This count includes good, errored, and invalid frames.
1754          */
1755         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1756                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_65_127_OCTETS_LO,
1757                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_65_127_OCTETS_HI,
1758                 hw, port_id);
1759         hw_stats->tx_size_65_127 = statistics;
1760
1761         /*36-bit statistics counter that collects the number of
1762          *RX frames between the length of 65 and 127 bytes,
1763          *including the CRC field
1764          *but excluding the preamble and SFD bytes.
1765          *This count includes good, errored, and invalid frames.
1766          */
1767         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1768                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_65_127_OCTETS_LO,
1769                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_65_127_OCTETS_HI,
1770                 hw, port_id);
1771         hw_stats->rx_size_65_127 = statistics;
1772
1773         /*36-bit statistics counter that collects the number of
1774          *TX frames between the length of 128 and 255 bytes,
1775          *including the CRC field
1776          *but excluding the preamble and SFD bytes.
1777          *This count includes good, errored, and invalid frames.
1778          */
1779         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1780                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_128_255_OCTETS_LO,
1781                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_128_255_OCTETS_HI,
1782                 hw, port_id);
1783         hw_stats->tx_size_128_255 = statistics;
1784
1785         /*36-bit statistics counter that collects the number of
1786          *RX frames between the length of 128 and 255 bytes,
1787          *including the CRC field
1788          *but excluding the preamble and SFD bytes.
1789          *This count includes good, errored, and invalid frames.
1790          */
1791         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1792                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_128_255_OCTETS_LO,
1793                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_128_255_OCTETS_HI,
1794                 hw, port_id);
1795         hw_stats->rx_size_128_255 = statistics;
1796
1797         /*36-bit statistics counter that collects the number of
1798          *TX frames between the length of 256 and 511 bytes,
1799          *including the CRC field
1800          *but excluding the preamble and SFD bytes.
1801          *This count includes good, errored, and invalid frames.
1802          */
1803         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1804                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_256_511_OCTETS_LO,
1805                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_256_511_OCTETS_HI,
1806                 hw, port_id);
1807         hw_stats->tx_size_256_511 = statistics;
1808
1809         /*36-bit statistics counter that collects the number of
1810          *RX frames between the length of 256 and 511 bytes,
1811          *including the CRC field
1812          *but excluding the preamble and SFD bytes.
1813          *This count includes good, errored, and invalid frames.
1814          */
1815         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1816                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_256_511_OCTETS_LO,
1817                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_256_511_OCTETS_HI,
1818                 hw, port_id);
1819         hw_stats->rx_size_256_511 = statistics;
1820
1821         /*36-bit statistics counter that collects the number of
1822          *TX frames between the length of 512 and 1023 bytes,
1823          *including the CRC field
1824          *but excluding the preamble and SFD bytes.
1825          *This count includes good, errored, and invalid frames.
1826          */
1827         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1828                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_512_1023_OCTETS_LO,
1829                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_512_1023_OCTETS_HI,
1830                 hw, port_id);
1831         hw_stats->tx_size_512_1023 = statistics;
1832
1833         /*36-bit statistics counter that collects the number of
1834          *RX frames between the length of 512 and 1023 bytes,
1835          *including the CRC field
1836          *but excluding the preamble and SFD bytes.
1837          *This count includes good, errored, and invalid frames.
1838          */
1839         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1840                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_512_1023_OCTETS_LO,
1841                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_512_1023_OCTETS_HI,
1842                 hw, port_id);
1843         hw_stats->rx_size_512_1023 = statistics;
1844
1845         /*36-bit statistics counter that collects the number of
1846          *TX frames between the length of 1024 and 1518 bytes,
1847          *including the CRC field but
1848          *excluding the preamble and SFD bytes.
1849          *This count includes good, errored, and invalid frames.
1850          */
1851         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1852                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_1024_1518_OCTETS_LO,
1853                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_1024_1518_OCTETS_HI,
1854                 hw, port_id);
1855         hw_stats->tx_size_1024_1518 = statistics;
1856
1857         /*36-bit statistics counter that collects the number of
1858          *RX frames between the length of 1024 and 1518 bytes,
1859          *including the CRC field
1860          *but excluding the preamble and SFD bytes.
1861          *This count includes good, errored, and invalid frames.
1862          */
1863         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1864                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_1024_1518_OCTETS_LO,
1865                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_1024_1518_OCTETS_HI,
1866                 hw, port_id);
1867         hw_stats->rx_size_1024_1518 = statistics;
1868
1869         /*36-bit statistics counter that collects the number of
1870          *TX frames equal or more than the length of 1,519 bytes,
1871          *including the CRC field
1872          *but excluding the preamble and SFD bytes.
1873          *This count includes good, errored, and invalid frames.
1874          */
1875         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1876                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_1519_X_OCTETS_LO,
1877                 IPN3KE_10G_TX_STATS_ETHER_STATS_PKTS_1519_X_OCTETS_HI,
1878                 hw, port_id);
1879         hw_stats->tx_size_1519_to_max = statistics;
1880
1881         /*36-bit statistics counter that collects the number of
1882          *RX frames equal or more than the length of 1,519 bytes,
1883          *including the CRC field
1884          *but excluding the preamble and SFD bytes.
1885          *This count includes good,
1886          *errored, and invalid frames.
1887          */
1888         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1889                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_1519_X_OCTETS_LO,
1890                 IPN3KE_10G_RX_STATS_ETHER_STATS_PKTS_1519_X_OCTETS_HI,
1891                 hw, port_id);
1892         hw_stats->rx_size_big = statistics;
1893
1894         /*36-bit statistics counter that collects the total number of
1895          *RX frames with length less than 64 bytes and CRC error.
1896          *The MAC does not drop these frames.
1897          */
1898         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1899                 IPN3KE_10G_RX_STATS_ETHER_STATS_FRAGMENTS_LO,
1900                 IPN3KE_10G_RX_STATS_ETHER_STATS_FRAGMENTS_HI,
1901                 hw, port_id);
1902
1903         /*36-bit statistics counter that collects the number of
1904          *oversized RX frames with CRC error.
1905          *The MAC does not drop these frames.
1906          */
1907         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1908                 IPN3KE_10G_RX_STATS_ETHER_STATS_JABBERS_LO,
1909                 IPN3KE_10G_RX_STATS_ETHER_STATS_JABBERS_HI,
1910                 hw, port_id);
1911
1912         /*36-bit statistics counter that collects the number of
1913          *RX frames with CRC error,
1914          *whose length is between 64 and the maximum frame length
1915          *specified in the register.
1916          *The MAC does not drop these frames.
1917          */
1918         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1919                 IPN3KE_10G_RX_STATS_ETHER_STATS_CRC_ERR_LO,
1920                 IPN3KE_10G_RX_STATS_ETHER_STATS_CRC_ERR_HI,
1921                 hw, port_id);
1922
1923         /*36-bit statistics counter that collects the number of
1924          *valid TX unicast control frames.
1925          */
1926         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1927                 IPN3KE_10G_TX_STATS_UNICAST_MAC_CTRL_FRAMES_LO,
1928                 IPN3KE_10G_TX_STATS_UNICAST_MAC_CTRL_FRAMES_HI,
1929                 hw, port_id);
1930         hw_stats->eth.tx_unicast += statistics;
1931
1932         /*36-bit statistics counter that collects the number of
1933          *valid RX unicast control frames.
1934          */
1935         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1936                 IPN3KE_10G_RX_STATS_UNICAST_MAC_CTRL_FRAMES_LO,
1937                 IPN3KE_10G_RX_STATS_UNICAST_MAC_CTRL_FRAMES_HI,
1938                 hw, port_id);
1939         hw_stats->eth.rx_unicast += statistics;
1940
1941         /*36-bit statistics counter that collects the number of
1942          *valid TX multicast control frames.
1943          */
1944         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1945                 IPN3KE_10G_TX_STATS_MULTICAST_MAC_CTRL_FRAMES_LO,
1946                 IPN3KE_10G_TX_STATS_MULTICAST_MAC_CTRL_FRAMES_HI,
1947                 hw, port_id);
1948         hw_stats->eth.tx_multicast += statistics;
1949
1950         /*36-bit statistics counter that collects the number of
1951          *valid RX multicast control frames.
1952          */
1953         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1954                 IPN3KE_10G_RX_STATS_MULTICAST_MAC_CTRL_FRAMES_LO,
1955                 IPN3KE_10G_RX_STATS_MULTICAST_MAC_CTRL_FRAMES_HI,
1956                 hw, port_id);
1957         hw_stats->eth.rx_multicast += statistics;
1958
1959         /*36-bit statistics counter that collects the number of
1960          *valid TX broadcast control frames.
1961          */
1962         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1963                 IPN3KE_10G_TX_STATS_BROADCAST_MAC_CTRL_FRAMES_LO,
1964                 IPN3KE_10G_TX_STATS_BROADCAST_MAC_CTRL_FRAMES_HI,
1965                 hw, port_id);
1966         hw_stats->eth.tx_broadcast += statistics;
1967
1968         /*36-bit statistics counter that collects the number of
1969          *valid RX broadcast control frames.
1970          */
1971         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1972                 IPN3KE_10G_RX_STATS_BROADCAST_MAC_CTRL_FRAMES_LO,
1973                 IPN3KE_10G_RX_STATS_BROADCAST_MAC_CTRL_FRAMES_HI,
1974                 hw, port_id);
1975         hw_stats->eth.rx_broadcast += statistics;
1976
1977         /*36-bit statistics counter that collects the number of
1978          *valid TX PFC frames.
1979          */
1980         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1981                 IPN3KE_10G_TX_STATS_PFC_MAC_CTRL_FRAMES_LO,
1982                 IPN3KE_10G_TX_STATS_PFC_MAC_CTRL_FRAMES_HI,
1983                 hw, port_id);
1984
1985         /*36-bit statistics counter that collects the number of
1986          *valid RX PFC frames.
1987          */
1988         statistics = ipn3ke_rpst_read_36bits_statistics_register(
1989                 IPN3KE_10G_RX_STATS_PFC_MAC_CTRL_FRAMES_LO,
1990                 IPN3KE_10G_RX_STATS_PFC_MAC_CTRL_FRAMES_HI,
1991                 hw, port_id);
1992
1993         return 0;
1994 }
1995
1996 static void
1997 ipn3ke_rpst_10g_lineside_tx_stats_reset(struct ipn3ke_hw *hw,
1998 uint16_t port_id)
1999 {
2000         uint32_t tmp;
2001
2002         /*Bit [0]: Set this register to 1 to clear all TX statistics
2003          *counters.
2004          *The IP core clears this bit when all counters are cleared.
2005          *Bits [31:1]: Reserved.
2006          */
2007         tmp = 0x00000000;
2008         (*hw->f_mac_read)(hw,
2009                 &tmp,
2010                 IPN3KE_10G_TX_STATS_CLR,
2011                 port_id,
2012                 0);
2013         tmp |= 0x00000001;
2014         (*hw->f_mac_write)(hw,
2015                 tmp,
2016                 IPN3KE_10G_TX_STATS_CLR,
2017                 port_id,
2018                 0);
2019 }
2020
2021 static void
2022 ipn3ke_rpst_10g_lineside_rx_stats_reset(struct ipn3ke_hw *hw,
2023 uint16_t port_id)
2024 {
2025         uint32_t tmp;
2026
2027         /*Bit [0]: Set this register to 1 to clear all RX statistics
2028          *counters.
2029          *The IP core clears this bit when all counters are cleared.
2030          *Bits [31:1]: Reserved
2031          */
2032         tmp = 0x00000000;
2033         (*hw->f_mac_read)(hw,
2034                 &tmp,
2035                 IPN3KE_10G_RX_STATS_CLR,
2036                 port_id,
2037                 0);
2038         tmp |= 0x00000001;
2039         (*hw->f_mac_write)(hw,
2040                 tmp,
2041                 IPN3KE_10G_RX_STATS_CLR,
2042                 port_id,
2043                 0);
2044 }
2045
2046 static int
2047 ipn3ke_rpst_stats_reset(struct rte_eth_dev *ethdev)
2048 {
2049         uint16_t port_id = 0;
2050         char *ch;
2051         int cnt = 0;
2052         struct rte_afu_device *afu_dev = NULL;
2053         struct ipn3ke_hw *hw = NULL;
2054
2055         if (!ethdev) {
2056                 IPN3KE_AFU_PMD_ERR("ethernet device to reset is NULL!");
2057                 return -EINVAL;
2058         }
2059
2060         afu_dev = RTE_ETH_DEV_TO_AFU(ethdev);
2061         if (!afu_dev) {
2062                 IPN3KE_AFU_PMD_ERR("afu device to reset is NULL!");
2063                 return -EINVAL;
2064         }
2065
2066         if (!afu_dev->shared.data) {
2067                 IPN3KE_AFU_PMD_ERR("hardware data to reset is NULL!");
2068                 return -EINVAL;
2069         }
2070
2071         hw = afu_dev->shared.data;
2072
2073         ch = ethdev->data->name;
2074         if (!ch) {
2075                 IPN3KE_AFU_PMD_ERR("ethdev name is NULL!");
2076                 return -EINVAL;
2077         }
2078         while (ch) {
2079                 if (*ch == '_')
2080                         cnt++;
2081                 ch++;
2082                 if (cnt == 3)
2083                         break;
2084         }
2085         if (!ch) {
2086                 IPN3KE_AFU_PMD_ERR("Can not get port_id from ethdev name!");
2087                 return -EINVAL;
2088         }
2089         port_id = atoi(ch);
2090
2091         if (hw->retimer.mac_type == IFPGA_RAWDEV_RETIMER_MAC_TYPE_25GE_25GAUI) {
2092                 ipn3ke_rpst_25g_nic_side_tx_stats_reset(hw, port_id);
2093                 ipn3ke_rpst_25g_nic_side_rx_stats_reset(hw, port_id);
2094                 ipn3ke_rpst_25g_lineside_tx_stats_reset(hw, port_id);
2095                 ipn3ke_rpst_25g_lineside_rx_stats_reset(hw, port_id);
2096         } else if (hw->retimer.mac_type ==
2097                         IFPGA_RAWDEV_RETIMER_MAC_TYPE_10GE_XFI) {
2098                 ipn3ke_rpst_10g_nic_side_tx_stats_reset(hw, port_id);
2099                 ipn3ke_rpst_10g_nic_side_rx_stats_reset(hw, port_id);
2100                 ipn3ke_rpst_10g_lineside_tx_stats_reset(hw, port_id);
2101                 ipn3ke_rpst_10g_lineside_rx_stats_reset(hw, port_id);
2102         }
2103
2104         return 0;
2105 }
2106
2107 static int
2108 ipn3ke_rpst_stats_get
2109 (struct rte_eth_dev *ethdev, struct rte_eth_stats *stats)
2110 {
2111         uint16_t port_id = 0;
2112         char *ch;
2113         int cnt = 0;
2114         int i = 0;
2115         struct rte_afu_device *afu_dev = NULL;
2116         struct ipn3ke_hw *hw = NULL;
2117         struct ipn3ke_rpst_hw_port_stats hw_stats;
2118
2119         if (!ethdev) {
2120                 IPN3KE_AFU_PMD_ERR("ethernet device to get statistics is NULL");
2121                 return -EINVAL;
2122         }
2123         if (!stats) {
2124                 IPN3KE_AFU_PMD_ERR("Address to return statistics is NULL!");
2125                 return -EINVAL;
2126         }
2127
2128         afu_dev = RTE_ETH_DEV_TO_AFU(ethdev);
2129         if (!afu_dev) {
2130                 IPN3KE_AFU_PMD_ERR("afu device to get statistics is NULL!");
2131                 return -EINVAL;
2132         }
2133
2134         if (!afu_dev->shared.data) {
2135                 IPN3KE_AFU_PMD_ERR("hardware data to get statistics is NULL!");
2136                 return -EINVAL;
2137         }
2138
2139         hw = afu_dev->shared.data;
2140
2141         ch = ethdev->data->name;
2142         if (!ch) {
2143                 IPN3KE_AFU_PMD_ERR("ethdev name is NULL!");
2144                 return -EINVAL;
2145         }
2146         while (ch) {
2147                 if (*ch == '_')
2148                         cnt++;
2149                 ch++;
2150                 if (cnt == 3)
2151                         break;
2152         }
2153         if (!ch) {
2154                 IPN3KE_AFU_PMD_ERR("Can not get port_id from ethdev name!");
2155                 return -EINVAL;
2156         }
2157         port_id = atoi(ch);
2158
2159         if (hw->retimer.mac_type == IFPGA_RAWDEV_RETIMER_MAC_TYPE_25GE_25GAUI) {
2160                 ipn3ke_rpst_read_25g_lineside_stats_registers(hw,
2161                                                         port_id,
2162                                                         &hw_stats);
2163
2164                 stats->ipackets  = hw_stats.rx_size_64
2165                                         + hw_stats.rx_size_65_127
2166                                         + hw_stats.rx_size_128_255
2167                                         + hw_stats.rx_size_256_511
2168                                         + hw_stats.rx_size_512_1023
2169                                         + hw_stats.rx_size_1024_1518
2170                                         + hw_stats.rx_size_big
2171                                         + hw_stats.rx_undersize
2172                                         + hw_stats.rx_fragments
2173                                         + hw_stats.rx_oversize
2174                                         + hw_stats.rx_jabber;
2175                 stats->opackets  = hw_stats.tx_size_64
2176                                         + hw_stats.tx_size_65_127
2177                                         + hw_stats.tx_size_128_255
2178                                         + hw_stats.tx_size_256_511
2179                                         + hw_stats.tx_size_512_1023
2180                                         + hw_stats.tx_size_1024_1518
2181                                         + hw_stats.tx_size_1519_to_max;
2182                 stats->ibytes    = hw_stats.eth.rx_bytes;
2183                 stats->obytes    = hw_stats.eth.tx_bytes;
2184                 stats->imissed   = 0;
2185                 stats->ierrors   = hw_stats.eth.rx_discards
2186                                         + hw_stats.eth.rx_unknown_protocol;
2187                 stats->oerrors   = hw_stats.eth.tx_discards
2188                                         + hw_stats.eth.tx_errors;
2189                 stats->rx_nombuf = 0;
2190                 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) {
2191                         stats->q_ipackets[i] = 0;
2192                         stats->q_opackets[i] = 0;
2193                         stats->q_ibytes[i] = 0;
2194                         stats->q_obytes[i] = 0;
2195                         stats->q_errors[i] = 0;
2196                 }
2197         } else {
2198                 ipn3ke_rpst_read_10g_lineside_stats_registers(hw,
2199                                                         port_id,
2200                                                         &hw_stats,
2201                                                         stats);
2202         }
2203
2204         return 0;
2205 }
2206
2207 static int
2208 ipn3ke_rpst_xstats_get
2209 (struct rte_eth_dev *ethdev, struct rte_eth_xstat *xstats, unsigned int n)
2210 {
2211         uint16_t port_id = 0;
2212         char *ch = NULL;
2213         int cnt = 0;
2214         unsigned int i, count, prio;
2215         struct rte_afu_device *afu_dev = NULL;
2216         struct ipn3ke_hw *hw = NULL;
2217         struct ipn3ke_rpst_hw_port_stats hw_stats;
2218         struct rte_eth_stats stats;
2219
2220         if (!xstats)
2221                 return 0;
2222
2223         if (!ethdev) {
2224                 IPN3KE_AFU_PMD_ERR("ethernet device to get statistics is NULL");
2225                 return -EINVAL;
2226         }
2227
2228         afu_dev = RTE_ETH_DEV_TO_AFU(ethdev);
2229         if (!afu_dev) {
2230                 IPN3KE_AFU_PMD_ERR("afu device to get statistics is NULL!");
2231                 return -EINVAL;
2232         }
2233
2234         if (!afu_dev->shared.data) {
2235                 IPN3KE_AFU_PMD_ERR("hardware data to get statistics is NULL!");
2236                 return -EINVAL;
2237         }
2238
2239         hw = afu_dev->shared.data;
2240
2241         ch = ethdev->data->name;
2242         if (!ch) {
2243                 IPN3KE_AFU_PMD_ERR("ethdev name is NULL!");
2244                 return -EINVAL;
2245         }
2246         while (ch) {
2247                 if (*ch == '_')
2248                         cnt++;
2249                 ch++;
2250                 if (cnt == 3)
2251                         break;
2252         }
2253         if (!ch) {
2254                 IPN3KE_AFU_PMD_ERR("Can not get port_id from ethdev name!");
2255                 return -EINVAL;
2256         }
2257         port_id = atoi(ch);
2258
2259         count = ipn3ke_rpst_xstats_calc_num();
2260         if (n < count)
2261                 return count;
2262
2263         if (hw->retimer.mac_type == IFPGA_RAWDEV_RETIMER_MAC_TYPE_25GE_25GAUI) {
2264                 ipn3ke_rpst_read_25g_lineside_stats_registers(hw,
2265                                                         port_id,
2266                                                         &hw_stats);
2267         } else {
2268                 ipn3ke_rpst_read_10g_lineside_stats_registers(hw,
2269                                                         port_id,
2270                                                         &hw_stats,
2271                                                         &stats);
2272         }
2273
2274         count = 0;
2275
2276         /* Get stats from ipn3ke_rpst_stats */
2277         for (i = 0; i < IPN3KE_RPST_ETH_XSTATS_CNT; i++) {
2278                 xstats[count].value = *(uint64_t *)(((char *)&hw_stats.eth)
2279                         + ipn3ke_rpst_stats_strings[i].offset);
2280                 xstats[count].id = count;
2281                 count++;
2282         }
2283
2284         /* Get individiual stats from ipn3ke_rpst_hw_port */
2285         for (i = 0; i < IPN3KE_RPST_HW_PORT_XSTATS_CNT; i++) {
2286                 xstats[count].value = *(uint64_t *)(((char *)(&hw_stats)) +
2287                         ipn3ke_rpst_hw_port_strings[i].offset);
2288                 xstats[count].id = count;
2289                 count++;
2290         }
2291
2292         /* Get individiual stats from ipn3ke_rpst_rxq_pri */
2293         for (i = 0; i < IPN3KE_RPST_RXQ_PRIO_XSTATS_CNT; i++) {
2294                 for (prio = 0; prio < IPN3KE_RPST_PRIO_XSTATS_CNT; prio++) {
2295                         xstats[count].value =
2296                                 *(uint64_t *)(((char *)(&hw_stats)) +
2297                                 ipn3ke_rpst_rxq_prio_strings[i].offset +
2298                                 (sizeof(uint64_t) * prio));
2299                         xstats[count].id = count;
2300                         count++;
2301                 }
2302         }
2303
2304         /* Get individiual stats from ipn3ke_rpst_txq_prio */
2305         for (i = 0; i < IPN3KE_RPST_TXQ_PRIO_XSTATS_CNT; i++) {
2306                 for (prio = 0; prio < IPN3KE_RPST_PRIO_XSTATS_CNT; prio++) {
2307                         xstats[count].value =
2308                                 *(uint64_t *)(((char *)(&hw_stats)) +
2309                                 ipn3ke_rpst_txq_prio_strings[i].offset +
2310                                 (sizeof(uint64_t) * prio));
2311                         xstats[count].id = count;
2312                         count++;
2313                 }
2314         }
2315
2316         return count;
2317 }
2318
2319 static int
2320 ipn3ke_rpst_xstats_get_names
2321 (__rte_unused struct rte_eth_dev *dev,
2322 struct rte_eth_xstat_name *xstats_names,
2323 __rte_unused unsigned int limit)
2324 {
2325         unsigned int count = 0;
2326         unsigned int i, prio;
2327
2328         if (!xstats_names)
2329                 return ipn3ke_rpst_xstats_calc_num();
2330
2331         /* Note: limit checked in rte_eth_xstats_names() */
2332
2333         /* Get stats from ipn3ke_rpst_stats */
2334         for (i = 0; i < IPN3KE_RPST_ETH_XSTATS_CNT; i++) {
2335                 snprintf(xstats_names[count].name,
2336                          sizeof(xstats_names[count].name),
2337                          "%s",
2338                          ipn3ke_rpst_stats_strings[i].name);
2339                 count++;
2340         }
2341
2342         /* Get individiual stats from ipn3ke_rpst_hw_port */
2343         for (i = 0; i < IPN3KE_RPST_HW_PORT_XSTATS_CNT; i++) {
2344                 snprintf(xstats_names[count].name,
2345                          sizeof(xstats_names[count].name),
2346                          "%s",
2347                          ipn3ke_rpst_hw_port_strings[i].name);
2348                 count++;
2349         }
2350
2351         /* Get individiual stats from ipn3ke_rpst_rxq_pri */
2352         for (i = 0; i < IPN3KE_RPST_RXQ_PRIO_XSTATS_CNT; i++) {
2353                 for (prio = 0; prio < 8; prio++) {
2354                         snprintf(xstats_names[count].name,
2355                                  sizeof(xstats_names[count].name),
2356                                  "rx_priority%u_%s",
2357                                  prio,
2358                                  ipn3ke_rpst_rxq_prio_strings[i].name);
2359                         count++;
2360                 }
2361         }
2362
2363         /* Get individiual stats from ipn3ke_rpst_txq_prio */
2364         for (i = 0; i < IPN3KE_RPST_TXQ_PRIO_XSTATS_CNT; i++) {
2365                 for (prio = 0; prio < 8; prio++) {
2366                         snprintf(xstats_names[count].name,
2367                                  sizeof(xstats_names[count].name),
2368                                  "tx_priority%u_%s",
2369                                  prio,
2370                                  ipn3ke_rpst_txq_prio_strings[i].name);
2371                         count++;
2372                 }
2373         }
2374         return count;
2375 }
2376
2377 static void
2378 ipn3ke_update_link(struct rte_rawdev *rawdev,
2379         uint16_t port, struct rte_eth_link *link)
2380 {
2381         uint64_t line_link_bitmap = 0;
2382         enum ifpga_rawdev_link_speed link_speed;
2383
2384         rawdev->dev_ops->attr_get(rawdev,
2385                                 "LineSideLinkStatus",
2386                                 (uint64_t *)&line_link_bitmap);
2387
2388         /* Parse the link status */
2389         if ((1 << port) & line_link_bitmap)
2390                 link->link_status = 1;
2391         else
2392                 link->link_status = 0;
2393
2394         IPN3KE_AFU_PMD_DEBUG("port is %d\n", port);
2395         IPN3KE_AFU_PMD_DEBUG("link->link_status is %d\n", link->link_status);
2396
2397         rawdev->dev_ops->attr_get(rawdev,
2398                                 "LineSideLinkSpeed",
2399                                 (uint64_t *)&link_speed);
2400         switch (link_speed) {
2401         case IFPGA_RAWDEV_LINK_SPEED_10GB:
2402                 link->link_speed = ETH_SPEED_NUM_10G;
2403                 break;
2404         case IFPGA_RAWDEV_LINK_SPEED_25GB:
2405                 link->link_speed = ETH_SPEED_NUM_25G;
2406                 break;
2407         default:
2408                 IPN3KE_AFU_PMD_ERR("Unknown link speed info %u", link_speed);
2409                 break;
2410         }
2411 }
2412
2413 /*
2414  * Set device link up.
2415  */
2416 int
2417 ipn3ke_rpst_dev_set_link_up(struct rte_eth_dev *dev)
2418 {
2419         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(dev);
2420         struct rte_eth_dev *pf;
2421         int ret = 0;
2422
2423         if (rpst->i40e_pf_eth) {
2424                 ret = rte_eth_dev_set_link_up(rpst->i40e_pf_eth_port_id);
2425                 pf = rpst->i40e_pf_eth;
2426                 (*rpst->i40e_pf_eth->dev_ops->link_update)(pf, 1);
2427         }
2428
2429         return ret;
2430 }
2431
2432 /*
2433  * Set device link down.
2434  */
2435 int
2436 ipn3ke_rpst_dev_set_link_down(struct rte_eth_dev *dev)
2437 {
2438         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(dev);
2439         struct rte_eth_dev *pf;
2440         int ret = 0;
2441
2442         if (rpst->i40e_pf_eth) {
2443                 ret = rte_eth_dev_set_link_down(rpst->i40e_pf_eth_port_id);
2444                 pf = rpst->i40e_pf_eth;
2445                 (*rpst->i40e_pf_eth->dev_ops->link_update)(pf, 1);
2446         }
2447
2448         return ret;
2449 }
2450
2451 int
2452 ipn3ke_rpst_link_update(struct rte_eth_dev *ethdev,
2453         __rte_unused int wait_to_complete)
2454 {
2455         struct ipn3ke_hw *hw = IPN3KE_DEV_PRIVATE_TO_HW(ethdev);
2456         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(ethdev);
2457         struct rte_rawdev *rawdev;
2458         struct rte_eth_link link;
2459         struct rte_eth_dev *pf;
2460
2461         memset(&link, 0, sizeof(link));
2462
2463         link.link_duplex = ETH_LINK_FULL_DUPLEX;
2464         link.link_autoneg = !(ethdev->data->dev_conf.link_speeds &
2465                                 ETH_LINK_SPEED_FIXED);
2466
2467         rawdev = hw->rawdev;
2468         ipn3ke_update_link(rawdev, rpst->port_id, &link);
2469
2470         if (!rpst->ori_linfo.link_status &&
2471                 link.link_status) {
2472                 IPN3KE_AFU_PMD_DEBUG("Update Rpst %d Up\n", rpst->port_id);
2473                 rpst->ori_linfo.link_status = link.link_status;
2474                 rpst->ori_linfo.link_speed = link.link_speed;
2475
2476                 rte_eth_linkstatus_set(ethdev, &link);
2477
2478                 if (rpst->i40e_pf_eth) {
2479                         IPN3KE_AFU_PMD_DEBUG("Update FVL PF %d Up\n",
2480                                 rpst->i40e_pf_eth_port_id);
2481                         rte_eth_dev_set_link_up(rpst->i40e_pf_eth_port_id);
2482                         pf = rpst->i40e_pf_eth;
2483                         (*rpst->i40e_pf_eth->dev_ops->link_update)(pf, 1);
2484                 }
2485         } else if (rpst->ori_linfo.link_status &&
2486                                 !link.link_status) {
2487                 IPN3KE_AFU_PMD_DEBUG("Update Rpst %d Down\n",
2488                         rpst->port_id);
2489                 rpst->ori_linfo.link_status = link.link_status;
2490                 rpst->ori_linfo.link_speed = link.link_speed;
2491
2492                 rte_eth_linkstatus_set(ethdev, &link);
2493
2494                 if (rpst->i40e_pf_eth) {
2495                         IPN3KE_AFU_PMD_DEBUG("Update FVL PF %d Down\n",
2496                                 rpst->i40e_pf_eth_port_id);
2497                         rte_eth_dev_set_link_down(rpst->i40e_pf_eth_port_id);
2498                         pf = rpst->i40e_pf_eth;
2499                         (*rpst->i40e_pf_eth->dev_ops->link_update)(pf, 1);
2500                 }
2501         }
2502
2503         return 0;
2504 }
2505
2506 static int
2507 ipn3ke_rpst_link_check(struct ipn3ke_rpst *rpst)
2508 {
2509         struct ipn3ke_hw *hw;
2510         struct rte_rawdev *rawdev;
2511         struct rte_eth_link link;
2512         struct rte_eth_dev *pf;
2513
2514         if (rpst == NULL)
2515                 return -1;
2516
2517         hw = rpst->hw;
2518
2519         memset(&link, 0, sizeof(link));
2520
2521         link.link_duplex = ETH_LINK_FULL_DUPLEX;
2522         link.link_autoneg = !(rpst->ethdev->data->dev_conf.link_speeds &
2523                                 ETH_LINK_SPEED_FIXED);
2524
2525         rawdev = hw->rawdev;
2526         ipn3ke_update_link(rawdev, rpst->port_id, &link);
2527
2528         if (!rpst->ori_linfo.link_status &&
2529                                 link.link_status) {
2530                 IPN3KE_AFU_PMD_DEBUG("Check Rpst %d Up\n", rpst->port_id);
2531                 rpst->ori_linfo.link_status = link.link_status;
2532                 rpst->ori_linfo.link_speed = link.link_speed;
2533
2534                 rte_eth_linkstatus_set(rpst->ethdev, &link);
2535
2536                 if (rpst->i40e_pf_eth) {
2537                         IPN3KE_AFU_PMD_DEBUG("Check FVL PF %d Up\n",
2538                                 rpst->i40e_pf_eth_port_id);
2539                         rte_eth_dev_set_link_up(rpst->i40e_pf_eth_port_id);
2540                         pf = rpst->i40e_pf_eth;
2541                         (*rpst->i40e_pf_eth->dev_ops->link_update)(pf, 1);
2542                 }
2543         } else if (rpst->ori_linfo.link_status &&
2544                 !link.link_status) {
2545                 IPN3KE_AFU_PMD_DEBUG("Check Rpst %d Down\n", rpst->port_id);
2546                 rpst->ori_linfo.link_status = link.link_status;
2547                 rpst->ori_linfo.link_speed = link.link_speed;
2548
2549                 rte_eth_linkstatus_set(rpst->ethdev, &link);
2550
2551                 if (rpst->i40e_pf_eth) {
2552                         IPN3KE_AFU_PMD_DEBUG("Check FVL PF %d Down\n",
2553                                 rpst->i40e_pf_eth_port_id);
2554                         rte_eth_dev_set_link_down(rpst->i40e_pf_eth_port_id);
2555                         pf = rpst->i40e_pf_eth;
2556                         (*rpst->i40e_pf_eth->dev_ops->link_update)(pf, 1);
2557                 }
2558         }
2559
2560         return 0;
2561 }
2562
2563 static void *
2564 ipn3ke_rpst_scan_handle_request(__rte_unused void *param)
2565 {
2566         struct ipn3ke_rpst *rpst;
2567         int num = 0;
2568 #define MS 1000
2569 #define SCAN_NUM 32
2570
2571         for (;;) {
2572                 num = 0;
2573                 TAILQ_FOREACH(rpst, &ipn3ke_rpst_list, next) {
2574                         if (rpst->i40e_pf_eth &&
2575                                 rpst->ethdev->data->dev_started &&
2576                                 rpst->i40e_pf_eth->data->dev_started)
2577                                 ipn3ke_rpst_link_check(rpst);
2578
2579                         if (++num > SCAN_NUM)
2580                                 rte_delay_us(1 * MS);
2581                 }
2582                 rte_delay_us(50 * MS);
2583
2584                 if (num == 0xffffff)
2585                         return NULL;
2586         }
2587
2588         return NULL;
2589 }
2590
2591 static int
2592 ipn3ke_rpst_scan_check(void)
2593 {
2594         int ret;
2595
2596         if (ipn3ke_rpst_scan_num == 1) {
2597                 ret = rte_ctrl_thread_create(&ipn3ke_rpst_scan_thread,
2598                         "ipn3ke scanner",
2599                         NULL,
2600                         ipn3ke_rpst_scan_handle_request, NULL);
2601                 if (ret) {
2602                         IPN3KE_AFU_PMD_ERR("Fail to create ipn3ke rpst scan thread");
2603                         return -1;
2604                 }
2605         } else if (ipn3ke_rpst_scan_num == 0) {
2606                 ret = pthread_cancel(ipn3ke_rpst_scan_thread);
2607                 if (ret)
2608                         IPN3KE_AFU_PMD_ERR("Can't cancel the thread");
2609
2610                 ret = pthread_join(ipn3ke_rpst_scan_thread, NULL);
2611                 if (ret)
2612                         IPN3KE_AFU_PMD_ERR("Can't join the thread");
2613
2614                 return ret;
2615         }
2616
2617         return 0;
2618 }
2619
2620 int
2621 ipn3ke_rpst_promiscuous_enable(struct rte_eth_dev *ethdev)
2622 {
2623         struct ipn3ke_hw *hw = IPN3KE_DEV_PRIVATE_TO_HW(ethdev);
2624         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(ethdev);
2625         uint32_t rddata, val;
2626
2627         if (hw->retimer.mac_type == IFPGA_RAWDEV_RETIMER_MAC_TYPE_10GE_XFI) {
2628                 /* Enable all unicast */
2629                 (*hw->f_mac_read)(hw,
2630                                 &rddata,
2631                                 IPN3KE_MAC_RX_FRAME_CONTROL,
2632                                 rpst->port_id,
2633                                 0);
2634                 val = 1;
2635                 val &= IPN3KE_MAC_RX_FRAME_CONTROL_EN_ALLUCAST_MASK;
2636                 val |= rddata;
2637                 (*hw->f_mac_write)(hw,
2638                                 val,
2639                                 IPN3KE_MAC_RX_FRAME_CONTROL,
2640                                 rpst->port_id,
2641                                 0);
2642         }
2643
2644         return 0;
2645 }
2646
2647 int
2648 ipn3ke_rpst_promiscuous_disable(struct rte_eth_dev *ethdev)
2649 {
2650         struct ipn3ke_hw *hw = IPN3KE_DEV_PRIVATE_TO_HW(ethdev);
2651         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(ethdev);
2652         uint32_t rddata, val;
2653
2654         if (hw->retimer.mac_type == IFPGA_RAWDEV_RETIMER_MAC_TYPE_10GE_XFI) {
2655                 /* Disable all unicast */
2656                 (*hw->f_mac_read)(hw,
2657                                 &rddata,
2658                                 IPN3KE_MAC_RX_FRAME_CONTROL,
2659                                 rpst->port_id,
2660                                 0);
2661                 val = 0;
2662                 val &= IPN3KE_MAC_RX_FRAME_CONTROL_EN_ALLUCAST_MASK;
2663                 val |= rddata;
2664                 (*hw->f_mac_write)(hw,
2665                                 val,
2666                                 IPN3KE_MAC_RX_FRAME_CONTROL,
2667                                 rpst->port_id,
2668                                 0);
2669         }
2670
2671         return 0;
2672 }
2673
2674 int
2675 ipn3ke_rpst_allmulticast_enable(struct rte_eth_dev *ethdev)
2676 {
2677         struct ipn3ke_hw *hw = IPN3KE_DEV_PRIVATE_TO_HW(ethdev);
2678         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(ethdev);
2679         uint32_t rddata, val;
2680
2681         if (hw->retimer.mac_type == IFPGA_RAWDEV_RETIMER_MAC_TYPE_10GE_XFI) {
2682                 /* Enable all unicast */
2683                 (*hw->f_mac_read)(hw,
2684                                 &rddata,
2685                                 IPN3KE_MAC_RX_FRAME_CONTROL,
2686                                 rpst->port_id,
2687                                 0);
2688                 val = 1;
2689                 val <<= IPN3KE_MAC_RX_FRAME_CONTROL_EN_ALLMCAST_SHIFT;
2690                 val &= IPN3KE_MAC_RX_FRAME_CONTROL_EN_ALLMCAST_MASK;
2691                 val |= rddata;
2692                 (*hw->f_mac_write)(hw,
2693                                 val,
2694                                 IPN3KE_MAC_RX_FRAME_CONTROL,
2695                                 rpst->port_id,
2696                                 0);
2697         }
2698
2699         return 0;
2700 }
2701
2702 int
2703 ipn3ke_rpst_allmulticast_disable(struct rte_eth_dev *ethdev)
2704 {
2705         struct ipn3ke_hw *hw = IPN3KE_DEV_PRIVATE_TO_HW(ethdev);
2706         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(ethdev);
2707         uint32_t rddata, val;
2708
2709         if (hw->retimer.mac_type == IFPGA_RAWDEV_RETIMER_MAC_TYPE_10GE_XFI) {
2710                 /* Disable all unicast */
2711                 (*hw->f_mac_read)(hw,
2712                                 &rddata,
2713                                 IPN3KE_MAC_RX_FRAME_CONTROL,
2714                                 rpst->port_id,
2715                                 0);
2716                 val = 0;
2717                 val <<= IPN3KE_MAC_RX_FRAME_CONTROL_EN_ALLMCAST_SHIFT;
2718                 val &= IPN3KE_MAC_RX_FRAME_CONTROL_EN_ALLMCAST_MASK;
2719                 val |= rddata;
2720                 (*hw->f_mac_write)(hw,
2721                                 val,
2722                                 IPN3KE_MAC_RX_FRAME_CONTROL,
2723                                 rpst->port_id,
2724                                 0);
2725         }
2726
2727         return 0;
2728 }
2729
2730 int
2731 ipn3ke_rpst_mac_addr_set(struct rte_eth_dev *ethdev,
2732                                 struct rte_ether_addr *mac_addr)
2733 {
2734         struct ipn3ke_hw *hw = IPN3KE_DEV_PRIVATE_TO_HW(ethdev);
2735         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(ethdev);
2736         uint32_t val;
2737
2738         if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
2739                 IPN3KE_AFU_PMD_ERR("Tried to set invalid MAC address.");
2740                 return -EINVAL;
2741         }
2742
2743         if (hw->retimer.mac_type == IFPGA_RAWDEV_RETIMER_MAC_TYPE_10GE_XFI) {
2744                 rte_ether_addr_copy(&mac_addr[0], &rpst->mac_addr);
2745
2746                 /* Set mac address */
2747                 rte_memcpy(((char *)(&val)), &mac_addr[0], sizeof(uint32_t));
2748                 (*hw->f_mac_write)(hw,
2749                                 val,
2750                                 IPN3KE_MAC_PRIMARY_MAC_ADDR0,
2751                                 rpst->port_id,
2752                                 0);
2753                 rte_memcpy(((char *)(&val)), &mac_addr[4], sizeof(uint16_t));
2754                 (*hw->f_mac_write)(hw,
2755                                 val,
2756                                 IPN3KE_MAC_PRIMARY_MAC_ADDR0,
2757                                 rpst->port_id,
2758                                 0);
2759         }
2760
2761         return 0;
2762 }
2763
2764 int
2765 ipn3ke_rpst_mtu_set(struct rte_eth_dev *ethdev, uint16_t mtu)
2766 {
2767         int ret = 0;
2768         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(ethdev);
2769         struct rte_eth_dev_data *dev_data = ethdev->data;
2770
2771         /* mtu setting is forbidden if port is start */
2772         /* make sure NIC port is stopped */
2773         if (rpst->i40e_pf_eth && rpst->i40e_pf_eth->data->dev_started) {
2774                 IPN3KE_AFU_PMD_ERR("NIC port %d must "
2775                         "be stopped before configuration",
2776                         rpst->i40e_pf_eth->data->port_id);
2777                 return -EBUSY;
2778         }
2779         /* mtu setting is forbidden if port is start */
2780         if (dev_data->dev_started) {
2781                 IPN3KE_AFU_PMD_ERR("FPGA port %d must "
2782                         "be stopped before configuration",
2783                         dev_data->port_id);
2784                 return -EBUSY;
2785         }
2786
2787         if (rpst->i40e_pf_eth) {
2788                 ret = rpst->i40e_pf_eth->dev_ops->mtu_set(rpst->i40e_pf_eth,
2789                                                         mtu);
2790                 if (!ret)
2791                         rpst->i40e_pf_eth->data->mtu = mtu;
2792         }
2793
2794         return ret;
2795 }
2796
2797 static int
2798 ipn3ke_afu_flow_ops_get(struct rte_eth_dev *ethdev,
2799                         const struct rte_flow_ops **ops)
2800 {
2801         struct ipn3ke_hw *hw;
2802         struct ipn3ke_rpst *rpst;
2803
2804         if (ethdev == NULL)
2805                 return -EINVAL;
2806
2807         hw = IPN3KE_DEV_PRIVATE_TO_HW(ethdev);
2808         rpst = IPN3KE_DEV_PRIVATE_TO_RPST(ethdev);
2809
2810         if (hw->acc_flow)
2811                 *ops = &ipn3ke_flow_ops;
2812         else if (rpst->i40e_pf_eth)
2813                 (*rpst->i40e_pf_eth->dev_ops->flow_ops_get)(ethdev, ops);
2814         else
2815                 return -EINVAL;
2816
2817         return 0;
2818 }
2819
2820 static const struct eth_dev_ops ipn3ke_rpst_dev_ops = {
2821         .dev_infos_get        = ipn3ke_rpst_dev_infos_get,
2822
2823         .dev_configure        = ipn3ke_rpst_dev_configure,
2824         .dev_start            = ipn3ke_rpst_dev_start,
2825         .dev_stop             = ipn3ke_rpst_dev_stop,
2826         .dev_close            = ipn3ke_rpst_dev_close,
2827         .dev_reset            = ipn3ke_rpst_dev_reset,
2828
2829         .stats_get            = ipn3ke_rpst_stats_get,
2830         .xstats_get           = ipn3ke_rpst_xstats_get,
2831         .xstats_get_names     = ipn3ke_rpst_xstats_get_names,
2832         .stats_reset          = ipn3ke_rpst_stats_reset,
2833         .xstats_reset         = ipn3ke_rpst_stats_reset,
2834
2835         .flow_ops_get         = ipn3ke_afu_flow_ops_get,
2836
2837         .rx_queue_start       = ipn3ke_rpst_rx_queue_start,
2838         .rx_queue_stop        = ipn3ke_rpst_rx_queue_stop,
2839         .tx_queue_start       = ipn3ke_rpst_tx_queue_start,
2840         .tx_queue_stop        = ipn3ke_rpst_tx_queue_stop,
2841         .rx_queue_setup       = ipn3ke_rpst_rx_queue_setup,
2842         .tx_queue_setup       = ipn3ke_rpst_tx_queue_setup,
2843
2844         .dev_set_link_up      = ipn3ke_rpst_dev_set_link_up,
2845         .dev_set_link_down    = ipn3ke_rpst_dev_set_link_down,
2846         .link_update          = ipn3ke_rpst_link_update,
2847
2848         .promiscuous_enable   = ipn3ke_rpst_promiscuous_enable,
2849         .promiscuous_disable  = ipn3ke_rpst_promiscuous_disable,
2850         .allmulticast_enable  = ipn3ke_rpst_allmulticast_enable,
2851         .allmulticast_disable = ipn3ke_rpst_allmulticast_disable,
2852         .mac_addr_set         = ipn3ke_rpst_mac_addr_set,
2853         .mtu_set              = ipn3ke_rpst_mtu_set,
2854
2855         .tm_ops_get           = ipn3ke_tm_ops_get,
2856 };
2857
2858 static uint16_t ipn3ke_rpst_recv_pkts(__rte_unused void *rx_q,
2859         __rte_unused struct rte_mbuf **rx_pkts, __rte_unused uint16_t nb_pkts)
2860 {
2861         return 0;
2862 }
2863
2864 static uint16_t
2865 ipn3ke_rpst_xmit_pkts(__rte_unused void *tx_queue,
2866         __rte_unused struct rte_mbuf **tx_pkts, __rte_unused uint16_t nb_pkts)
2867 {
2868         return 0;
2869 }
2870
2871 int
2872 ipn3ke_rpst_init(struct rte_eth_dev *ethdev, void *init_params)
2873 {
2874         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(ethdev);
2875         struct ipn3ke_rpst *representor_param =
2876                         (struct ipn3ke_rpst *)init_params;
2877
2878         if (representor_param->port_id >= representor_param->hw->port_num)
2879                 return -ENODEV;
2880
2881         if (ipn3ke_bridge_func.set_i40e_sw_dev == NULL)
2882                 return -ENOMEM;
2883
2884         rpst->ethdev = ethdev;
2885         rpst->switch_domain_id = representor_param->switch_domain_id;
2886         rpst->port_id = representor_param->port_id;
2887         rpst->hw = representor_param->hw;
2888         rpst->i40e_pf_eth = representor_param->i40e_pf_eth;
2889         rpst->i40e_pf_eth_port_id = representor_param->i40e_pf_eth_port_id;
2890         if (rpst->i40e_pf_eth)
2891                 ipn3ke_bridge_func.set_i40e_sw_dev(rpst->i40e_pf_eth_port_id,
2892                                             rpst->ethdev);
2893
2894         ethdev->data->mac_addrs = rte_zmalloc("ipn3ke", RTE_ETHER_ADDR_LEN, 0);
2895         if (!ethdev->data->mac_addrs) {
2896                 IPN3KE_AFU_PMD_ERR("Failed to "
2897                         "allocated memory for storing mac address");
2898                 return -ENODEV;
2899         }
2900
2901         if (rpst->hw->tm_hw_enable)
2902                 ipn3ke_tm_init(rpst);
2903
2904         /* Set representor device ops */
2905         ethdev->dev_ops = &ipn3ke_rpst_dev_ops;
2906
2907         /* No data-path, but need stub Rx/Tx functions to avoid crash
2908          * when testing with the likes of testpmd.
2909          */
2910         ethdev->rx_pkt_burst = ipn3ke_rpst_recv_pkts;
2911         ethdev->tx_pkt_burst = ipn3ke_rpst_xmit_pkts;
2912
2913         ethdev->data->nb_rx_queues = 1;
2914         ethdev->data->nb_tx_queues = 1;
2915
2916         ethdev->data->mac_addrs = rte_zmalloc("ipn3ke_afu_representor",
2917                                                 RTE_ETHER_ADDR_LEN,
2918                                                 0);
2919         if (!ethdev->data->mac_addrs) {
2920                 IPN3KE_AFU_PMD_ERR("Failed to "
2921                         "allocated memory for storing mac address");
2922                 return -ENODEV;
2923         }
2924
2925         ethdev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR |
2926                                         RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
2927
2928         rte_spinlock_lock(&ipn3ke_link_notify_list_lk);
2929         TAILQ_INSERT_TAIL(&ipn3ke_rpst_list, rpst, next);
2930         ipn3ke_rpst_scan_num++;
2931         ipn3ke_rpst_scan_check();
2932         rte_spinlock_unlock(&ipn3ke_link_notify_list_lk);
2933
2934         return 0;
2935 }
2936
2937 int
2938 ipn3ke_rpst_uninit(struct rte_eth_dev *ethdev)
2939 {
2940         struct ipn3ke_rpst *rpst = IPN3KE_DEV_PRIVATE_TO_RPST(ethdev);
2941
2942         rte_spinlock_lock(&ipn3ke_link_notify_list_lk);
2943         TAILQ_REMOVE(&ipn3ke_rpst_list, rpst, next);
2944         ipn3ke_rpst_scan_num--;
2945         ipn3ke_rpst_scan_check();
2946         rte_spinlock_unlock(&ipn3ke_link_notify_list_lk);
2947
2948         return 0;
2949 }