xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/cavium/thunder/nicvf_ethtool.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2015 Cavium, Inc.
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun /* ETHTOOL Support for VNIC_VF Device*/
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/pci.h>
9*4882a593Smuzhiyun #include <linux/net_tstamp.h>
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include "nic_reg.h"
12*4882a593Smuzhiyun #include "nic.h"
13*4882a593Smuzhiyun #include "nicvf_queues.h"
14*4882a593Smuzhiyun #include "q_struct.h"
15*4882a593Smuzhiyun #include "thunder_bgx.h"
16*4882a593Smuzhiyun #include "../common/cavium_ptp.h"
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define DRV_NAME	"nicvf"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun struct nicvf_stat {
21*4882a593Smuzhiyun 	char name[ETH_GSTRING_LEN];
22*4882a593Smuzhiyun 	unsigned int index;
23*4882a593Smuzhiyun };
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #define NICVF_HW_STAT(stat) { \
26*4882a593Smuzhiyun 	.name = #stat, \
27*4882a593Smuzhiyun 	.index = offsetof(struct nicvf_hw_stats, stat) / sizeof(u64), \
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define NICVF_DRV_STAT(stat) { \
31*4882a593Smuzhiyun 	.name = #stat, \
32*4882a593Smuzhiyun 	.index = offsetof(struct nicvf_drv_stats, stat) / sizeof(u64), \
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun static const struct nicvf_stat nicvf_hw_stats[] = {
36*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_bytes),
37*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_frames),
38*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_ucast_frames),
39*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_bcast_frames),
40*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_mcast_frames),
41*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_drops),
42*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_drop_red),
43*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_drop_red_bytes),
44*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_drop_overrun),
45*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_drop_overrun_bytes),
46*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_drop_bcast),
47*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_drop_mcast),
48*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_drop_l3_bcast),
49*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_drop_l3_mcast),
50*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_fcs_errors),
51*4882a593Smuzhiyun 	NICVF_HW_STAT(rx_l2_errors),
52*4882a593Smuzhiyun 	NICVF_HW_STAT(tx_bytes),
53*4882a593Smuzhiyun 	NICVF_HW_STAT(tx_frames),
54*4882a593Smuzhiyun 	NICVF_HW_STAT(tx_ucast_frames),
55*4882a593Smuzhiyun 	NICVF_HW_STAT(tx_bcast_frames),
56*4882a593Smuzhiyun 	NICVF_HW_STAT(tx_mcast_frames),
57*4882a593Smuzhiyun 	NICVF_HW_STAT(tx_drops),
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun static const struct nicvf_stat nicvf_drv_stats[] = {
61*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_bgx_truncated_pkts),
62*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_jabber_errs),
63*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_fcs_errs),
64*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_bgx_errs),
65*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_prel2_errs),
66*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_l2_hdr_malformed),
67*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_oversize),
68*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_undersize),
69*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_l2_len_mismatch),
70*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_l2_pclp),
71*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_ip_ver_errs),
72*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_ip_csum_errs),
73*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_ip_hdr_malformed),
74*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_ip_payload_malformed),
75*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_ip_ttl_errs),
76*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_l3_pclp),
77*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_l4_malformed),
78*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_l4_csum_errs),
79*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_udp_len_errs),
80*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_l4_port_errs),
81*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_tcp_flag_errs),
82*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_tcp_offset_errs),
83*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_l4_pclp),
84*4882a593Smuzhiyun 	NICVF_DRV_STAT(rx_truncated_pkts),
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_desc_fault),
87*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_hdr_cons_err),
88*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_subdesc_err),
89*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_max_size_exceeded),
90*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_imm_size_oflow),
91*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_data_seq_err),
92*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_mem_seq_err),
93*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_lock_viol),
94*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_data_fault),
95*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_tstmp_conflict),
96*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_tstmp_timeout),
97*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_mem_fault),
98*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_csum_overlap),
99*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_csum_overflow),
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_tso),
102*4882a593Smuzhiyun 	NICVF_DRV_STAT(tx_timeout),
103*4882a593Smuzhiyun 	NICVF_DRV_STAT(txq_stop),
104*4882a593Smuzhiyun 	NICVF_DRV_STAT(txq_wake),
105*4882a593Smuzhiyun 	NICVF_DRV_STAT(rcv_buffer_alloc_failures),
106*4882a593Smuzhiyun 	NICVF_DRV_STAT(page_alloc),
107*4882a593Smuzhiyun };
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun static const struct nicvf_stat nicvf_queue_stats[] = {
110*4882a593Smuzhiyun 	{ "bytes", 0 },
111*4882a593Smuzhiyun 	{ "frames", 1 },
112*4882a593Smuzhiyun };
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun static const unsigned int nicvf_n_hw_stats = ARRAY_SIZE(nicvf_hw_stats);
115*4882a593Smuzhiyun static const unsigned int nicvf_n_drv_stats = ARRAY_SIZE(nicvf_drv_stats);
116*4882a593Smuzhiyun static const unsigned int nicvf_n_queue_stats = ARRAY_SIZE(nicvf_queue_stats);
117*4882a593Smuzhiyun 
nicvf_get_link_ksettings(struct net_device * netdev,struct ethtool_link_ksettings * cmd)118*4882a593Smuzhiyun static int nicvf_get_link_ksettings(struct net_device *netdev,
119*4882a593Smuzhiyun 				    struct ethtool_link_ksettings *cmd)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(netdev);
122*4882a593Smuzhiyun 	u32 supported, advertising;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	supported = 0;
125*4882a593Smuzhiyun 	advertising = 0;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	if (!nic->link_up) {
128*4882a593Smuzhiyun 		cmd->base.duplex = DUPLEX_UNKNOWN;
129*4882a593Smuzhiyun 		cmd->base.speed = SPEED_UNKNOWN;
130*4882a593Smuzhiyun 		return 0;
131*4882a593Smuzhiyun 	}
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	switch (nic->speed) {
134*4882a593Smuzhiyun 	case SPEED_1000:
135*4882a593Smuzhiyun 		cmd->base.port = PORT_MII | PORT_TP;
136*4882a593Smuzhiyun 		cmd->base.autoneg = AUTONEG_ENABLE;
137*4882a593Smuzhiyun 		supported |= SUPPORTED_MII | SUPPORTED_TP;
138*4882a593Smuzhiyun 		supported |= SUPPORTED_1000baseT_Full |
139*4882a593Smuzhiyun 				  SUPPORTED_1000baseT_Half |
140*4882a593Smuzhiyun 				  SUPPORTED_100baseT_Full  |
141*4882a593Smuzhiyun 				  SUPPORTED_100baseT_Half  |
142*4882a593Smuzhiyun 				  SUPPORTED_10baseT_Full   |
143*4882a593Smuzhiyun 				  SUPPORTED_10baseT_Half;
144*4882a593Smuzhiyun 		supported |= SUPPORTED_Autoneg;
145*4882a593Smuzhiyun 		advertising |= ADVERTISED_1000baseT_Full |
146*4882a593Smuzhiyun 				    ADVERTISED_1000baseT_Half |
147*4882a593Smuzhiyun 				    ADVERTISED_100baseT_Full  |
148*4882a593Smuzhiyun 				    ADVERTISED_100baseT_Half  |
149*4882a593Smuzhiyun 				    ADVERTISED_10baseT_Full   |
150*4882a593Smuzhiyun 				    ADVERTISED_10baseT_Half;
151*4882a593Smuzhiyun 		break;
152*4882a593Smuzhiyun 	case SPEED_10000:
153*4882a593Smuzhiyun 		if (nic->mac_type == BGX_MODE_RXAUI) {
154*4882a593Smuzhiyun 			cmd->base.port = PORT_TP;
155*4882a593Smuzhiyun 			supported |= SUPPORTED_TP;
156*4882a593Smuzhiyun 		} else {
157*4882a593Smuzhiyun 			cmd->base.port = PORT_FIBRE;
158*4882a593Smuzhiyun 			supported |= SUPPORTED_FIBRE;
159*4882a593Smuzhiyun 		}
160*4882a593Smuzhiyun 		cmd->base.autoneg = AUTONEG_DISABLE;
161*4882a593Smuzhiyun 		supported |= SUPPORTED_10000baseT_Full;
162*4882a593Smuzhiyun 		break;
163*4882a593Smuzhiyun 	case SPEED_40000:
164*4882a593Smuzhiyun 		cmd->base.port = PORT_FIBRE;
165*4882a593Smuzhiyun 		cmd->base.autoneg = AUTONEG_DISABLE;
166*4882a593Smuzhiyun 		supported |= SUPPORTED_FIBRE;
167*4882a593Smuzhiyun 		supported |= SUPPORTED_40000baseCR4_Full;
168*4882a593Smuzhiyun 		break;
169*4882a593Smuzhiyun 	}
170*4882a593Smuzhiyun 	cmd->base.duplex = nic->duplex;
171*4882a593Smuzhiyun 	cmd->base.speed = nic->speed;
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
174*4882a593Smuzhiyun 						supported);
175*4882a593Smuzhiyun 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
176*4882a593Smuzhiyun 						advertising);
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	return 0;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
nicvf_get_link(struct net_device * netdev)181*4882a593Smuzhiyun static u32 nicvf_get_link(struct net_device *netdev)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(netdev);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	return nic->link_up;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
nicvf_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * info)188*4882a593Smuzhiyun static void nicvf_get_drvinfo(struct net_device *netdev,
189*4882a593Smuzhiyun 			      struct ethtool_drvinfo *info)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(netdev);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
194*4882a593Smuzhiyun 	strlcpy(info->bus_info, pci_name(nic->pdev), sizeof(info->bus_info));
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun 
nicvf_get_msglevel(struct net_device * netdev)197*4882a593Smuzhiyun static u32 nicvf_get_msglevel(struct net_device *netdev)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(netdev);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	return nic->msg_enable;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun 
nicvf_set_msglevel(struct net_device * netdev,u32 lvl)204*4882a593Smuzhiyun static void nicvf_set_msglevel(struct net_device *netdev, u32 lvl)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(netdev);
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	nic->msg_enable = lvl;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun 
nicvf_get_qset_strings(struct nicvf * nic,u8 ** data,int qset)211*4882a593Smuzhiyun static void nicvf_get_qset_strings(struct nicvf *nic, u8 **data, int qset)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun 	int stats, qidx;
214*4882a593Smuzhiyun 	int start_qidx = qset * MAX_RCV_QUEUES_PER_QS;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	for (qidx = 0; qidx < nic->qs->rq_cnt; qidx++) {
217*4882a593Smuzhiyun 		for (stats = 0; stats < nicvf_n_queue_stats; stats++) {
218*4882a593Smuzhiyun 			sprintf(*data, "rxq%d: %s", qidx + start_qidx,
219*4882a593Smuzhiyun 				nicvf_queue_stats[stats].name);
220*4882a593Smuzhiyun 			*data += ETH_GSTRING_LEN;
221*4882a593Smuzhiyun 		}
222*4882a593Smuzhiyun 	}
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	for (qidx = 0; qidx < nic->qs->sq_cnt; qidx++) {
225*4882a593Smuzhiyun 		for (stats = 0; stats < nicvf_n_queue_stats; stats++) {
226*4882a593Smuzhiyun 			sprintf(*data, "txq%d: %s", qidx + start_qidx,
227*4882a593Smuzhiyun 				nicvf_queue_stats[stats].name);
228*4882a593Smuzhiyun 			*data += ETH_GSTRING_LEN;
229*4882a593Smuzhiyun 		}
230*4882a593Smuzhiyun 	}
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun 
nicvf_get_strings(struct net_device * netdev,u32 sset,u8 * data)233*4882a593Smuzhiyun static void nicvf_get_strings(struct net_device *netdev, u32 sset, u8 *data)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(netdev);
236*4882a593Smuzhiyun 	int stats;
237*4882a593Smuzhiyun 	int sqs;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	if (sset != ETH_SS_STATS)
240*4882a593Smuzhiyun 		return;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	for (stats = 0; stats < nicvf_n_hw_stats; stats++) {
243*4882a593Smuzhiyun 		memcpy(data, nicvf_hw_stats[stats].name, ETH_GSTRING_LEN);
244*4882a593Smuzhiyun 		data += ETH_GSTRING_LEN;
245*4882a593Smuzhiyun 	}
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	for (stats = 0; stats < nicvf_n_drv_stats; stats++) {
248*4882a593Smuzhiyun 		memcpy(data, nicvf_drv_stats[stats].name, ETH_GSTRING_LEN);
249*4882a593Smuzhiyun 		data += ETH_GSTRING_LEN;
250*4882a593Smuzhiyun 	}
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	nicvf_get_qset_strings(nic, &data, 0);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	for (sqs = 0; sqs < nic->sqs_count; sqs++) {
255*4882a593Smuzhiyun 		if (!nic->snicvf[sqs])
256*4882a593Smuzhiyun 			continue;
257*4882a593Smuzhiyun 		nicvf_get_qset_strings(nic->snicvf[sqs], &data, sqs + 1);
258*4882a593Smuzhiyun 	}
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	for (stats = 0; stats < BGX_RX_STATS_COUNT; stats++) {
261*4882a593Smuzhiyun 		sprintf(data, "bgx_rxstat%d: ", stats);
262*4882a593Smuzhiyun 		data += ETH_GSTRING_LEN;
263*4882a593Smuzhiyun 	}
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	for (stats = 0; stats < BGX_TX_STATS_COUNT; stats++) {
266*4882a593Smuzhiyun 		sprintf(data, "bgx_txstat%d: ", stats);
267*4882a593Smuzhiyun 		data += ETH_GSTRING_LEN;
268*4882a593Smuzhiyun 	}
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun 
nicvf_get_sset_count(struct net_device * netdev,int sset)271*4882a593Smuzhiyun static int nicvf_get_sset_count(struct net_device *netdev, int sset)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(netdev);
274*4882a593Smuzhiyun 	int qstats_count;
275*4882a593Smuzhiyun 	int sqs;
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	if (sset != ETH_SS_STATS)
278*4882a593Smuzhiyun 		return -EINVAL;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	qstats_count = nicvf_n_queue_stats *
281*4882a593Smuzhiyun 		       (nic->qs->rq_cnt + nic->qs->sq_cnt);
282*4882a593Smuzhiyun 	for (sqs = 0; sqs < nic->sqs_count; sqs++) {
283*4882a593Smuzhiyun 		struct nicvf *snic;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 		snic = nic->snicvf[sqs];
286*4882a593Smuzhiyun 		if (!snic)
287*4882a593Smuzhiyun 			continue;
288*4882a593Smuzhiyun 		qstats_count += nicvf_n_queue_stats *
289*4882a593Smuzhiyun 				(snic->qs->rq_cnt + snic->qs->sq_cnt);
290*4882a593Smuzhiyun 	}
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	return nicvf_n_hw_stats + nicvf_n_drv_stats +
293*4882a593Smuzhiyun 		qstats_count +
294*4882a593Smuzhiyun 		BGX_RX_STATS_COUNT + BGX_TX_STATS_COUNT;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun 
nicvf_get_qset_stats(struct nicvf * nic,struct ethtool_stats * stats,u64 ** data)297*4882a593Smuzhiyun static void nicvf_get_qset_stats(struct nicvf *nic,
298*4882a593Smuzhiyun 				 struct ethtool_stats *stats, u64 **data)
299*4882a593Smuzhiyun {
300*4882a593Smuzhiyun 	int stat, qidx;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	if (!nic)
303*4882a593Smuzhiyun 		return;
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	for (qidx = 0; qidx < nic->qs->rq_cnt; qidx++) {
306*4882a593Smuzhiyun 		nicvf_update_rq_stats(nic, qidx);
307*4882a593Smuzhiyun 		for (stat = 0; stat < nicvf_n_queue_stats; stat++)
308*4882a593Smuzhiyun 			*((*data)++) = ((u64 *)&nic->qs->rq[qidx].stats)
309*4882a593Smuzhiyun 					[nicvf_queue_stats[stat].index];
310*4882a593Smuzhiyun 	}
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	for (qidx = 0; qidx < nic->qs->sq_cnt; qidx++) {
313*4882a593Smuzhiyun 		nicvf_update_sq_stats(nic, qidx);
314*4882a593Smuzhiyun 		for (stat = 0; stat < nicvf_n_queue_stats; stat++)
315*4882a593Smuzhiyun 			*((*data)++) = ((u64 *)&nic->qs->sq[qidx].stats)
316*4882a593Smuzhiyun 					[nicvf_queue_stats[stat].index];
317*4882a593Smuzhiyun 	}
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun 
nicvf_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * data)320*4882a593Smuzhiyun static void nicvf_get_ethtool_stats(struct net_device *netdev,
321*4882a593Smuzhiyun 				    struct ethtool_stats *stats, u64 *data)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(netdev);
324*4882a593Smuzhiyun 	int stat, tmp_stats;
325*4882a593Smuzhiyun 	int sqs, cpu;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	nicvf_update_stats(nic);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	/* Update LMAC stats */
330*4882a593Smuzhiyun 	nicvf_update_lmac_stats(nic);
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	for (stat = 0; stat < nicvf_n_hw_stats; stat++)
333*4882a593Smuzhiyun 		*(data++) = ((u64 *)&nic->hw_stats)
334*4882a593Smuzhiyun 				[nicvf_hw_stats[stat].index];
335*4882a593Smuzhiyun 	for (stat = 0; stat < nicvf_n_drv_stats; stat++) {
336*4882a593Smuzhiyun 		tmp_stats = 0;
337*4882a593Smuzhiyun 		for_each_possible_cpu(cpu)
338*4882a593Smuzhiyun 			tmp_stats += ((u64 *)per_cpu_ptr(nic->drv_stats, cpu))
339*4882a593Smuzhiyun 				     [nicvf_drv_stats[stat].index];
340*4882a593Smuzhiyun 		*(data++) = tmp_stats;
341*4882a593Smuzhiyun 	}
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	nicvf_get_qset_stats(nic, stats, &data);
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	for (sqs = 0; sqs < nic->sqs_count; sqs++) {
346*4882a593Smuzhiyun 		if (!nic->snicvf[sqs])
347*4882a593Smuzhiyun 			continue;
348*4882a593Smuzhiyun 		nicvf_get_qset_stats(nic->snicvf[sqs], stats, &data);
349*4882a593Smuzhiyun 	}
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	for (stat = 0; stat < BGX_RX_STATS_COUNT; stat++)
352*4882a593Smuzhiyun 		*(data++) = nic->bgx_stats.rx_stats[stat];
353*4882a593Smuzhiyun 	for (stat = 0; stat < BGX_TX_STATS_COUNT; stat++)
354*4882a593Smuzhiyun 		*(data++) = nic->bgx_stats.tx_stats[stat];
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun 
nicvf_get_regs_len(struct net_device * dev)357*4882a593Smuzhiyun static int nicvf_get_regs_len(struct net_device *dev)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	return sizeof(u64) * NIC_VF_REG_COUNT;
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun 
nicvf_get_regs(struct net_device * dev,struct ethtool_regs * regs,void * reg)362*4882a593Smuzhiyun static void nicvf_get_regs(struct net_device *dev,
363*4882a593Smuzhiyun 			   struct ethtool_regs *regs, void *reg)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(dev);
366*4882a593Smuzhiyun 	u64 *p = (u64 *)reg;
367*4882a593Smuzhiyun 	u64 reg_offset;
368*4882a593Smuzhiyun 	int mbox, key, stat, q;
369*4882a593Smuzhiyun 	int i = 0;
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	regs->version = 0;
372*4882a593Smuzhiyun 	memset(p, 0, NIC_VF_REG_COUNT);
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	p[i++] = nicvf_reg_read(nic, NIC_VNIC_CFG);
375*4882a593Smuzhiyun 	/* Mailbox registers */
376*4882a593Smuzhiyun 	for (mbox = 0; mbox < NIC_PF_VF_MAILBOX_SIZE; mbox++)
377*4882a593Smuzhiyun 		p[i++] = nicvf_reg_read(nic,
378*4882a593Smuzhiyun 					NIC_VF_PF_MAILBOX_0_1 | (mbox << 3));
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	p[i++] = nicvf_reg_read(nic, NIC_VF_INT);
381*4882a593Smuzhiyun 	p[i++] = nicvf_reg_read(nic, NIC_VF_INT_W1S);
382*4882a593Smuzhiyun 	p[i++] = nicvf_reg_read(nic, NIC_VF_ENA_W1C);
383*4882a593Smuzhiyun 	p[i++] = nicvf_reg_read(nic, NIC_VF_ENA_W1S);
384*4882a593Smuzhiyun 	p[i++] = nicvf_reg_read(nic, NIC_VNIC_RSS_CFG);
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	for (key = 0; key < RSS_HASH_KEY_SIZE; key++)
387*4882a593Smuzhiyun 		p[i++] = nicvf_reg_read(nic, NIC_VNIC_RSS_KEY_0_4 | (key << 3));
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	/* Tx/Rx statistics */
390*4882a593Smuzhiyun 	for (stat = 0; stat < TX_STATS_ENUM_LAST; stat++)
391*4882a593Smuzhiyun 		p[i++] = nicvf_reg_read(nic,
392*4882a593Smuzhiyun 					NIC_VNIC_TX_STAT_0_4 | (stat << 3));
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	for (i = 0; i < RX_STATS_ENUM_LAST; i++)
395*4882a593Smuzhiyun 		p[i++] = nicvf_reg_read(nic,
396*4882a593Smuzhiyun 					NIC_VNIC_RX_STAT_0_13 | (stat << 3));
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	p[i++] = nicvf_reg_read(nic, NIC_QSET_RQ_GEN_CFG);
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	/* All completion queue's registers */
401*4882a593Smuzhiyun 	for (q = 0; q < MAX_CMP_QUEUES_PER_QS; q++) {
402*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_CFG, q);
403*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_CFG2, q);
404*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_THRESH, q);
405*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_BASE, q);
406*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_HEAD, q);
407*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_TAIL, q);
408*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_DOOR, q);
409*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS, q);
410*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS2, q);
411*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_DEBUG, q);
412*4882a593Smuzhiyun 	}
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	/* All receive queue's registers */
415*4882a593Smuzhiyun 	for (q = 0; q < MAX_RCV_QUEUES_PER_QS; q++) {
416*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RQ_0_7_CFG, q);
417*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic,
418*4882a593Smuzhiyun 						  NIC_QSET_RQ_0_7_STAT_0_1, q);
419*4882a593Smuzhiyun 		reg_offset = NIC_QSET_RQ_0_7_STAT_0_1 | (1 << 3);
420*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, reg_offset, q);
421*4882a593Smuzhiyun 	}
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	for (q = 0; q < MAX_SND_QUEUES_PER_QS; q++) {
424*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, q);
425*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_THRESH, q);
426*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_BASE, q);
427*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_HEAD, q);
428*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_TAIL, q);
429*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_DOOR, q);
430*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_STATUS, q);
431*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_DEBUG, q);
432*4882a593Smuzhiyun 		/* Padding, was NIC_QSET_SQ_0_7_CNM_CHG, which
433*4882a593Smuzhiyun 		 * produces bus errors when read
434*4882a593Smuzhiyun 		 */
435*4882a593Smuzhiyun 		p[i++] = 0;
436*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_STAT_0_1, q);
437*4882a593Smuzhiyun 		reg_offset = NIC_QSET_SQ_0_7_STAT_0_1 | (1 << 3);
438*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, reg_offset, q);
439*4882a593Smuzhiyun 	}
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	for (q = 0; q < MAX_RCV_BUF_DESC_RINGS_PER_QS; q++) {
442*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_CFG, q);
443*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_THRESH, q);
444*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_BASE, q);
445*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_HEAD, q);
446*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_TAIL, q);
447*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_DOOR, q);
448*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic,
449*4882a593Smuzhiyun 					      NIC_QSET_RBDR_0_1_STATUS0, q);
450*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic,
451*4882a593Smuzhiyun 					      NIC_QSET_RBDR_0_1_STATUS1, q);
452*4882a593Smuzhiyun 		reg_offset = NIC_QSET_RBDR_0_1_PREFETCH_STATUS;
453*4882a593Smuzhiyun 		p[i++] = nicvf_queue_reg_read(nic, reg_offset, q);
454*4882a593Smuzhiyun 	}
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun 
nicvf_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * cmd)457*4882a593Smuzhiyun static int nicvf_get_coalesce(struct net_device *netdev,
458*4882a593Smuzhiyun 			      struct ethtool_coalesce *cmd)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(netdev);
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	cmd->rx_coalesce_usecs = nic->cq_coalesce_usecs;
463*4882a593Smuzhiyun 	return 0;
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun 
nicvf_get_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring)466*4882a593Smuzhiyun static void nicvf_get_ringparam(struct net_device *netdev,
467*4882a593Smuzhiyun 				struct ethtool_ringparam *ring)
468*4882a593Smuzhiyun {
469*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(netdev);
470*4882a593Smuzhiyun 	struct queue_set *qs = nic->qs;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	ring->rx_max_pending = MAX_CMP_QUEUE_LEN;
473*4882a593Smuzhiyun 	ring->rx_pending = qs->cq_len;
474*4882a593Smuzhiyun 	ring->tx_max_pending = MAX_SND_QUEUE_LEN;
475*4882a593Smuzhiyun 	ring->tx_pending = qs->sq_len;
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun 
nicvf_set_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring)478*4882a593Smuzhiyun static int nicvf_set_ringparam(struct net_device *netdev,
479*4882a593Smuzhiyun 			       struct ethtool_ringparam *ring)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(netdev);
482*4882a593Smuzhiyun 	struct queue_set *qs = nic->qs;
483*4882a593Smuzhiyun 	u32 rx_count, tx_count;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	/* Due to HW errata this is not supported on T88 pass 1.x silicon */
486*4882a593Smuzhiyun 	if (pass1_silicon(nic->pdev))
487*4882a593Smuzhiyun 		return -EINVAL;
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
490*4882a593Smuzhiyun 		return -EINVAL;
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	tx_count = clamp_t(u32, ring->tx_pending,
493*4882a593Smuzhiyun 			   MIN_SND_QUEUE_LEN, MAX_SND_QUEUE_LEN);
494*4882a593Smuzhiyun 	rx_count = clamp_t(u32, ring->rx_pending,
495*4882a593Smuzhiyun 			   MIN_CMP_QUEUE_LEN, MAX_CMP_QUEUE_LEN);
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	if ((tx_count == qs->sq_len) && (rx_count == qs->cq_len))
498*4882a593Smuzhiyun 		return 0;
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	/* Permitted lengths are 1K, 2K, 4K, 8K, 16K, 32K, 64K */
501*4882a593Smuzhiyun 	qs->sq_len = rounddown_pow_of_two(tx_count);
502*4882a593Smuzhiyun 	qs->cq_len = rounddown_pow_of_two(rx_count);
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	if (netif_running(netdev)) {
505*4882a593Smuzhiyun 		nicvf_stop(netdev);
506*4882a593Smuzhiyun 		nicvf_open(netdev);
507*4882a593Smuzhiyun 	}
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	return 0;
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun 
nicvf_get_rss_hash_opts(struct nicvf * nic,struct ethtool_rxnfc * info)512*4882a593Smuzhiyun static int nicvf_get_rss_hash_opts(struct nicvf *nic,
513*4882a593Smuzhiyun 				   struct ethtool_rxnfc *info)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun 	info->data = 0;
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	switch (info->flow_type) {
518*4882a593Smuzhiyun 	case TCP_V4_FLOW:
519*4882a593Smuzhiyun 	case TCP_V6_FLOW:
520*4882a593Smuzhiyun 	case UDP_V4_FLOW:
521*4882a593Smuzhiyun 	case UDP_V6_FLOW:
522*4882a593Smuzhiyun 	case SCTP_V4_FLOW:
523*4882a593Smuzhiyun 	case SCTP_V6_FLOW:
524*4882a593Smuzhiyun 		info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
525*4882a593Smuzhiyun 		fallthrough;
526*4882a593Smuzhiyun 	case IPV4_FLOW:
527*4882a593Smuzhiyun 	case IPV6_FLOW:
528*4882a593Smuzhiyun 		info->data |= RXH_IP_SRC | RXH_IP_DST;
529*4882a593Smuzhiyun 		break;
530*4882a593Smuzhiyun 	default:
531*4882a593Smuzhiyun 		return -EINVAL;
532*4882a593Smuzhiyun 	}
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	return 0;
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun 
nicvf_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info,u32 * rules)537*4882a593Smuzhiyun static int nicvf_get_rxnfc(struct net_device *dev,
538*4882a593Smuzhiyun 			   struct ethtool_rxnfc *info, u32 *rules)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(dev);
541*4882a593Smuzhiyun 	int ret = -EOPNOTSUPP;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	switch (info->cmd) {
544*4882a593Smuzhiyun 	case ETHTOOL_GRXRINGS:
545*4882a593Smuzhiyun 		info->data = nic->rx_queues;
546*4882a593Smuzhiyun 		ret = 0;
547*4882a593Smuzhiyun 		break;
548*4882a593Smuzhiyun 	case ETHTOOL_GRXFH:
549*4882a593Smuzhiyun 		return nicvf_get_rss_hash_opts(nic, info);
550*4882a593Smuzhiyun 	default:
551*4882a593Smuzhiyun 		break;
552*4882a593Smuzhiyun 	}
553*4882a593Smuzhiyun 	return ret;
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun 
nicvf_set_rss_hash_opts(struct nicvf * nic,struct ethtool_rxnfc * info)556*4882a593Smuzhiyun static int nicvf_set_rss_hash_opts(struct nicvf *nic,
557*4882a593Smuzhiyun 				   struct ethtool_rxnfc *info)
558*4882a593Smuzhiyun {
559*4882a593Smuzhiyun 	struct nicvf_rss_info *rss = &nic->rss_info;
560*4882a593Smuzhiyun 	u64 rss_cfg = nicvf_reg_read(nic, NIC_VNIC_RSS_CFG);
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	if (!rss->enable)
563*4882a593Smuzhiyun 		netdev_err(nic->netdev,
564*4882a593Smuzhiyun 			   "RSS is disabled, hash cannot be set\n");
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	netdev_info(nic->netdev, "Set RSS flow type = %d, data = %lld\n",
567*4882a593Smuzhiyun 		    info->flow_type, info->data);
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	if (!(info->data & RXH_IP_SRC) || !(info->data & RXH_IP_DST))
570*4882a593Smuzhiyun 		return -EINVAL;
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 	switch (info->flow_type) {
573*4882a593Smuzhiyun 	case TCP_V4_FLOW:
574*4882a593Smuzhiyun 	case TCP_V6_FLOW:
575*4882a593Smuzhiyun 		switch (info->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
576*4882a593Smuzhiyun 		case 0:
577*4882a593Smuzhiyun 			rss_cfg &= ~(1ULL << RSS_HASH_TCP);
578*4882a593Smuzhiyun 			break;
579*4882a593Smuzhiyun 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
580*4882a593Smuzhiyun 			rss_cfg |= (1ULL << RSS_HASH_TCP);
581*4882a593Smuzhiyun 			break;
582*4882a593Smuzhiyun 		default:
583*4882a593Smuzhiyun 			return -EINVAL;
584*4882a593Smuzhiyun 		}
585*4882a593Smuzhiyun 		break;
586*4882a593Smuzhiyun 	case UDP_V4_FLOW:
587*4882a593Smuzhiyun 	case UDP_V6_FLOW:
588*4882a593Smuzhiyun 		switch (info->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
589*4882a593Smuzhiyun 		case 0:
590*4882a593Smuzhiyun 			rss_cfg &= ~(1ULL << RSS_HASH_UDP);
591*4882a593Smuzhiyun 			break;
592*4882a593Smuzhiyun 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
593*4882a593Smuzhiyun 			rss_cfg |= (1ULL << RSS_HASH_UDP);
594*4882a593Smuzhiyun 			break;
595*4882a593Smuzhiyun 		default:
596*4882a593Smuzhiyun 			return -EINVAL;
597*4882a593Smuzhiyun 		}
598*4882a593Smuzhiyun 		break;
599*4882a593Smuzhiyun 	case SCTP_V4_FLOW:
600*4882a593Smuzhiyun 	case SCTP_V6_FLOW:
601*4882a593Smuzhiyun 		switch (info->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
602*4882a593Smuzhiyun 		case 0:
603*4882a593Smuzhiyun 			rss_cfg &= ~(1ULL << RSS_HASH_L4ETC);
604*4882a593Smuzhiyun 			break;
605*4882a593Smuzhiyun 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
606*4882a593Smuzhiyun 			rss_cfg |= (1ULL << RSS_HASH_L4ETC);
607*4882a593Smuzhiyun 			break;
608*4882a593Smuzhiyun 		default:
609*4882a593Smuzhiyun 			return -EINVAL;
610*4882a593Smuzhiyun 		}
611*4882a593Smuzhiyun 		break;
612*4882a593Smuzhiyun 	case IPV4_FLOW:
613*4882a593Smuzhiyun 	case IPV6_FLOW:
614*4882a593Smuzhiyun 		rss_cfg = RSS_HASH_IP;
615*4882a593Smuzhiyun 		break;
616*4882a593Smuzhiyun 	default:
617*4882a593Smuzhiyun 		return -EINVAL;
618*4882a593Smuzhiyun 	}
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	nicvf_reg_write(nic, NIC_VNIC_RSS_CFG, rss_cfg);
621*4882a593Smuzhiyun 	return 0;
622*4882a593Smuzhiyun }
623*4882a593Smuzhiyun 
nicvf_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info)624*4882a593Smuzhiyun static int nicvf_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
625*4882a593Smuzhiyun {
626*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(dev);
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	switch (info->cmd) {
629*4882a593Smuzhiyun 	case ETHTOOL_SRXFH:
630*4882a593Smuzhiyun 		return nicvf_set_rss_hash_opts(nic, info);
631*4882a593Smuzhiyun 	default:
632*4882a593Smuzhiyun 		break;
633*4882a593Smuzhiyun 	}
634*4882a593Smuzhiyun 	return -EOPNOTSUPP;
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun 
nicvf_get_rxfh_key_size(struct net_device * netdev)637*4882a593Smuzhiyun static u32 nicvf_get_rxfh_key_size(struct net_device *netdev)
638*4882a593Smuzhiyun {
639*4882a593Smuzhiyun 	return RSS_HASH_KEY_SIZE * sizeof(u64);
640*4882a593Smuzhiyun }
641*4882a593Smuzhiyun 
nicvf_get_rxfh_indir_size(struct net_device * dev)642*4882a593Smuzhiyun static u32 nicvf_get_rxfh_indir_size(struct net_device *dev)
643*4882a593Smuzhiyun {
644*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(dev);
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 	return nic->rss_info.rss_size;
647*4882a593Smuzhiyun }
648*4882a593Smuzhiyun 
nicvf_get_rxfh(struct net_device * dev,u32 * indir,u8 * hkey,u8 * hfunc)649*4882a593Smuzhiyun static int nicvf_get_rxfh(struct net_device *dev, u32 *indir, u8 *hkey,
650*4882a593Smuzhiyun 			  u8 *hfunc)
651*4882a593Smuzhiyun {
652*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(dev);
653*4882a593Smuzhiyun 	struct nicvf_rss_info *rss = &nic->rss_info;
654*4882a593Smuzhiyun 	int idx;
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	if (indir) {
657*4882a593Smuzhiyun 		for (idx = 0; idx < rss->rss_size; idx++)
658*4882a593Smuzhiyun 			indir[idx] = rss->ind_tbl[idx];
659*4882a593Smuzhiyun 	}
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	if (hkey)
662*4882a593Smuzhiyun 		memcpy(hkey, rss->key, RSS_HASH_KEY_SIZE * sizeof(u64));
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	if (hfunc)
665*4882a593Smuzhiyun 		*hfunc = ETH_RSS_HASH_TOP;
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 	return 0;
668*4882a593Smuzhiyun }
669*4882a593Smuzhiyun 
nicvf_set_rxfh(struct net_device * dev,const u32 * indir,const u8 * hkey,const u8 hfunc)670*4882a593Smuzhiyun static int nicvf_set_rxfh(struct net_device *dev, const u32 *indir,
671*4882a593Smuzhiyun 			  const u8 *hkey, const u8 hfunc)
672*4882a593Smuzhiyun {
673*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(dev);
674*4882a593Smuzhiyun 	struct nicvf_rss_info *rss = &nic->rss_info;
675*4882a593Smuzhiyun 	int idx;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
678*4882a593Smuzhiyun 		return -EOPNOTSUPP;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	if (!rss->enable) {
681*4882a593Smuzhiyun 		netdev_err(nic->netdev,
682*4882a593Smuzhiyun 			   "RSS is disabled, cannot change settings\n");
683*4882a593Smuzhiyun 		return -EIO;
684*4882a593Smuzhiyun 	}
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 	if (indir) {
687*4882a593Smuzhiyun 		for (idx = 0; idx < rss->rss_size; idx++)
688*4882a593Smuzhiyun 			rss->ind_tbl[idx] = indir[idx];
689*4882a593Smuzhiyun 	}
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	if (hkey) {
692*4882a593Smuzhiyun 		memcpy(rss->key, hkey, RSS_HASH_KEY_SIZE * sizeof(u64));
693*4882a593Smuzhiyun 		nicvf_set_rss_key(nic);
694*4882a593Smuzhiyun 	}
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	nicvf_config_rss(nic);
697*4882a593Smuzhiyun 	return 0;
698*4882a593Smuzhiyun }
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun /* Get no of queues device supports and current queue count */
nicvf_get_channels(struct net_device * dev,struct ethtool_channels * channel)701*4882a593Smuzhiyun static void nicvf_get_channels(struct net_device *dev,
702*4882a593Smuzhiyun 			       struct ethtool_channels *channel)
703*4882a593Smuzhiyun {
704*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(dev);
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	memset(channel, 0, sizeof(*channel));
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun 	channel->max_rx = nic->max_queues;
709*4882a593Smuzhiyun 	channel->max_tx = nic->max_queues;
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	channel->rx_count = nic->rx_queues;
712*4882a593Smuzhiyun 	channel->tx_count = nic->tx_queues;
713*4882a593Smuzhiyun }
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun /* Set no of Tx, Rx queues to be used */
nicvf_set_channels(struct net_device * dev,struct ethtool_channels * channel)716*4882a593Smuzhiyun static int nicvf_set_channels(struct net_device *dev,
717*4882a593Smuzhiyun 			      struct ethtool_channels *channel)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(dev);
720*4882a593Smuzhiyun 	int err = 0;
721*4882a593Smuzhiyun 	bool if_up = netif_running(dev);
722*4882a593Smuzhiyun 	u8 cqcount, txq_count;
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 	if (!channel->rx_count || !channel->tx_count)
725*4882a593Smuzhiyun 		return -EINVAL;
726*4882a593Smuzhiyun 	if (channel->rx_count > nic->max_queues)
727*4882a593Smuzhiyun 		return -EINVAL;
728*4882a593Smuzhiyun 	if (channel->tx_count > nic->max_queues)
729*4882a593Smuzhiyun 		return -EINVAL;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	if (nic->xdp_prog &&
732*4882a593Smuzhiyun 	    ((channel->tx_count + channel->rx_count) > nic->max_queues)) {
733*4882a593Smuzhiyun 		netdev_err(nic->netdev,
734*4882a593Smuzhiyun 			   "XDP mode, RXQs + TXQs > Max %d\n",
735*4882a593Smuzhiyun 			   nic->max_queues);
736*4882a593Smuzhiyun 		return -EINVAL;
737*4882a593Smuzhiyun 	}
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	if (if_up)
740*4882a593Smuzhiyun 		nicvf_stop(dev);
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 	nic->rx_queues = channel->rx_count;
743*4882a593Smuzhiyun 	nic->tx_queues = channel->tx_count;
744*4882a593Smuzhiyun 	if (!nic->xdp_prog)
745*4882a593Smuzhiyun 		nic->xdp_tx_queues = 0;
746*4882a593Smuzhiyun 	else
747*4882a593Smuzhiyun 		nic->xdp_tx_queues = channel->rx_count;
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 	txq_count = nic->xdp_tx_queues + nic->tx_queues;
750*4882a593Smuzhiyun 	cqcount = max(nic->rx_queues, txq_count);
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	if (cqcount > MAX_CMP_QUEUES_PER_QS) {
753*4882a593Smuzhiyun 		nic->sqs_count = roundup(cqcount, MAX_CMP_QUEUES_PER_QS);
754*4882a593Smuzhiyun 		nic->sqs_count = (nic->sqs_count / MAX_CMP_QUEUES_PER_QS) - 1;
755*4882a593Smuzhiyun 	} else {
756*4882a593Smuzhiyun 		nic->sqs_count = 0;
757*4882a593Smuzhiyun 	}
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun 	nic->qs->rq_cnt = min_t(u8, nic->rx_queues, MAX_RCV_QUEUES_PER_QS);
760*4882a593Smuzhiyun 	nic->qs->sq_cnt = min_t(u8, txq_count, MAX_SND_QUEUES_PER_QS);
761*4882a593Smuzhiyun 	nic->qs->cq_cnt = max(nic->qs->rq_cnt, nic->qs->sq_cnt);
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 	err = nicvf_set_real_num_queues(dev, nic->tx_queues, nic->rx_queues);
764*4882a593Smuzhiyun 	if (err)
765*4882a593Smuzhiyun 		return err;
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	if (if_up)
768*4882a593Smuzhiyun 		nicvf_open(dev);
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	netdev_info(dev, "Setting num Tx rings to %d, Rx rings to %d success\n",
771*4882a593Smuzhiyun 		    nic->tx_queues, nic->rx_queues);
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun 	return err;
774*4882a593Smuzhiyun }
775*4882a593Smuzhiyun 
nicvf_get_pauseparam(struct net_device * dev,struct ethtool_pauseparam * pause)776*4882a593Smuzhiyun static void nicvf_get_pauseparam(struct net_device *dev,
777*4882a593Smuzhiyun 				 struct ethtool_pauseparam *pause)
778*4882a593Smuzhiyun {
779*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(dev);
780*4882a593Smuzhiyun 	union nic_mbx mbx = {};
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	/* Supported only for 10G/40G interfaces */
783*4882a593Smuzhiyun 	if ((nic->mac_type == BGX_MODE_SGMII) ||
784*4882a593Smuzhiyun 	    (nic->mac_type == BGX_MODE_QSGMII) ||
785*4882a593Smuzhiyun 	    (nic->mac_type == BGX_MODE_RGMII))
786*4882a593Smuzhiyun 		return;
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	mbx.pfc.msg = NIC_MBOX_MSG_PFC;
789*4882a593Smuzhiyun 	mbx.pfc.get = 1;
790*4882a593Smuzhiyun 	if (!nicvf_send_msg_to_pf(nic, &mbx)) {
791*4882a593Smuzhiyun 		pause->autoneg = nic->pfc.autoneg;
792*4882a593Smuzhiyun 		pause->rx_pause = nic->pfc.fc_rx;
793*4882a593Smuzhiyun 		pause->tx_pause = nic->pfc.fc_tx;
794*4882a593Smuzhiyun 	}
795*4882a593Smuzhiyun }
796*4882a593Smuzhiyun 
nicvf_set_pauseparam(struct net_device * dev,struct ethtool_pauseparam * pause)797*4882a593Smuzhiyun static int nicvf_set_pauseparam(struct net_device *dev,
798*4882a593Smuzhiyun 				struct ethtool_pauseparam *pause)
799*4882a593Smuzhiyun {
800*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(dev);
801*4882a593Smuzhiyun 	union nic_mbx mbx = {};
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	/* Supported only for 10G/40G interfaces */
804*4882a593Smuzhiyun 	if ((nic->mac_type == BGX_MODE_SGMII) ||
805*4882a593Smuzhiyun 	    (nic->mac_type == BGX_MODE_QSGMII) ||
806*4882a593Smuzhiyun 	    (nic->mac_type == BGX_MODE_RGMII))
807*4882a593Smuzhiyun 		return -EOPNOTSUPP;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	if (pause->autoneg)
810*4882a593Smuzhiyun 		return -EOPNOTSUPP;
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	mbx.pfc.msg = NIC_MBOX_MSG_PFC;
813*4882a593Smuzhiyun 	mbx.pfc.get = 0;
814*4882a593Smuzhiyun 	mbx.pfc.fc_rx = pause->rx_pause;
815*4882a593Smuzhiyun 	mbx.pfc.fc_tx = pause->tx_pause;
816*4882a593Smuzhiyun 	if (nicvf_send_msg_to_pf(nic, &mbx))
817*4882a593Smuzhiyun 		return -EAGAIN;
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	nic->pfc.fc_rx = pause->rx_pause;
820*4882a593Smuzhiyun 	nic->pfc.fc_tx = pause->tx_pause;
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	return 0;
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun 
nicvf_get_ts_info(struct net_device * netdev,struct ethtool_ts_info * info)825*4882a593Smuzhiyun static int nicvf_get_ts_info(struct net_device *netdev,
826*4882a593Smuzhiyun 			     struct ethtool_ts_info *info)
827*4882a593Smuzhiyun {
828*4882a593Smuzhiyun 	struct nicvf *nic = netdev_priv(netdev);
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	if (!nic->ptp_clock)
831*4882a593Smuzhiyun 		return ethtool_op_get_ts_info(netdev, info);
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
834*4882a593Smuzhiyun 				SOF_TIMESTAMPING_RX_SOFTWARE |
835*4882a593Smuzhiyun 				SOF_TIMESTAMPING_SOFTWARE |
836*4882a593Smuzhiyun 				SOF_TIMESTAMPING_TX_HARDWARE |
837*4882a593Smuzhiyun 				SOF_TIMESTAMPING_RX_HARDWARE |
838*4882a593Smuzhiyun 				SOF_TIMESTAMPING_RAW_HARDWARE;
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 	info->phc_index = cavium_ptp_clock_index(nic->ptp_clock);
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
845*4882a593Smuzhiyun 			   (1 << HWTSTAMP_FILTER_ALL);
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	return 0;
848*4882a593Smuzhiyun }
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun static const struct ethtool_ops nicvf_ethtool_ops = {
851*4882a593Smuzhiyun 	.get_link		= nicvf_get_link,
852*4882a593Smuzhiyun 	.get_drvinfo		= nicvf_get_drvinfo,
853*4882a593Smuzhiyun 	.get_msglevel		= nicvf_get_msglevel,
854*4882a593Smuzhiyun 	.set_msglevel		= nicvf_set_msglevel,
855*4882a593Smuzhiyun 	.get_strings		= nicvf_get_strings,
856*4882a593Smuzhiyun 	.get_sset_count		= nicvf_get_sset_count,
857*4882a593Smuzhiyun 	.get_ethtool_stats	= nicvf_get_ethtool_stats,
858*4882a593Smuzhiyun 	.get_regs_len		= nicvf_get_regs_len,
859*4882a593Smuzhiyun 	.get_regs		= nicvf_get_regs,
860*4882a593Smuzhiyun 	.get_coalesce		= nicvf_get_coalesce,
861*4882a593Smuzhiyun 	.get_ringparam		= nicvf_get_ringparam,
862*4882a593Smuzhiyun 	.set_ringparam		= nicvf_set_ringparam,
863*4882a593Smuzhiyun 	.get_rxnfc		= nicvf_get_rxnfc,
864*4882a593Smuzhiyun 	.set_rxnfc		= nicvf_set_rxnfc,
865*4882a593Smuzhiyun 	.get_rxfh_key_size	= nicvf_get_rxfh_key_size,
866*4882a593Smuzhiyun 	.get_rxfh_indir_size	= nicvf_get_rxfh_indir_size,
867*4882a593Smuzhiyun 	.get_rxfh		= nicvf_get_rxfh,
868*4882a593Smuzhiyun 	.set_rxfh		= nicvf_set_rxfh,
869*4882a593Smuzhiyun 	.get_channels		= nicvf_get_channels,
870*4882a593Smuzhiyun 	.set_channels		= nicvf_set_channels,
871*4882a593Smuzhiyun 	.get_pauseparam         = nicvf_get_pauseparam,
872*4882a593Smuzhiyun 	.set_pauseparam         = nicvf_set_pauseparam,
873*4882a593Smuzhiyun 	.get_ts_info		= nicvf_get_ts_info,
874*4882a593Smuzhiyun 	.get_link_ksettings	= nicvf_get_link_ksettings,
875*4882a593Smuzhiyun };
876*4882a593Smuzhiyun 
nicvf_set_ethtool_ops(struct net_device * netdev)877*4882a593Smuzhiyun void nicvf_set_ethtool_ops(struct net_device *netdev)
878*4882a593Smuzhiyun {
879*4882a593Smuzhiyun 	netdev->ethtool_ops = &nicvf_ethtool_ops;
880*4882a593Smuzhiyun }
881