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