1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Ethernet driver
3  *
4  * Copyright (C) 2020 Marvell.
5  *
6  */
7 
8 #include <linux/pci.h>
9 #include <linux/ethtool.h>
10 #include <linux/stddef.h>
11 #include <linux/etherdevice.h>
12 #include <linux/log2.h>
13 #include <linux/net_tstamp.h>
14 #include <linux/linkmode.h>
15 
16 #include "otx2_common.h"
17 #include "otx2_ptp.h"
18 
19 #define DRV_NAME	"rvu-nicpf"
20 #define DRV_VF_NAME	"rvu-nicvf"
21 
22 struct otx2_stat {
23 	char name[ETH_GSTRING_LEN];
24 	unsigned int index;
25 };
26 
27 /* HW device stats */
28 #define OTX2_DEV_STAT(stat) { \
29 	.name = #stat, \
30 	.index = offsetof(struct otx2_dev_stats, stat) / sizeof(u64), \
31 }
32 
33 enum link_mode {
34 	OTX2_MODE_SUPPORTED,
35 	OTX2_MODE_ADVERTISED
36 };
37 
38 static const struct otx2_stat otx2_dev_stats[] = {
39 	OTX2_DEV_STAT(rx_ucast_frames),
40 	OTX2_DEV_STAT(rx_bcast_frames),
41 	OTX2_DEV_STAT(rx_mcast_frames),
42 
43 	OTX2_DEV_STAT(tx_ucast_frames),
44 	OTX2_DEV_STAT(tx_bcast_frames),
45 	OTX2_DEV_STAT(tx_mcast_frames),
46 };
47 
48 /* Driver level stats */
49 #define OTX2_DRV_STAT(stat) { \
50 	.name = #stat, \
51 	.index = offsetof(struct otx2_drv_stats, stat) / sizeof(atomic_t), \
52 }
53 
54 static const struct otx2_stat otx2_drv_stats[] = {
55 	OTX2_DRV_STAT(rx_fcs_errs),
56 	OTX2_DRV_STAT(rx_oversize_errs),
57 	OTX2_DRV_STAT(rx_undersize_errs),
58 	OTX2_DRV_STAT(rx_csum_errs),
59 	OTX2_DRV_STAT(rx_len_errs),
60 	OTX2_DRV_STAT(rx_other_errs),
61 };
62 
63 static const struct otx2_stat otx2_queue_stats[] = {
64 	{ "bytes", 0 },
65 	{ "frames", 1 },
66 };
67 
68 static const unsigned int otx2_n_dev_stats = ARRAY_SIZE(otx2_dev_stats);
69 static const unsigned int otx2_n_drv_stats = ARRAY_SIZE(otx2_drv_stats);
70 static const unsigned int otx2_n_queue_stats = ARRAY_SIZE(otx2_queue_stats);
71 
72 static struct cgx_fw_data *otx2_get_fwdata(struct otx2_nic *pfvf);
73 
74 static void otx2_get_drvinfo(struct net_device *netdev,
75 			     struct ethtool_drvinfo *info)
76 {
77 	struct otx2_nic *pfvf = netdev_priv(netdev);
78 
79 	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
80 	strscpy(info->bus_info, pci_name(pfvf->pdev), sizeof(info->bus_info));
81 }
82 
83 static void otx2_get_qset_strings(struct otx2_nic *pfvf, u8 **data, int qset)
84 {
85 	int start_qidx = qset * pfvf->hw.rx_queues;
86 	int qidx, stats;
87 
88 	for (qidx = 0; qidx < pfvf->hw.rx_queues; qidx++) {
89 		for (stats = 0; stats < otx2_n_queue_stats; stats++) {
90 			sprintf(*data, "rxq%d: %s", qidx + start_qidx,
91 				otx2_queue_stats[stats].name);
92 			*data += ETH_GSTRING_LEN;
93 		}
94 	}
95 
96 	for (qidx = 0; qidx < otx2_get_total_tx_queues(pfvf); qidx++) {
97 		for (stats = 0; stats < otx2_n_queue_stats; stats++) {
98 			if (qidx >= pfvf->hw.non_qos_queues)
99 				sprintf(*data, "txq_qos%d: %s",
100 					qidx + start_qidx - pfvf->hw.non_qos_queues,
101 					otx2_queue_stats[stats].name);
102 			else
103 				sprintf(*data, "txq%d: %s", qidx + start_qidx,
104 					otx2_queue_stats[stats].name);
105 			*data += ETH_GSTRING_LEN;
106 		}
107 	}
108 }
109 
110 static void otx2_get_strings(struct net_device *netdev, u32 sset, u8 *data)
111 {
112 	struct otx2_nic *pfvf = netdev_priv(netdev);
113 	int stats;
114 
115 	if (sset != ETH_SS_STATS)
116 		return;
117 
118 	for (stats = 0; stats < otx2_n_dev_stats; stats++) {
119 		memcpy(data, otx2_dev_stats[stats].name, ETH_GSTRING_LEN);
120 		data += ETH_GSTRING_LEN;
121 	}
122 
123 	for (stats = 0; stats < otx2_n_drv_stats; stats++) {
124 		memcpy(data, otx2_drv_stats[stats].name, ETH_GSTRING_LEN);
125 		data += ETH_GSTRING_LEN;
126 	}
127 
128 	otx2_get_qset_strings(pfvf, &data, 0);
129 
130 	if (!test_bit(CN10K_RPM, &pfvf->hw.cap_flag)) {
131 		for (stats = 0; stats < CGX_RX_STATS_COUNT; stats++) {
132 			sprintf(data, "cgx_rxstat%d: ", stats);
133 			data += ETH_GSTRING_LEN;
134 		}
135 
136 		for (stats = 0; stats < CGX_TX_STATS_COUNT; stats++) {
137 			sprintf(data, "cgx_txstat%d: ", stats);
138 			data += ETH_GSTRING_LEN;
139 		}
140 	}
141 
142 	strcpy(data, "reset_count");
143 	data += ETH_GSTRING_LEN;
144 	sprintf(data, "Fec Corrected Errors: ");
145 	data += ETH_GSTRING_LEN;
146 	sprintf(data, "Fec Uncorrected Errors: ");
147 	data += ETH_GSTRING_LEN;
148 }
149 
150 static void otx2_get_qset_stats(struct otx2_nic *pfvf,
151 				struct ethtool_stats *stats, u64 **data)
152 {
153 	int stat, qidx;
154 
155 	if (!pfvf)
156 		return;
157 	for (qidx = 0; qidx < pfvf->hw.rx_queues; qidx++) {
158 		if (!otx2_update_rq_stats(pfvf, qidx)) {
159 			for (stat = 0; stat < otx2_n_queue_stats; stat++)
160 				*((*data)++) = 0;
161 			continue;
162 		}
163 		for (stat = 0; stat < otx2_n_queue_stats; stat++)
164 			*((*data)++) = ((u64 *)&pfvf->qset.rq[qidx].stats)
165 				[otx2_queue_stats[stat].index];
166 	}
167 
168 	for (qidx = 0; qidx < otx2_get_total_tx_queues(pfvf); qidx++) {
169 		if (!otx2_update_sq_stats(pfvf, qidx)) {
170 			for (stat = 0; stat < otx2_n_queue_stats; stat++)
171 				*((*data)++) = 0;
172 			continue;
173 		}
174 		for (stat = 0; stat < otx2_n_queue_stats; stat++)
175 			*((*data)++) = ((u64 *)&pfvf->qset.sq[qidx].stats)
176 				[otx2_queue_stats[stat].index];
177 	}
178 }
179 
180 static int otx2_get_phy_fec_stats(struct otx2_nic *pfvf)
181 {
182 	struct msg_req *req;
183 	int rc = -ENOMEM;
184 
185 	mutex_lock(&pfvf->mbox.lock);
186 	req = otx2_mbox_alloc_msg_cgx_get_phy_fec_stats(&pfvf->mbox);
187 	if (!req)
188 		goto end;
189 
190 	if (!otx2_sync_mbox_msg(&pfvf->mbox))
191 		rc = 0;
192 end:
193 	mutex_unlock(&pfvf->mbox.lock);
194 	return rc;
195 }
196 
197 /* Get device and per queue statistics */
198 static void otx2_get_ethtool_stats(struct net_device *netdev,
199 				   struct ethtool_stats *stats, u64 *data)
200 {
201 	struct otx2_nic *pfvf = netdev_priv(netdev);
202 	u64 fec_corr_blks, fec_uncorr_blks;
203 	struct cgx_fw_data *rsp;
204 	int stat;
205 
206 	otx2_get_dev_stats(pfvf);
207 	for (stat = 0; stat < otx2_n_dev_stats; stat++)
208 		*(data++) = ((u64 *)&pfvf->hw.dev_stats)
209 				[otx2_dev_stats[stat].index];
210 
211 	for (stat = 0; stat < otx2_n_drv_stats; stat++)
212 		*(data++) = atomic_read(&((atomic_t *)&pfvf->hw.drv_stats)
213 						[otx2_drv_stats[stat].index]);
214 
215 	otx2_get_qset_stats(pfvf, stats, &data);
216 
217 	if (!test_bit(CN10K_RPM, &pfvf->hw.cap_flag)) {
218 		otx2_update_lmac_stats(pfvf);
219 		for (stat = 0; stat < CGX_RX_STATS_COUNT; stat++)
220 			*(data++) = pfvf->hw.cgx_rx_stats[stat];
221 		for (stat = 0; stat < CGX_TX_STATS_COUNT; stat++)
222 			*(data++) = pfvf->hw.cgx_tx_stats[stat];
223 	}
224 
225 	*(data++) = pfvf->reset_count;
226 
227 	fec_corr_blks = pfvf->hw.cgx_fec_corr_blks;
228 	fec_uncorr_blks = pfvf->hw.cgx_fec_uncorr_blks;
229 
230 	rsp = otx2_get_fwdata(pfvf);
231 	if (!IS_ERR(rsp) && rsp->fwdata.phy.misc.has_fec_stats &&
232 	    !otx2_get_phy_fec_stats(pfvf)) {
233 		/* Fetch fwdata again because it's been recently populated with
234 		 * latest PHY FEC stats.
235 		 */
236 		rsp = otx2_get_fwdata(pfvf);
237 		if (!IS_ERR(rsp)) {
238 			struct fec_stats_s *p = &rsp->fwdata.phy.fec_stats;
239 
240 			if (pfvf->linfo.fec == OTX2_FEC_BASER) {
241 				fec_corr_blks   = p->brfec_corr_blks;
242 				fec_uncorr_blks = p->brfec_uncorr_blks;
243 			} else {
244 				fec_corr_blks   = p->rsfec_corr_cws;
245 				fec_uncorr_blks = p->rsfec_uncorr_cws;
246 			}
247 		}
248 	}
249 
250 	*(data++) = fec_corr_blks;
251 	*(data++) = fec_uncorr_blks;
252 }
253 
254 static int otx2_get_sset_count(struct net_device *netdev, int sset)
255 {
256 	struct otx2_nic *pfvf = netdev_priv(netdev);
257 	int qstats_count, mac_stats = 0;
258 
259 	if (sset != ETH_SS_STATS)
260 		return -EINVAL;
261 
262 	qstats_count = otx2_n_queue_stats *
263 		       (pfvf->hw.rx_queues + otx2_get_total_tx_queues(pfvf));
264 	if (!test_bit(CN10K_RPM, &pfvf->hw.cap_flag))
265 		mac_stats = CGX_RX_STATS_COUNT + CGX_TX_STATS_COUNT;
266 	otx2_update_lmac_fec_stats(pfvf);
267 
268 	return otx2_n_dev_stats + otx2_n_drv_stats + qstats_count +
269 	       mac_stats + OTX2_FEC_STATS_CNT + 1;
270 }
271 
272 /* Get no of queues device supports and current queue count */
273 static void otx2_get_channels(struct net_device *dev,
274 			      struct ethtool_channels *channel)
275 {
276 	struct otx2_nic *pfvf = netdev_priv(dev);
277 
278 	channel->max_rx = pfvf->hw.max_queues;
279 	channel->max_tx = pfvf->hw.max_queues;
280 
281 	channel->rx_count = pfvf->hw.rx_queues;
282 	channel->tx_count = pfvf->hw.tx_queues;
283 }
284 
285 /* Set no of Tx, Rx queues to be used */
286 static int otx2_set_channels(struct net_device *dev,
287 			     struct ethtool_channels *channel)
288 {
289 	struct otx2_nic *pfvf = netdev_priv(dev);
290 	bool if_up = netif_running(dev);
291 	int err, qos_txqs;
292 
293 	if (!channel->rx_count || !channel->tx_count)
294 		return -EINVAL;
295 
296 	if (bitmap_weight(&pfvf->rq_bmap, pfvf->hw.rx_queues) > 1) {
297 		netdev_err(dev,
298 			   "Receive queues are in use by TC police action\n");
299 		return -EINVAL;
300 	}
301 
302 	if (if_up)
303 		dev->netdev_ops->ndo_stop(dev);
304 
305 	qos_txqs = bitmap_weight(pfvf->qos.qos_sq_bmap,
306 				 OTX2_QOS_MAX_LEAF_NODES);
307 
308 	err = otx2_set_real_num_queues(dev, channel->tx_count + qos_txqs,
309 				       channel->rx_count);
310 	if (err)
311 		return err;
312 
313 	pfvf->hw.rx_queues = channel->rx_count;
314 	pfvf->hw.tx_queues = channel->tx_count;
315 	if (pfvf->xdp_prog)
316 		pfvf->hw.xdp_queues = channel->rx_count;
317 	pfvf->hw.non_qos_queues =  pfvf->hw.tx_queues + pfvf->hw.xdp_queues;
318 
319 	if (if_up)
320 		err = dev->netdev_ops->ndo_open(dev);
321 
322 	netdev_info(dev, "Setting num Tx rings to %d, Rx rings to %d success\n",
323 		    pfvf->hw.tx_queues, pfvf->hw.rx_queues);
324 
325 	return err;
326 }
327 
328 static void otx2_get_pauseparam(struct net_device *netdev,
329 				struct ethtool_pauseparam *pause)
330 {
331 	struct otx2_nic *pfvf = netdev_priv(netdev);
332 	struct cgx_pause_frm_cfg *req, *rsp;
333 
334 	if (is_otx2_lbkvf(pfvf->pdev))
335 		return;
336 
337 	req = otx2_mbox_alloc_msg_cgx_cfg_pause_frm(&pfvf->mbox);
338 	if (!req)
339 		return;
340 
341 	if (!otx2_sync_mbox_msg(&pfvf->mbox)) {
342 		rsp = (struct cgx_pause_frm_cfg *)
343 		       otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
344 		pause->rx_pause = rsp->rx_pause;
345 		pause->tx_pause = rsp->tx_pause;
346 	}
347 }
348 
349 static int otx2_set_pauseparam(struct net_device *netdev,
350 			       struct ethtool_pauseparam *pause)
351 {
352 	struct otx2_nic *pfvf = netdev_priv(netdev);
353 
354 	if (pause->autoneg)
355 		return -EOPNOTSUPP;
356 
357 	if (is_otx2_lbkvf(pfvf->pdev))
358 		return -EOPNOTSUPP;
359 
360 	if (pause->rx_pause)
361 		pfvf->flags |= OTX2_FLAG_RX_PAUSE_ENABLED;
362 	else
363 		pfvf->flags &= ~OTX2_FLAG_RX_PAUSE_ENABLED;
364 
365 	if (pause->tx_pause)
366 		pfvf->flags |= OTX2_FLAG_TX_PAUSE_ENABLED;
367 	else
368 		pfvf->flags &= ~OTX2_FLAG_TX_PAUSE_ENABLED;
369 
370 	return otx2_config_pause_frm(pfvf);
371 }
372 
373 static void otx2_get_ringparam(struct net_device *netdev,
374 			       struct ethtool_ringparam *ring,
375 			       struct kernel_ethtool_ringparam *kernel_ring,
376 			       struct netlink_ext_ack *extack)
377 {
378 	struct otx2_nic *pfvf = netdev_priv(netdev);
379 	struct otx2_qset *qs = &pfvf->qset;
380 
381 	ring->rx_max_pending = Q_COUNT(Q_SIZE_MAX);
382 	ring->rx_pending = qs->rqe_cnt ? qs->rqe_cnt : Q_COUNT(Q_SIZE_256);
383 	ring->tx_max_pending = Q_COUNT(Q_SIZE_MAX);
384 	ring->tx_pending = qs->sqe_cnt ? qs->sqe_cnt : Q_COUNT(Q_SIZE_4K);
385 	kernel_ring->rx_buf_len = pfvf->hw.rbuf_len;
386 	kernel_ring->cqe_size = pfvf->hw.xqe_size;
387 }
388 
389 static int otx2_set_ringparam(struct net_device *netdev,
390 			      struct ethtool_ringparam *ring,
391 			      struct kernel_ethtool_ringparam *kernel_ring,
392 			      struct netlink_ext_ack *extack)
393 {
394 	struct otx2_nic *pfvf = netdev_priv(netdev);
395 	u32 rx_buf_len = kernel_ring->rx_buf_len;
396 	u32 old_rx_buf_len = pfvf->hw.rbuf_len;
397 	u32 xqe_size = kernel_ring->cqe_size;
398 	bool if_up = netif_running(netdev);
399 	struct otx2_qset *qs = &pfvf->qset;
400 	u32 rx_count, tx_count;
401 
402 	if (ring->rx_mini_pending || ring->rx_jumbo_pending)
403 		return -EINVAL;
404 
405 	/* Hardware supports max size of 32k for a receive buffer
406 	 * and 1536 is typical ethernet frame size.
407 	 */
408 	if (rx_buf_len && (rx_buf_len < 1536 || rx_buf_len > 32768)) {
409 		netdev_err(netdev,
410 			   "Receive buffer range is 1536 - 32768");
411 		return -EINVAL;
412 	}
413 
414 	if (xqe_size != 128 && xqe_size != 512) {
415 		netdev_err(netdev,
416 			   "Completion event size must be 128 or 512");
417 		return -EINVAL;
418 	}
419 
420 	/* Permitted lengths are 16 64 256 1K 4K 16K 64K 256K 1M  */
421 	rx_count = ring->rx_pending;
422 	/* On some silicon variants a skid or reserved CQEs are
423 	 * needed to avoid CQ overflow.
424 	 */
425 	if (rx_count < pfvf->hw.rq_skid)
426 		rx_count =  pfvf->hw.rq_skid;
427 	rx_count = Q_COUNT(Q_SIZE(rx_count, 3));
428 
429 	/* Due pipelining impact minimum 2000 unused SQ CQE's
430 	 * need to be maintained to avoid CQ overflow, hence the
431 	 * minimum 4K size.
432 	 */
433 	tx_count = clamp_t(u32, ring->tx_pending,
434 			   Q_COUNT(Q_SIZE_4K), Q_COUNT(Q_SIZE_MAX));
435 	tx_count = Q_COUNT(Q_SIZE(tx_count, 3));
436 
437 	if (tx_count == qs->sqe_cnt && rx_count == qs->rqe_cnt &&
438 	    rx_buf_len == old_rx_buf_len && xqe_size == pfvf->hw.xqe_size)
439 		return 0;
440 
441 	if (if_up)
442 		netdev->netdev_ops->ndo_stop(netdev);
443 
444 	/* Assigned to the nearest possible exponent. */
445 	qs->sqe_cnt = tx_count;
446 	qs->rqe_cnt = rx_count;
447 
448 	pfvf->hw.rbuf_len = rx_buf_len;
449 	pfvf->hw.xqe_size = xqe_size;
450 
451 	if (if_up)
452 		return netdev->netdev_ops->ndo_open(netdev);
453 
454 	return 0;
455 }
456 
457 static int otx2_get_coalesce(struct net_device *netdev,
458 			     struct ethtool_coalesce *cmd,
459 			     struct kernel_ethtool_coalesce *kernel_coal,
460 			     struct netlink_ext_ack *extack)
461 {
462 	struct otx2_nic *pfvf = netdev_priv(netdev);
463 	struct otx2_hw *hw = &pfvf->hw;
464 
465 	cmd->rx_coalesce_usecs = hw->cq_time_wait;
466 	cmd->rx_max_coalesced_frames = hw->cq_ecount_wait;
467 	cmd->tx_coalesce_usecs = hw->cq_time_wait;
468 	cmd->tx_max_coalesced_frames = hw->cq_ecount_wait;
469 	if ((pfvf->flags & OTX2_FLAG_ADPTV_INT_COAL_ENABLED) ==
470 			OTX2_FLAG_ADPTV_INT_COAL_ENABLED) {
471 		cmd->use_adaptive_rx_coalesce = 1;
472 		cmd->use_adaptive_tx_coalesce = 1;
473 	} else {
474 		cmd->use_adaptive_rx_coalesce = 0;
475 		cmd->use_adaptive_tx_coalesce = 0;
476 	}
477 
478 	return 0;
479 }
480 
481 static int otx2_set_coalesce(struct net_device *netdev,
482 			     struct ethtool_coalesce *ec,
483 			     struct kernel_ethtool_coalesce *kernel_coal,
484 			     struct netlink_ext_ack *extack)
485 {
486 	struct otx2_nic *pfvf = netdev_priv(netdev);
487 	struct otx2_hw *hw = &pfvf->hw;
488 	u8 priv_coalesce_status;
489 	int qidx;
490 
491 	if (!ec->rx_max_coalesced_frames || !ec->tx_max_coalesced_frames)
492 		return 0;
493 
494 	if (ec->use_adaptive_rx_coalesce != ec->use_adaptive_tx_coalesce) {
495 		netdev_err(netdev,
496 			   "adaptive-rx should be same as adaptive-tx");
497 		return -EINVAL;
498 	}
499 
500 	/* Check and update coalesce status */
501 	if ((pfvf->flags & OTX2_FLAG_ADPTV_INT_COAL_ENABLED) ==
502 			OTX2_FLAG_ADPTV_INT_COAL_ENABLED) {
503 		priv_coalesce_status = 1;
504 		if (!ec->use_adaptive_rx_coalesce)
505 			pfvf->flags &= ~OTX2_FLAG_ADPTV_INT_COAL_ENABLED;
506 	} else {
507 		priv_coalesce_status = 0;
508 		if (ec->use_adaptive_rx_coalesce)
509 			pfvf->flags |= OTX2_FLAG_ADPTV_INT_COAL_ENABLED;
510 	}
511 
512 	/* 'cq_time_wait' is 8bit and is in multiple of 100ns,
513 	 * so clamp the user given value to the range of 1 to 25usec.
514 	 */
515 	ec->rx_coalesce_usecs = clamp_t(u32, ec->rx_coalesce_usecs,
516 					1, CQ_TIMER_THRESH_MAX);
517 	ec->tx_coalesce_usecs = clamp_t(u32, ec->tx_coalesce_usecs,
518 					1, CQ_TIMER_THRESH_MAX);
519 
520 	/* Rx and Tx are mapped to same CQ, check which one
521 	 * is changed, if both then choose the min.
522 	 */
523 	if (hw->cq_time_wait == ec->rx_coalesce_usecs)
524 		hw->cq_time_wait = ec->tx_coalesce_usecs;
525 	else if (hw->cq_time_wait == ec->tx_coalesce_usecs)
526 		hw->cq_time_wait = ec->rx_coalesce_usecs;
527 	else
528 		hw->cq_time_wait = min_t(u8, ec->rx_coalesce_usecs,
529 					 ec->tx_coalesce_usecs);
530 
531 	/* Max ecount_wait supported is 16bit,
532 	 * so clamp the user given value to the range of 1 to 64k.
533 	 */
534 	ec->rx_max_coalesced_frames = clamp_t(u32, ec->rx_max_coalesced_frames,
535 					      1, NAPI_POLL_WEIGHT);
536 	ec->tx_max_coalesced_frames = clamp_t(u32, ec->tx_max_coalesced_frames,
537 					      1, NAPI_POLL_WEIGHT);
538 
539 	/* Rx and Tx are mapped to same CQ, check which one
540 	 * is changed, if both then choose the min.
541 	 */
542 	if (hw->cq_ecount_wait == ec->rx_max_coalesced_frames)
543 		hw->cq_ecount_wait = ec->tx_max_coalesced_frames;
544 	else if (hw->cq_ecount_wait == ec->tx_max_coalesced_frames)
545 		hw->cq_ecount_wait = ec->rx_max_coalesced_frames;
546 	else
547 		hw->cq_ecount_wait = min_t(u16, ec->rx_max_coalesced_frames,
548 					   ec->tx_max_coalesced_frames);
549 
550 	/* Reset 'cq_time_wait' and 'cq_ecount_wait' to
551 	 * default values if coalesce status changed from
552 	 * 'on' to 'off'.
553 	 */
554 	if (priv_coalesce_status &&
555 	    ((pfvf->flags & OTX2_FLAG_ADPTV_INT_COAL_ENABLED) !=
556 	     OTX2_FLAG_ADPTV_INT_COAL_ENABLED)) {
557 		hw->cq_time_wait = CQ_TIMER_THRESH_DEFAULT;
558 		hw->cq_ecount_wait = CQ_CQE_THRESH_DEFAULT;
559 	}
560 
561 	if (netif_running(netdev)) {
562 		for (qidx = 0; qidx < pfvf->hw.cint_cnt; qidx++)
563 			otx2_config_irq_coalescing(pfvf, qidx);
564 	}
565 
566 	return 0;
567 }
568 
569 static int otx2_get_rss_hash_opts(struct otx2_nic *pfvf,
570 				  struct ethtool_rxnfc *nfc)
571 {
572 	struct otx2_rss_info *rss = &pfvf->hw.rss_info;
573 
574 	if (!(rss->flowkey_cfg &
575 	    (NIX_FLOW_KEY_TYPE_IPV4 | NIX_FLOW_KEY_TYPE_IPV6)))
576 		return 0;
577 
578 	/* Mimimum is IPv4 and IPv6, SIP/DIP */
579 	nfc->data = RXH_IP_SRC | RXH_IP_DST;
580 	if (rss->flowkey_cfg & NIX_FLOW_KEY_TYPE_VLAN)
581 		nfc->data |= RXH_VLAN;
582 
583 	switch (nfc->flow_type) {
584 	case TCP_V4_FLOW:
585 	case TCP_V6_FLOW:
586 		if (rss->flowkey_cfg & NIX_FLOW_KEY_TYPE_TCP)
587 			nfc->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
588 		break;
589 	case UDP_V4_FLOW:
590 	case UDP_V6_FLOW:
591 		if (rss->flowkey_cfg & NIX_FLOW_KEY_TYPE_UDP)
592 			nfc->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
593 		break;
594 	case SCTP_V4_FLOW:
595 	case SCTP_V6_FLOW:
596 		if (rss->flowkey_cfg & NIX_FLOW_KEY_TYPE_SCTP)
597 			nfc->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
598 		break;
599 	case AH_ESP_V4_FLOW:
600 	case AH_ESP_V6_FLOW:
601 		if (rss->flowkey_cfg & NIX_FLOW_KEY_TYPE_ESP)
602 			nfc->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
603 		break;
604 	case AH_V4_FLOW:
605 	case ESP_V4_FLOW:
606 	case IPV4_FLOW:
607 		break;
608 	case AH_V6_FLOW:
609 	case ESP_V6_FLOW:
610 	case IPV6_FLOW:
611 		break;
612 	default:
613 		return -EINVAL;
614 	}
615 
616 	return 0;
617 }
618 
619 static int otx2_set_rss_hash_opts(struct otx2_nic *pfvf,
620 				  struct ethtool_rxnfc *nfc)
621 {
622 	struct otx2_rss_info *rss = &pfvf->hw.rss_info;
623 	u32 rxh_l4 = RXH_L4_B_0_1 | RXH_L4_B_2_3;
624 	u32 rss_cfg = rss->flowkey_cfg;
625 
626 	if (!rss->enable) {
627 		netdev_err(pfvf->netdev,
628 			   "RSS is disabled, cannot change settings\n");
629 		return -EIO;
630 	}
631 
632 	/* Mimimum is IPv4 and IPv6, SIP/DIP */
633 	if (!(nfc->data & RXH_IP_SRC) || !(nfc->data & RXH_IP_DST))
634 		return -EINVAL;
635 
636 	if (nfc->data & RXH_VLAN)
637 		rss_cfg |=  NIX_FLOW_KEY_TYPE_VLAN;
638 	else
639 		rss_cfg &= ~NIX_FLOW_KEY_TYPE_VLAN;
640 
641 	switch (nfc->flow_type) {
642 	case TCP_V4_FLOW:
643 	case TCP_V6_FLOW:
644 		/* Different config for v4 and v6 is not supported.
645 		 * Both of them have to be either 4-tuple or 2-tuple.
646 		 */
647 		switch (nfc->data & rxh_l4) {
648 		case 0:
649 			rss_cfg &= ~NIX_FLOW_KEY_TYPE_TCP;
650 			break;
651 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
652 			rss_cfg |= NIX_FLOW_KEY_TYPE_TCP;
653 			break;
654 		default:
655 			return -EINVAL;
656 		}
657 		break;
658 	case UDP_V4_FLOW:
659 	case UDP_V6_FLOW:
660 		switch (nfc->data & rxh_l4) {
661 		case 0:
662 			rss_cfg &= ~NIX_FLOW_KEY_TYPE_UDP;
663 			break;
664 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
665 			rss_cfg |= NIX_FLOW_KEY_TYPE_UDP;
666 			break;
667 		default:
668 			return -EINVAL;
669 		}
670 		break;
671 	case SCTP_V4_FLOW:
672 	case SCTP_V6_FLOW:
673 		switch (nfc->data & rxh_l4) {
674 		case 0:
675 			rss_cfg &= ~NIX_FLOW_KEY_TYPE_SCTP;
676 			break;
677 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
678 			rss_cfg |= NIX_FLOW_KEY_TYPE_SCTP;
679 			break;
680 		default:
681 			return -EINVAL;
682 		}
683 		break;
684 	case AH_ESP_V4_FLOW:
685 	case AH_ESP_V6_FLOW:
686 		switch (nfc->data & rxh_l4) {
687 		case 0:
688 			rss_cfg &= ~(NIX_FLOW_KEY_TYPE_ESP |
689 				     NIX_FLOW_KEY_TYPE_AH);
690 			rss_cfg |= NIX_FLOW_KEY_TYPE_VLAN |
691 				   NIX_FLOW_KEY_TYPE_IPV4_PROTO;
692 			break;
693 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
694 			/* If VLAN hashing is also requested for ESP then do not
695 			 * allow because of hardware 40 bytes flow key limit.
696 			 */
697 			if (rss_cfg & NIX_FLOW_KEY_TYPE_VLAN) {
698 				netdev_err(pfvf->netdev,
699 					   "RSS hash of ESP or AH with VLAN is not supported\n");
700 				return -EOPNOTSUPP;
701 			}
702 
703 			rss_cfg |= NIX_FLOW_KEY_TYPE_ESP | NIX_FLOW_KEY_TYPE_AH;
704 			/* Disable IPv4 proto hashing since IPv6 SA+DA(32 bytes)
705 			 * and ESP SPI+sequence(8 bytes) uses hardware maximum
706 			 * limit of 40 byte flow key.
707 			 */
708 			rss_cfg &= ~NIX_FLOW_KEY_TYPE_IPV4_PROTO;
709 			break;
710 		default:
711 			return -EINVAL;
712 		}
713 		break;
714 	case IPV4_FLOW:
715 	case IPV6_FLOW:
716 		rss_cfg = NIX_FLOW_KEY_TYPE_IPV4 | NIX_FLOW_KEY_TYPE_IPV6;
717 		break;
718 	default:
719 		return -EINVAL;
720 	}
721 
722 	rss->flowkey_cfg = rss_cfg;
723 	otx2_set_flowkey_cfg(pfvf);
724 	return 0;
725 }
726 
727 static int otx2_get_rxnfc(struct net_device *dev,
728 			  struct ethtool_rxnfc *nfc, u32 *rules)
729 {
730 	bool ntuple = !!(dev->features & NETIF_F_NTUPLE);
731 	struct otx2_nic *pfvf = netdev_priv(dev);
732 	int ret = -EOPNOTSUPP;
733 
734 	switch (nfc->cmd) {
735 	case ETHTOOL_GRXRINGS:
736 		nfc->data = pfvf->hw.rx_queues;
737 		ret = 0;
738 		break;
739 	case ETHTOOL_GRXCLSRLCNT:
740 		if (netif_running(dev) && ntuple) {
741 			nfc->rule_cnt = pfvf->flow_cfg->nr_flows;
742 			ret = 0;
743 		}
744 		break;
745 	case ETHTOOL_GRXCLSRULE:
746 		if (netif_running(dev) && ntuple)
747 			ret = otx2_get_flow(pfvf, nfc,  nfc->fs.location);
748 		break;
749 	case ETHTOOL_GRXCLSRLALL:
750 		if (netif_running(dev) && ntuple)
751 			ret = otx2_get_all_flows(pfvf, nfc, rules);
752 		break;
753 	case ETHTOOL_GRXFH:
754 		return otx2_get_rss_hash_opts(pfvf, nfc);
755 	default:
756 		break;
757 	}
758 	return ret;
759 }
760 
761 static int otx2_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *nfc)
762 {
763 	bool ntuple = !!(dev->features & NETIF_F_NTUPLE);
764 	struct otx2_nic *pfvf = netdev_priv(dev);
765 	int ret = -EOPNOTSUPP;
766 
767 	switch (nfc->cmd) {
768 	case ETHTOOL_SRXFH:
769 		ret = otx2_set_rss_hash_opts(pfvf, nfc);
770 		break;
771 	case ETHTOOL_SRXCLSRLINS:
772 		if (netif_running(dev) && ntuple)
773 			ret = otx2_add_flow(pfvf, nfc);
774 		break;
775 	case ETHTOOL_SRXCLSRLDEL:
776 		if (netif_running(dev) && ntuple)
777 			ret = otx2_remove_flow(pfvf, nfc->fs.location);
778 		break;
779 	default:
780 		break;
781 	}
782 
783 	return ret;
784 }
785 
786 static u32 otx2_get_rxfh_key_size(struct net_device *netdev)
787 {
788 	struct otx2_nic *pfvf = netdev_priv(netdev);
789 	struct otx2_rss_info *rss;
790 
791 	rss = &pfvf->hw.rss_info;
792 
793 	return sizeof(rss->key);
794 }
795 
796 static u32 otx2_get_rxfh_indir_size(struct net_device *dev)
797 {
798 	return  MAX_RSS_INDIR_TBL_SIZE;
799 }
800 
801 static int otx2_rss_ctx_delete(struct otx2_nic *pfvf, int ctx_id)
802 {
803 	struct otx2_rss_info *rss = &pfvf->hw.rss_info;
804 
805 	otx2_rss_ctx_flow_del(pfvf, ctx_id);
806 	kfree(rss->rss_ctx[ctx_id]);
807 	rss->rss_ctx[ctx_id] = NULL;
808 
809 	return 0;
810 }
811 
812 static int otx2_rss_ctx_create(struct otx2_nic *pfvf,
813 			       u32 *rss_context)
814 {
815 	struct otx2_rss_info *rss = &pfvf->hw.rss_info;
816 	u8 ctx;
817 
818 	for (ctx = 0; ctx < MAX_RSS_GROUPS; ctx++) {
819 		if (!rss->rss_ctx[ctx])
820 			break;
821 	}
822 	if (ctx == MAX_RSS_GROUPS)
823 		return -EINVAL;
824 
825 	rss->rss_ctx[ctx] = kzalloc(sizeof(*rss->rss_ctx[ctx]), GFP_KERNEL);
826 	if (!rss->rss_ctx[ctx])
827 		return -ENOMEM;
828 	*rss_context = ctx;
829 
830 	return 0;
831 }
832 
833 /* RSS context configuration */
834 static int otx2_set_rxfh_context(struct net_device *dev, const u32 *indir,
835 				 const u8 *hkey, const u8 hfunc,
836 				 u32 *rss_context, bool delete)
837 {
838 	struct otx2_nic *pfvf = netdev_priv(dev);
839 	struct otx2_rss_ctx *rss_ctx;
840 	struct otx2_rss_info *rss;
841 	int ret, idx;
842 
843 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
844 		return -EOPNOTSUPP;
845 
846 	if (*rss_context != ETH_RXFH_CONTEXT_ALLOC &&
847 	    *rss_context >= MAX_RSS_GROUPS)
848 		return -EINVAL;
849 
850 	rss = &pfvf->hw.rss_info;
851 
852 	if (!rss->enable) {
853 		netdev_err(dev, "RSS is disabled, cannot change settings\n");
854 		return -EIO;
855 	}
856 
857 	if (hkey) {
858 		memcpy(rss->key, hkey, sizeof(rss->key));
859 		otx2_set_rss_key(pfvf);
860 	}
861 	if (delete)
862 		return otx2_rss_ctx_delete(pfvf, *rss_context);
863 
864 	if (*rss_context == ETH_RXFH_CONTEXT_ALLOC) {
865 		ret = otx2_rss_ctx_create(pfvf, rss_context);
866 		if (ret)
867 			return ret;
868 	}
869 	if (indir) {
870 		rss_ctx = rss->rss_ctx[*rss_context];
871 		for (idx = 0; idx < rss->rss_size; idx++)
872 			rss_ctx->ind_tbl[idx] = indir[idx];
873 	}
874 	otx2_set_rss_table(pfvf, *rss_context);
875 
876 	return 0;
877 }
878 
879 static int otx2_get_rxfh_context(struct net_device *dev, u32 *indir,
880 				 u8 *hkey, u8 *hfunc, u32 rss_context)
881 {
882 	struct otx2_nic *pfvf = netdev_priv(dev);
883 	struct otx2_rss_ctx *rss_ctx;
884 	struct otx2_rss_info *rss;
885 	int idx, rx_queues;
886 
887 	rss = &pfvf->hw.rss_info;
888 
889 	if (hfunc)
890 		*hfunc = ETH_RSS_HASH_TOP;
891 
892 	if (!indir)
893 		return 0;
894 
895 	if (!rss->enable && rss_context == DEFAULT_RSS_CONTEXT_GROUP) {
896 		rx_queues = pfvf->hw.rx_queues;
897 		for (idx = 0; idx < MAX_RSS_INDIR_TBL_SIZE; idx++)
898 			indir[idx] = ethtool_rxfh_indir_default(idx, rx_queues);
899 		return 0;
900 	}
901 	if (rss_context >= MAX_RSS_GROUPS)
902 		return -ENOENT;
903 
904 	rss_ctx = rss->rss_ctx[rss_context];
905 	if (!rss_ctx)
906 		return -ENOENT;
907 
908 	if (indir) {
909 		for (idx = 0; idx < rss->rss_size; idx++)
910 			indir[idx] = rss_ctx->ind_tbl[idx];
911 	}
912 	if (hkey)
913 		memcpy(hkey, rss->key, sizeof(rss->key));
914 
915 	return 0;
916 }
917 
918 /* Get RSS configuration */
919 static int otx2_get_rxfh(struct net_device *dev, u32 *indir,
920 			 u8 *hkey, u8 *hfunc)
921 {
922 	return otx2_get_rxfh_context(dev, indir, hkey, hfunc,
923 				     DEFAULT_RSS_CONTEXT_GROUP);
924 }
925 
926 /* Configure RSS table and hash key */
927 static int otx2_set_rxfh(struct net_device *dev, const u32 *indir,
928 			 const u8 *hkey, const u8 hfunc)
929 {
930 
931 	u32 rss_context = DEFAULT_RSS_CONTEXT_GROUP;
932 
933 	return otx2_set_rxfh_context(dev, indir, hkey, hfunc, &rss_context, 0);
934 }
935 
936 static u32 otx2_get_msglevel(struct net_device *netdev)
937 {
938 	struct otx2_nic *pfvf = netdev_priv(netdev);
939 
940 	return pfvf->msg_enable;
941 }
942 
943 static void otx2_set_msglevel(struct net_device *netdev, u32 val)
944 {
945 	struct otx2_nic *pfvf = netdev_priv(netdev);
946 
947 	pfvf->msg_enable = val;
948 }
949 
950 static u32 otx2_get_link(struct net_device *netdev)
951 {
952 	struct otx2_nic *pfvf = netdev_priv(netdev);
953 
954 	/* LBK link is internal and always UP */
955 	if (is_otx2_lbkvf(pfvf->pdev))
956 		return 1;
957 	return pfvf->linfo.link_up;
958 }
959 
960 static int otx2_get_ts_info(struct net_device *netdev,
961 			    struct ethtool_ts_info *info)
962 {
963 	struct otx2_nic *pfvf = netdev_priv(netdev);
964 
965 	if (!pfvf->ptp)
966 		return ethtool_op_get_ts_info(netdev, info);
967 
968 	info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
969 				SOF_TIMESTAMPING_RX_SOFTWARE |
970 				SOF_TIMESTAMPING_SOFTWARE |
971 				SOF_TIMESTAMPING_TX_HARDWARE |
972 				SOF_TIMESTAMPING_RX_HARDWARE |
973 				SOF_TIMESTAMPING_RAW_HARDWARE;
974 
975 	info->phc_index = otx2_ptp_clock_index(pfvf);
976 
977 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
978 	if (test_bit(CN10K_PTP_ONESTEP, &pfvf->hw.cap_flag))
979 		info->tx_types |= BIT(HWTSTAMP_TX_ONESTEP_SYNC);
980 
981 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
982 			   BIT(HWTSTAMP_FILTER_ALL);
983 
984 	return 0;
985 }
986 
987 static struct cgx_fw_data *otx2_get_fwdata(struct otx2_nic *pfvf)
988 {
989 	struct cgx_fw_data *rsp = NULL;
990 	struct msg_req *req;
991 	int err = 0;
992 
993 	mutex_lock(&pfvf->mbox.lock);
994 	req = otx2_mbox_alloc_msg_cgx_get_aux_link_info(&pfvf->mbox);
995 	if (!req) {
996 		mutex_unlock(&pfvf->mbox.lock);
997 		return ERR_PTR(-ENOMEM);
998 	}
999 
1000 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1001 	if (!err) {
1002 		rsp = (struct cgx_fw_data *)
1003 			otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
1004 	} else {
1005 		rsp = ERR_PTR(err);
1006 	}
1007 
1008 	mutex_unlock(&pfvf->mbox.lock);
1009 	return rsp;
1010 }
1011 
1012 static int otx2_get_fecparam(struct net_device *netdev,
1013 			     struct ethtool_fecparam *fecparam)
1014 {
1015 	struct otx2_nic *pfvf = netdev_priv(netdev);
1016 	struct cgx_fw_data *rsp;
1017 	const int fec[] = {
1018 		ETHTOOL_FEC_OFF,
1019 		ETHTOOL_FEC_BASER,
1020 		ETHTOOL_FEC_RS,
1021 		ETHTOOL_FEC_BASER | ETHTOOL_FEC_RS};
1022 #define FEC_MAX_INDEX 4
1023 	if (pfvf->linfo.fec < FEC_MAX_INDEX)
1024 		fecparam->active_fec = fec[pfvf->linfo.fec];
1025 
1026 	rsp = otx2_get_fwdata(pfvf);
1027 	if (IS_ERR(rsp))
1028 		return PTR_ERR(rsp);
1029 
1030 	if (rsp->fwdata.supported_fec < FEC_MAX_INDEX) {
1031 		if (!rsp->fwdata.supported_fec)
1032 			fecparam->fec = ETHTOOL_FEC_NONE;
1033 		else
1034 			fecparam->fec = fec[rsp->fwdata.supported_fec];
1035 	}
1036 	return 0;
1037 }
1038 
1039 static int otx2_set_fecparam(struct net_device *netdev,
1040 			     struct ethtool_fecparam *fecparam)
1041 {
1042 	struct otx2_nic *pfvf = netdev_priv(netdev);
1043 	struct mbox *mbox = &pfvf->mbox;
1044 	struct fec_mode *req, *rsp;
1045 	int err = 0, fec = 0;
1046 
1047 	switch (fecparam->fec) {
1048 	/* Firmware does not support AUTO mode consider it as FEC_OFF */
1049 	case ETHTOOL_FEC_OFF:
1050 	case ETHTOOL_FEC_AUTO:
1051 		fec = OTX2_FEC_OFF;
1052 		break;
1053 	case ETHTOOL_FEC_RS:
1054 		fec = OTX2_FEC_RS;
1055 		break;
1056 	case ETHTOOL_FEC_BASER:
1057 		fec = OTX2_FEC_BASER;
1058 		break;
1059 	default:
1060 		netdev_warn(pfvf->netdev, "Unsupported FEC mode: %d",
1061 			    fecparam->fec);
1062 		return -EINVAL;
1063 	}
1064 
1065 	if (fec == pfvf->linfo.fec)
1066 		return 0;
1067 
1068 	mutex_lock(&mbox->lock);
1069 	req = otx2_mbox_alloc_msg_cgx_set_fec_param(&pfvf->mbox);
1070 	if (!req) {
1071 		err = -ENOMEM;
1072 		goto end;
1073 	}
1074 	req->fec = fec;
1075 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1076 	if (err)
1077 		goto end;
1078 
1079 	rsp = (struct fec_mode *)otx2_mbox_get_rsp(&pfvf->mbox.mbox,
1080 						   0, &req->hdr);
1081 	if (rsp->fec >= 0)
1082 		pfvf->linfo.fec = rsp->fec;
1083 	else
1084 		err = rsp->fec;
1085 end:
1086 	mutex_unlock(&mbox->lock);
1087 	return err;
1088 }
1089 
1090 static void otx2_get_fec_info(u64 index, int req_mode,
1091 			      struct ethtool_link_ksettings *link_ksettings)
1092 {
1093 	__ETHTOOL_DECLARE_LINK_MODE_MASK(otx2_fec_modes) = { 0, };
1094 
1095 	switch (index) {
1096 	case OTX2_FEC_NONE:
1097 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT,
1098 				 otx2_fec_modes);
1099 		break;
1100 	case OTX2_FEC_BASER:
1101 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT,
1102 				 otx2_fec_modes);
1103 		break;
1104 	case OTX2_FEC_RS:
1105 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT,
1106 				 otx2_fec_modes);
1107 		break;
1108 	case OTX2_FEC_BASER | OTX2_FEC_RS:
1109 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT,
1110 				 otx2_fec_modes);
1111 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT,
1112 				 otx2_fec_modes);
1113 		break;
1114 	}
1115 
1116 	/* Add fec modes to existing modes */
1117 	if (req_mode == OTX2_MODE_ADVERTISED)
1118 		linkmode_or(link_ksettings->link_modes.advertising,
1119 			    link_ksettings->link_modes.advertising,
1120 			    otx2_fec_modes);
1121 	else
1122 		linkmode_or(link_ksettings->link_modes.supported,
1123 			    link_ksettings->link_modes.supported,
1124 			    otx2_fec_modes);
1125 }
1126 
1127 static void otx2_get_link_mode_info(u64 link_mode_bmap,
1128 				    bool req_mode,
1129 				    struct ethtool_link_ksettings
1130 				    *link_ksettings)
1131 {
1132 	__ETHTOOL_DECLARE_LINK_MODE_MASK(otx2_link_modes) = { 0, };
1133 	const int otx2_sgmii_features[6] = {
1134 		ETHTOOL_LINK_MODE_10baseT_Half_BIT,
1135 		ETHTOOL_LINK_MODE_10baseT_Full_BIT,
1136 		ETHTOOL_LINK_MODE_100baseT_Half_BIT,
1137 		ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1138 		ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1139 		ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1140 	};
1141 	/* CGX link modes to Ethtool link mode mapping */
1142 	const int cgx_link_mode[27] = {
1143 		0, /* SGMII  Mode */
1144 		ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
1145 		ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
1146 		ETHTOOL_LINK_MODE_10000baseSR_Full_BIT,
1147 		ETHTOOL_LINK_MODE_10000baseLR_Full_BIT,
1148 		ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
1149 		0,
1150 		ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
1151 		0,
1152 		0,
1153 		ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
1154 		ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
1155 		ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT,
1156 		ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT,
1157 		ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT,
1158 		ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT,
1159 		0,
1160 		ETHTOOL_LINK_MODE_50000baseSR_Full_BIT,
1161 		0,
1162 		ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
1163 		ETHTOOL_LINK_MODE_50000baseCR_Full_BIT,
1164 		ETHTOOL_LINK_MODE_50000baseKR_Full_BIT,
1165 		0,
1166 		ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT,
1167 		ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
1168 		ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
1169 		ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT
1170 	};
1171 	u8 bit;
1172 
1173 	for_each_set_bit(bit, (unsigned long *)&link_mode_bmap, 27) {
1174 		/* SGMII mode is set */
1175 		if (bit == 0)
1176 			linkmode_set_bit_array(otx2_sgmii_features,
1177 					       ARRAY_SIZE(otx2_sgmii_features),
1178 					       otx2_link_modes);
1179 		else
1180 			linkmode_set_bit(cgx_link_mode[bit], otx2_link_modes);
1181 	}
1182 
1183 	if (req_mode == OTX2_MODE_ADVERTISED)
1184 		linkmode_copy(link_ksettings->link_modes.advertising,
1185 			      otx2_link_modes);
1186 	else
1187 		linkmode_copy(link_ksettings->link_modes.supported,
1188 			      otx2_link_modes);
1189 }
1190 
1191 static int otx2_get_link_ksettings(struct net_device *netdev,
1192 				   struct ethtool_link_ksettings *cmd)
1193 {
1194 	struct otx2_nic *pfvf = netdev_priv(netdev);
1195 	struct cgx_fw_data *rsp = NULL;
1196 
1197 	cmd->base.duplex  = pfvf->linfo.full_duplex;
1198 	cmd->base.speed   = pfvf->linfo.speed;
1199 	cmd->base.autoneg = pfvf->linfo.an;
1200 
1201 	rsp = otx2_get_fwdata(pfvf);
1202 	if (IS_ERR(rsp))
1203 		return PTR_ERR(rsp);
1204 
1205 	if (rsp->fwdata.supported_an)
1206 		ethtool_link_ksettings_add_link_mode(cmd,
1207 						     supported,
1208 						     Autoneg);
1209 
1210 	otx2_get_link_mode_info(rsp->fwdata.advertised_link_modes,
1211 				OTX2_MODE_ADVERTISED, cmd);
1212 	otx2_get_fec_info(rsp->fwdata.advertised_fec,
1213 			  OTX2_MODE_ADVERTISED, cmd);
1214 	otx2_get_link_mode_info(rsp->fwdata.supported_link_modes,
1215 				OTX2_MODE_SUPPORTED, cmd);
1216 	otx2_get_fec_info(rsp->fwdata.supported_fec,
1217 			  OTX2_MODE_SUPPORTED, cmd);
1218 	return 0;
1219 }
1220 
1221 static void otx2_get_advertised_mode(const struct ethtool_link_ksettings *cmd,
1222 				     u64 *mode)
1223 {
1224 	u32 bit_pos;
1225 
1226 	/* Firmware does not support requesting multiple advertised modes
1227 	 * return first set bit
1228 	 */
1229 	bit_pos = find_first_bit(cmd->link_modes.advertising,
1230 				 __ETHTOOL_LINK_MODE_MASK_NBITS);
1231 	if (bit_pos != __ETHTOOL_LINK_MODE_MASK_NBITS)
1232 		*mode = bit_pos;
1233 }
1234 
1235 static int otx2_set_link_ksettings(struct net_device *netdev,
1236 				   const struct ethtool_link_ksettings *cmd)
1237 {
1238 	struct otx2_nic *pf = netdev_priv(netdev);
1239 	struct ethtool_link_ksettings cur_ks;
1240 	struct cgx_set_link_mode_req *req;
1241 	struct mbox *mbox = &pf->mbox;
1242 	int err = 0;
1243 
1244 	memset(&cur_ks, 0, sizeof(struct ethtool_link_ksettings));
1245 
1246 	if (!ethtool_validate_speed(cmd->base.speed) ||
1247 	    !ethtool_validate_duplex(cmd->base.duplex))
1248 		return -EINVAL;
1249 
1250 	if (cmd->base.autoneg != AUTONEG_ENABLE &&
1251 	    cmd->base.autoneg != AUTONEG_DISABLE)
1252 		return -EINVAL;
1253 
1254 	otx2_get_link_ksettings(netdev, &cur_ks);
1255 
1256 	/* Check requested modes against supported modes by hardware */
1257 	if (!linkmode_subset(cmd->link_modes.advertising,
1258 			     cur_ks.link_modes.supported))
1259 		return -EINVAL;
1260 
1261 	mutex_lock(&mbox->lock);
1262 	req = otx2_mbox_alloc_msg_cgx_set_link_mode(&pf->mbox);
1263 	if (!req) {
1264 		err = -ENOMEM;
1265 		goto end;
1266 	}
1267 
1268 	req->args.speed = cmd->base.speed;
1269 	/* firmware expects 1 for half duplex and 0 for full duplex
1270 	 * hence inverting
1271 	 */
1272 	req->args.duplex = cmd->base.duplex ^ 0x1;
1273 	req->args.an = cmd->base.autoneg;
1274 	otx2_get_advertised_mode(cmd, &req->args.mode);
1275 
1276 	err = otx2_sync_mbox_msg(&pf->mbox);
1277 end:
1278 	mutex_unlock(&mbox->lock);
1279 	return err;
1280 }
1281 
1282 static void otx2_get_fec_stats(struct net_device *netdev,
1283 			       struct ethtool_fec_stats *fec_stats)
1284 {
1285 	struct otx2_nic *pfvf = netdev_priv(netdev);
1286 	struct cgx_fw_data *rsp;
1287 
1288 	otx2_update_lmac_fec_stats(pfvf);
1289 
1290 	/* Report MAC FEC stats */
1291 	fec_stats->corrected_blocks.total     = pfvf->hw.cgx_fec_corr_blks;
1292 	fec_stats->uncorrectable_blocks.total = pfvf->hw.cgx_fec_uncorr_blks;
1293 
1294 	rsp = otx2_get_fwdata(pfvf);
1295 	if (!IS_ERR(rsp) && rsp->fwdata.phy.misc.has_fec_stats &&
1296 	    !otx2_get_phy_fec_stats(pfvf)) {
1297 		/* Fetch fwdata again because it's been recently populated with
1298 		 * latest PHY FEC stats.
1299 		 */
1300 		rsp = otx2_get_fwdata(pfvf);
1301 		if (!IS_ERR(rsp)) {
1302 			struct fec_stats_s *p = &rsp->fwdata.phy.fec_stats;
1303 
1304 			if (pfvf->linfo.fec == OTX2_FEC_BASER) {
1305 				fec_stats->corrected_blocks.total = p->brfec_corr_blks;
1306 				fec_stats->uncorrectable_blocks.total = p->brfec_uncorr_blks;
1307 			} else {
1308 				fec_stats->corrected_blocks.total = p->rsfec_corr_cws;
1309 				fec_stats->uncorrectable_blocks.total = p->rsfec_uncorr_cws;
1310 			}
1311 		}
1312 	}
1313 }
1314 
1315 static const struct ethtool_ops otx2_ethtool_ops = {
1316 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1317 				     ETHTOOL_COALESCE_MAX_FRAMES |
1318 				     ETHTOOL_COALESCE_USE_ADAPTIVE,
1319 	.supported_ring_params  = ETHTOOL_RING_USE_RX_BUF_LEN |
1320 				  ETHTOOL_RING_USE_CQE_SIZE,
1321 	.get_link		= otx2_get_link,
1322 	.get_drvinfo		= otx2_get_drvinfo,
1323 	.get_strings		= otx2_get_strings,
1324 	.get_ethtool_stats	= otx2_get_ethtool_stats,
1325 	.get_sset_count		= otx2_get_sset_count,
1326 	.set_channels		= otx2_set_channels,
1327 	.get_channels		= otx2_get_channels,
1328 	.get_ringparam		= otx2_get_ringparam,
1329 	.set_ringparam		= otx2_set_ringparam,
1330 	.get_coalesce		= otx2_get_coalesce,
1331 	.set_coalesce		= otx2_set_coalesce,
1332 	.get_rxnfc		= otx2_get_rxnfc,
1333 	.set_rxnfc              = otx2_set_rxnfc,
1334 	.get_rxfh_key_size	= otx2_get_rxfh_key_size,
1335 	.get_rxfh_indir_size	= otx2_get_rxfh_indir_size,
1336 	.get_rxfh		= otx2_get_rxfh,
1337 	.set_rxfh		= otx2_set_rxfh,
1338 	.get_rxfh_context	= otx2_get_rxfh_context,
1339 	.set_rxfh_context	= otx2_set_rxfh_context,
1340 	.get_msglevel		= otx2_get_msglevel,
1341 	.set_msglevel		= otx2_set_msglevel,
1342 	.get_pauseparam		= otx2_get_pauseparam,
1343 	.set_pauseparam		= otx2_set_pauseparam,
1344 	.get_ts_info		= otx2_get_ts_info,
1345 	.get_fec_stats		= otx2_get_fec_stats,
1346 	.get_fecparam		= otx2_get_fecparam,
1347 	.set_fecparam		= otx2_set_fecparam,
1348 	.get_link_ksettings     = otx2_get_link_ksettings,
1349 	.set_link_ksettings     = otx2_set_link_ksettings,
1350 };
1351 
1352 void otx2_set_ethtool_ops(struct net_device *netdev)
1353 {
1354 	netdev->ethtool_ops = &otx2_ethtool_ops;
1355 }
1356 
1357 /* VF's ethtool APIs */
1358 static void otx2vf_get_drvinfo(struct net_device *netdev,
1359 			       struct ethtool_drvinfo *info)
1360 {
1361 	struct otx2_nic *vf = netdev_priv(netdev);
1362 
1363 	strscpy(info->driver, DRV_VF_NAME, sizeof(info->driver));
1364 	strscpy(info->bus_info, pci_name(vf->pdev), sizeof(info->bus_info));
1365 }
1366 
1367 static void otx2vf_get_strings(struct net_device *netdev, u32 sset, u8 *data)
1368 {
1369 	struct otx2_nic *vf = netdev_priv(netdev);
1370 	int stats;
1371 
1372 	if (sset != ETH_SS_STATS)
1373 		return;
1374 
1375 	for (stats = 0; stats < otx2_n_dev_stats; stats++) {
1376 		memcpy(data, otx2_dev_stats[stats].name, ETH_GSTRING_LEN);
1377 		data += ETH_GSTRING_LEN;
1378 	}
1379 
1380 	for (stats = 0; stats < otx2_n_drv_stats; stats++) {
1381 		memcpy(data, otx2_drv_stats[stats].name, ETH_GSTRING_LEN);
1382 		data += ETH_GSTRING_LEN;
1383 	}
1384 
1385 	otx2_get_qset_strings(vf, &data, 0);
1386 
1387 	strcpy(data, "reset_count");
1388 	data += ETH_GSTRING_LEN;
1389 }
1390 
1391 static void otx2vf_get_ethtool_stats(struct net_device *netdev,
1392 				     struct ethtool_stats *stats, u64 *data)
1393 {
1394 	struct otx2_nic *vf = netdev_priv(netdev);
1395 	int stat;
1396 
1397 	otx2_get_dev_stats(vf);
1398 	for (stat = 0; stat < otx2_n_dev_stats; stat++)
1399 		*(data++) = ((u64 *)&vf->hw.dev_stats)
1400 				[otx2_dev_stats[stat].index];
1401 
1402 	for (stat = 0; stat < otx2_n_drv_stats; stat++)
1403 		*(data++) = atomic_read(&((atomic_t *)&vf->hw.drv_stats)
1404 						[otx2_drv_stats[stat].index]);
1405 
1406 	otx2_get_qset_stats(vf, stats, &data);
1407 	*(data++) = vf->reset_count;
1408 }
1409 
1410 static int otx2vf_get_sset_count(struct net_device *netdev, int sset)
1411 {
1412 	struct otx2_nic *vf = netdev_priv(netdev);
1413 	int qstats_count;
1414 
1415 	if (sset != ETH_SS_STATS)
1416 		return -EINVAL;
1417 
1418 	qstats_count = otx2_n_queue_stats *
1419 		       (vf->hw.rx_queues + otx2_get_total_tx_queues(vf));
1420 
1421 	return otx2_n_dev_stats + otx2_n_drv_stats + qstats_count + 1;
1422 }
1423 
1424 static int otx2vf_get_link_ksettings(struct net_device *netdev,
1425 				     struct ethtool_link_ksettings *cmd)
1426 {
1427 	struct otx2_nic *pfvf = netdev_priv(netdev);
1428 
1429 	if (is_otx2_lbkvf(pfvf->pdev)) {
1430 		cmd->base.duplex = DUPLEX_FULL;
1431 		cmd->base.speed = SPEED_100000;
1432 	} else {
1433 		return otx2_get_link_ksettings(netdev, cmd);
1434 	}
1435 	return 0;
1436 }
1437 
1438 static const struct ethtool_ops otx2vf_ethtool_ops = {
1439 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1440 				     ETHTOOL_COALESCE_MAX_FRAMES |
1441 				     ETHTOOL_COALESCE_USE_ADAPTIVE,
1442 	.supported_ring_params  = ETHTOOL_RING_USE_RX_BUF_LEN |
1443 				  ETHTOOL_RING_USE_CQE_SIZE,
1444 	.get_link		= otx2_get_link,
1445 	.get_drvinfo		= otx2vf_get_drvinfo,
1446 	.get_strings		= otx2vf_get_strings,
1447 	.get_ethtool_stats	= otx2vf_get_ethtool_stats,
1448 	.get_sset_count		= otx2vf_get_sset_count,
1449 	.set_channels		= otx2_set_channels,
1450 	.get_channels		= otx2_get_channels,
1451 	.get_rxnfc		= otx2_get_rxnfc,
1452 	.set_rxnfc              = otx2_set_rxnfc,
1453 	.get_rxfh_key_size	= otx2_get_rxfh_key_size,
1454 	.get_rxfh_indir_size	= otx2_get_rxfh_indir_size,
1455 	.get_rxfh		= otx2_get_rxfh,
1456 	.set_rxfh		= otx2_set_rxfh,
1457 	.get_rxfh_context	= otx2_get_rxfh_context,
1458 	.set_rxfh_context	= otx2_set_rxfh_context,
1459 	.get_ringparam		= otx2_get_ringparam,
1460 	.set_ringparam		= otx2_set_ringparam,
1461 	.get_coalesce		= otx2_get_coalesce,
1462 	.set_coalesce		= otx2_set_coalesce,
1463 	.get_msglevel		= otx2_get_msglevel,
1464 	.set_msglevel		= otx2_set_msglevel,
1465 	.get_pauseparam		= otx2_get_pauseparam,
1466 	.set_pauseparam		= otx2_set_pauseparam,
1467 	.get_link_ksettings     = otx2vf_get_link_ksettings,
1468 	.get_ts_info		= otx2_get_ts_info,
1469 };
1470 
1471 void otx2vf_set_ethtool_ops(struct net_device *netdev)
1472 {
1473 	netdev->ethtool_ops = &otx2vf_ethtool_ops;
1474 }
1475 EXPORT_SYMBOL(otx2vf_set_ethtool_ops);
1476