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