• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell OcteonTx2 RVU Ethernet driver
3  *
4  * Copyright (C) 2020 Marvell International Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #include <linux/pci.h>
12 #include <linux/ethtool.h>
13 #include <linux/stddef.h>
14 #include <linux/etherdevice.h>
15 #include <linux/log2.h>
16 #include <linux/net_tstamp.h>
17 
18 #include "otx2_common.h"
19 #include "otx2_ptp.h"
20 
21 #define DRV_NAME	"octeontx2-nicpf"
22 #define DRV_VF_NAME	"octeontx2-nicvf"
23 
24 struct otx2_stat {
25 	char name[ETH_GSTRING_LEN];
26 	unsigned int index;
27 };
28 
29 /* HW device stats */
30 #define OTX2_DEV_STAT(stat) { \
31 	.name = #stat, \
32 	.index = offsetof(struct otx2_dev_stats, stat) / sizeof(u64), \
33 }
34 
35 static const struct otx2_stat otx2_dev_stats[] = {
36 	OTX2_DEV_STAT(rx_ucast_frames),
37 	OTX2_DEV_STAT(rx_bcast_frames),
38 	OTX2_DEV_STAT(rx_mcast_frames),
39 
40 	OTX2_DEV_STAT(tx_ucast_frames),
41 	OTX2_DEV_STAT(tx_bcast_frames),
42 	OTX2_DEV_STAT(tx_mcast_frames),
43 };
44 
45 /* Driver level stats */
46 #define OTX2_DRV_STAT(stat) { \
47 	.name = #stat, \
48 	.index = offsetof(struct otx2_drv_stats, stat) / sizeof(atomic_t), \
49 }
50 
51 static const struct otx2_stat otx2_drv_stats[] = {
52 	OTX2_DRV_STAT(rx_fcs_errs),
53 	OTX2_DRV_STAT(rx_oversize_errs),
54 	OTX2_DRV_STAT(rx_undersize_errs),
55 	OTX2_DRV_STAT(rx_csum_errs),
56 	OTX2_DRV_STAT(rx_len_errs),
57 	OTX2_DRV_STAT(rx_other_errs),
58 };
59 
60 static const struct otx2_stat otx2_queue_stats[] = {
61 	{ "bytes", 0 },
62 	{ "frames", 1 },
63 };
64 
65 static const unsigned int otx2_n_dev_stats = ARRAY_SIZE(otx2_dev_stats);
66 static const unsigned int otx2_n_drv_stats = ARRAY_SIZE(otx2_drv_stats);
67 static const unsigned int otx2_n_queue_stats = ARRAY_SIZE(otx2_queue_stats);
68 
otx2_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * info)69 static void otx2_get_drvinfo(struct net_device *netdev,
70 			     struct ethtool_drvinfo *info)
71 {
72 	struct otx2_nic *pfvf = netdev_priv(netdev);
73 
74 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
75 	strlcpy(info->bus_info, pci_name(pfvf->pdev), sizeof(info->bus_info));
76 }
77 
otx2_get_qset_strings(struct otx2_nic * pfvf,u8 ** data,int qset)78 static void otx2_get_qset_strings(struct otx2_nic *pfvf, u8 **data, int qset)
79 {
80 	int start_qidx = qset * pfvf->hw.rx_queues;
81 	int qidx, stats;
82 
83 	for (qidx = 0; qidx < pfvf->hw.rx_queues; qidx++) {
84 		for (stats = 0; stats < otx2_n_queue_stats; stats++) {
85 			sprintf(*data, "rxq%d: %s", qidx + start_qidx,
86 				otx2_queue_stats[stats].name);
87 			*data += ETH_GSTRING_LEN;
88 		}
89 	}
90 	for (qidx = 0; qidx < pfvf->hw.tx_queues; qidx++) {
91 		for (stats = 0; stats < otx2_n_queue_stats; stats++) {
92 			sprintf(*data, "txq%d: %s", qidx + start_qidx,
93 				otx2_queue_stats[stats].name);
94 			*data += ETH_GSTRING_LEN;
95 		}
96 	}
97 }
98 
otx2_get_strings(struct net_device * netdev,u32 sset,u8 * data)99 static void otx2_get_strings(struct net_device *netdev, u32 sset, u8 *data)
100 {
101 	struct otx2_nic *pfvf = netdev_priv(netdev);
102 	int stats;
103 
104 	if (sset != ETH_SS_STATS)
105 		return;
106 
107 	for (stats = 0; stats < otx2_n_dev_stats; stats++) {
108 		memcpy(data, otx2_dev_stats[stats].name, ETH_GSTRING_LEN);
109 		data += ETH_GSTRING_LEN;
110 	}
111 
112 	for (stats = 0; stats < otx2_n_drv_stats; stats++) {
113 		memcpy(data, otx2_drv_stats[stats].name, ETH_GSTRING_LEN);
114 		data += ETH_GSTRING_LEN;
115 	}
116 
117 	otx2_get_qset_strings(pfvf, &data, 0);
118 
119 	for (stats = 0; stats < CGX_RX_STATS_COUNT; stats++) {
120 		sprintf(data, "cgx_rxstat%d: ", stats);
121 		data += ETH_GSTRING_LEN;
122 	}
123 
124 	for (stats = 0; stats < CGX_TX_STATS_COUNT; stats++) {
125 		sprintf(data, "cgx_txstat%d: ", stats);
126 		data += ETH_GSTRING_LEN;
127 	}
128 
129 	strcpy(data, "reset_count");
130 	data += ETH_GSTRING_LEN;
131 }
132 
otx2_get_qset_stats(struct otx2_nic * pfvf,struct ethtool_stats * stats,u64 ** data)133 static void otx2_get_qset_stats(struct otx2_nic *pfvf,
134 				struct ethtool_stats *stats, u64 **data)
135 {
136 	int stat, qidx;
137 
138 	if (!pfvf)
139 		return;
140 	for (qidx = 0; qidx < pfvf->hw.rx_queues; qidx++) {
141 		if (!otx2_update_rq_stats(pfvf, qidx)) {
142 			for (stat = 0; stat < otx2_n_queue_stats; stat++)
143 				*((*data)++) = 0;
144 			continue;
145 		}
146 		for (stat = 0; stat < otx2_n_queue_stats; stat++)
147 			*((*data)++) = ((u64 *)&pfvf->qset.rq[qidx].stats)
148 				[otx2_queue_stats[stat].index];
149 	}
150 
151 	for (qidx = 0; qidx < pfvf->hw.tx_queues; qidx++) {
152 		if (!otx2_update_sq_stats(pfvf, qidx)) {
153 			for (stat = 0; stat < otx2_n_queue_stats; stat++)
154 				*((*data)++) = 0;
155 			continue;
156 		}
157 		for (stat = 0; stat < otx2_n_queue_stats; stat++)
158 			*((*data)++) = ((u64 *)&pfvf->qset.sq[qidx].stats)
159 				[otx2_queue_stats[stat].index];
160 	}
161 }
162 
163 /* Get device and per queue statistics */
otx2_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * data)164 static void otx2_get_ethtool_stats(struct net_device *netdev,
165 				   struct ethtool_stats *stats, u64 *data)
166 {
167 	struct otx2_nic *pfvf = netdev_priv(netdev);
168 	int stat;
169 
170 	otx2_get_dev_stats(pfvf);
171 	for (stat = 0; stat < otx2_n_dev_stats; stat++)
172 		*(data++) = ((u64 *)&pfvf->hw.dev_stats)
173 				[otx2_dev_stats[stat].index];
174 
175 	for (stat = 0; stat < otx2_n_drv_stats; stat++)
176 		*(data++) = atomic_read(&((atomic_t *)&pfvf->hw.drv_stats)
177 						[otx2_drv_stats[stat].index]);
178 
179 	otx2_get_qset_stats(pfvf, stats, &data);
180 	otx2_update_lmac_stats(pfvf);
181 	for (stat = 0; stat < CGX_RX_STATS_COUNT; stat++)
182 		*(data++) = pfvf->hw.cgx_rx_stats[stat];
183 	for (stat = 0; stat < CGX_TX_STATS_COUNT; stat++)
184 		*(data++) = pfvf->hw.cgx_tx_stats[stat];
185 	*(data++) = pfvf->reset_count;
186 }
187 
otx2_get_sset_count(struct net_device * netdev,int sset)188 static int otx2_get_sset_count(struct net_device *netdev, int sset)
189 {
190 	struct otx2_nic *pfvf = netdev_priv(netdev);
191 	int qstats_count;
192 
193 	if (sset != ETH_SS_STATS)
194 		return -EINVAL;
195 
196 	qstats_count = otx2_n_queue_stats *
197 		       (pfvf->hw.rx_queues + pfvf->hw.tx_queues);
198 
199 	return otx2_n_dev_stats + otx2_n_drv_stats + qstats_count +
200 		CGX_RX_STATS_COUNT + CGX_TX_STATS_COUNT + 1;
201 }
202 
203 /* Get no of queues device supports and current queue count */
otx2_get_channels(struct net_device * dev,struct ethtool_channels * channel)204 static void otx2_get_channels(struct net_device *dev,
205 			      struct ethtool_channels *channel)
206 {
207 	struct otx2_nic *pfvf = netdev_priv(dev);
208 
209 	channel->max_rx = pfvf->hw.max_queues;
210 	channel->max_tx = pfvf->hw.max_queues;
211 
212 	channel->rx_count = pfvf->hw.rx_queues;
213 	channel->tx_count = pfvf->hw.tx_queues;
214 }
215 
216 /* Set no of Tx, Rx queues to be used */
otx2_set_channels(struct net_device * dev,struct ethtool_channels * channel)217 static int otx2_set_channels(struct net_device *dev,
218 			     struct ethtool_channels *channel)
219 {
220 	struct otx2_nic *pfvf = netdev_priv(dev);
221 	bool if_up = netif_running(dev);
222 	int err = 0;
223 
224 	if (!channel->rx_count || !channel->tx_count)
225 		return -EINVAL;
226 
227 	if (if_up)
228 		dev->netdev_ops->ndo_stop(dev);
229 
230 	err = otx2_set_real_num_queues(dev, channel->tx_count,
231 				       channel->rx_count);
232 	if (err)
233 		return err;
234 
235 	pfvf->hw.rx_queues = channel->rx_count;
236 	pfvf->hw.tx_queues = channel->tx_count;
237 	pfvf->qset.cq_cnt = pfvf->hw.tx_queues +  pfvf->hw.rx_queues;
238 
239 	if (if_up)
240 		err = dev->netdev_ops->ndo_open(dev);
241 
242 	netdev_info(dev, "Setting num Tx rings to %d, Rx rings to %d success\n",
243 		    pfvf->hw.tx_queues, pfvf->hw.rx_queues);
244 
245 	return err;
246 }
247 
otx2_get_pauseparam(struct net_device * netdev,struct ethtool_pauseparam * pause)248 static void otx2_get_pauseparam(struct net_device *netdev,
249 				struct ethtool_pauseparam *pause)
250 {
251 	struct otx2_nic *pfvf = netdev_priv(netdev);
252 	struct cgx_pause_frm_cfg *req, *rsp;
253 
254 	if (is_otx2_lbkvf(pfvf->pdev))
255 		return;
256 
257 	req = otx2_mbox_alloc_msg_cgx_cfg_pause_frm(&pfvf->mbox);
258 	if (!req)
259 		return;
260 
261 	if (!otx2_sync_mbox_msg(&pfvf->mbox)) {
262 		rsp = (struct cgx_pause_frm_cfg *)
263 		       otx2_mbox_get_rsp(&pfvf->mbox.mbox, 0, &req->hdr);
264 		pause->rx_pause = rsp->rx_pause;
265 		pause->tx_pause = rsp->tx_pause;
266 	}
267 }
268 
otx2_set_pauseparam(struct net_device * netdev,struct ethtool_pauseparam * pause)269 static int otx2_set_pauseparam(struct net_device *netdev,
270 			       struct ethtool_pauseparam *pause)
271 {
272 	struct otx2_nic *pfvf = netdev_priv(netdev);
273 
274 	if (pause->autoneg)
275 		return -EOPNOTSUPP;
276 
277 	if (is_otx2_lbkvf(pfvf->pdev))
278 		return -EOPNOTSUPP;
279 
280 	if (pause->rx_pause)
281 		pfvf->flags |= OTX2_FLAG_RX_PAUSE_ENABLED;
282 	else
283 		pfvf->flags &= ~OTX2_FLAG_RX_PAUSE_ENABLED;
284 
285 	if (pause->tx_pause)
286 		pfvf->flags |= OTX2_FLAG_TX_PAUSE_ENABLED;
287 	else
288 		pfvf->flags &= ~OTX2_FLAG_TX_PAUSE_ENABLED;
289 
290 	return otx2_config_pause_frm(pfvf);
291 }
292 
otx2_get_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring)293 static void otx2_get_ringparam(struct net_device *netdev,
294 			       struct ethtool_ringparam *ring)
295 {
296 	struct otx2_nic *pfvf = netdev_priv(netdev);
297 	struct otx2_qset *qs = &pfvf->qset;
298 
299 	ring->rx_max_pending = Q_COUNT(Q_SIZE_MAX);
300 	ring->rx_pending = qs->rqe_cnt ? qs->rqe_cnt : Q_COUNT(Q_SIZE_256);
301 	ring->tx_max_pending = Q_COUNT(Q_SIZE_MAX);
302 	ring->tx_pending = qs->sqe_cnt ? qs->sqe_cnt : Q_COUNT(Q_SIZE_4K);
303 }
304 
otx2_set_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring)305 static int otx2_set_ringparam(struct net_device *netdev,
306 			      struct ethtool_ringparam *ring)
307 {
308 	struct otx2_nic *pfvf = netdev_priv(netdev);
309 	bool if_up = netif_running(netdev);
310 	struct otx2_qset *qs = &pfvf->qset;
311 	u32 rx_count, tx_count;
312 
313 	if (ring->rx_mini_pending || ring->rx_jumbo_pending)
314 		return -EINVAL;
315 
316 	/* Permitted lengths are 16 64 256 1K 4K 16K 64K 256K 1M  */
317 	rx_count = ring->rx_pending;
318 	/* On some silicon variants a skid or reserved CQEs are
319 	 * needed to avoid CQ overflow.
320 	 */
321 	if (rx_count < pfvf->hw.rq_skid)
322 		rx_count =  pfvf->hw.rq_skid;
323 	rx_count = Q_COUNT(Q_SIZE(rx_count, 3));
324 
325 	/* Due pipelining impact minimum 2000 unused SQ CQE's
326 	 * need to be maintained to avoid CQ overflow, hence the
327 	 * minimum 4K size.
328 	 */
329 	tx_count = clamp_t(u32, ring->tx_pending,
330 			   Q_COUNT(Q_SIZE_4K), Q_COUNT(Q_SIZE_MAX));
331 	tx_count = Q_COUNT(Q_SIZE(tx_count, 3));
332 
333 	if (tx_count == qs->sqe_cnt && rx_count == qs->rqe_cnt)
334 		return 0;
335 
336 	if (if_up)
337 		netdev->netdev_ops->ndo_stop(netdev);
338 
339 	/* Assigned to the nearest possible exponent. */
340 	qs->sqe_cnt = tx_count;
341 	qs->rqe_cnt = rx_count;
342 
343 	if (if_up)
344 		return netdev->netdev_ops->ndo_open(netdev);
345 
346 	return 0;
347 }
348 
otx2_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * cmd)349 static int otx2_get_coalesce(struct net_device *netdev,
350 			     struct ethtool_coalesce *cmd)
351 {
352 	struct otx2_nic *pfvf = netdev_priv(netdev);
353 	struct otx2_hw *hw = &pfvf->hw;
354 
355 	cmd->rx_coalesce_usecs = hw->cq_time_wait;
356 	cmd->rx_max_coalesced_frames = hw->cq_ecount_wait;
357 	cmd->tx_coalesce_usecs = hw->cq_time_wait;
358 	cmd->tx_max_coalesced_frames = hw->cq_ecount_wait;
359 
360 	return 0;
361 }
362 
otx2_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec)363 static int otx2_set_coalesce(struct net_device *netdev,
364 			     struct ethtool_coalesce *ec)
365 {
366 	struct otx2_nic *pfvf = netdev_priv(netdev);
367 	struct otx2_hw *hw = &pfvf->hw;
368 	int qidx;
369 
370 	if (!ec->rx_max_coalesced_frames || !ec->tx_max_coalesced_frames)
371 		return 0;
372 
373 	/* 'cq_time_wait' is 8bit and is in multiple of 100ns,
374 	 * so clamp the user given value to the range of 1 to 25usec.
375 	 */
376 	ec->rx_coalesce_usecs = clamp_t(u32, ec->rx_coalesce_usecs,
377 					1, CQ_TIMER_THRESH_MAX);
378 	ec->tx_coalesce_usecs = clamp_t(u32, ec->tx_coalesce_usecs,
379 					1, CQ_TIMER_THRESH_MAX);
380 
381 	/* Rx and Tx are mapped to same CQ, check which one
382 	 * is changed, if both then choose the min.
383 	 */
384 	if (hw->cq_time_wait == ec->rx_coalesce_usecs)
385 		hw->cq_time_wait = ec->tx_coalesce_usecs;
386 	else if (hw->cq_time_wait == ec->tx_coalesce_usecs)
387 		hw->cq_time_wait = ec->rx_coalesce_usecs;
388 	else
389 		hw->cq_time_wait = min_t(u8, ec->rx_coalesce_usecs,
390 					 ec->tx_coalesce_usecs);
391 
392 	/* Max ecount_wait supported is 16bit,
393 	 * so clamp the user given value to the range of 1 to 64k.
394 	 */
395 	ec->rx_max_coalesced_frames = clamp_t(u32, ec->rx_max_coalesced_frames,
396 					      1, U16_MAX);
397 	ec->tx_max_coalesced_frames = clamp_t(u32, ec->tx_max_coalesced_frames,
398 					      1, U16_MAX);
399 
400 	/* Rx and Tx are mapped to same CQ, check which one
401 	 * is changed, if both then choose the min.
402 	 */
403 	if (hw->cq_ecount_wait == ec->rx_max_coalesced_frames)
404 		hw->cq_ecount_wait = ec->tx_max_coalesced_frames;
405 	else if (hw->cq_ecount_wait == ec->tx_max_coalesced_frames)
406 		hw->cq_ecount_wait = ec->rx_max_coalesced_frames;
407 	else
408 		hw->cq_ecount_wait = min_t(u16, ec->rx_max_coalesced_frames,
409 					   ec->tx_max_coalesced_frames);
410 
411 	if (netif_running(netdev)) {
412 		for (qidx = 0; qidx < pfvf->hw.cint_cnt; qidx++)
413 			otx2_config_irq_coalescing(pfvf, qidx);
414 	}
415 
416 	return 0;
417 }
418 
otx2_get_rss_hash_opts(struct otx2_nic * pfvf,struct ethtool_rxnfc * nfc)419 static int otx2_get_rss_hash_opts(struct otx2_nic *pfvf,
420 				  struct ethtool_rxnfc *nfc)
421 {
422 	struct otx2_rss_info *rss = &pfvf->hw.rss_info;
423 
424 	if (!(rss->flowkey_cfg &
425 	    (NIX_FLOW_KEY_TYPE_IPV4 | NIX_FLOW_KEY_TYPE_IPV6)))
426 		return 0;
427 
428 	/* Mimimum is IPv4 and IPv6, SIP/DIP */
429 	nfc->data = RXH_IP_SRC | RXH_IP_DST;
430 	if (rss->flowkey_cfg & NIX_FLOW_KEY_TYPE_VLAN)
431 		nfc->data |= RXH_VLAN;
432 
433 	switch (nfc->flow_type) {
434 	case TCP_V4_FLOW:
435 	case TCP_V6_FLOW:
436 		if (rss->flowkey_cfg & NIX_FLOW_KEY_TYPE_TCP)
437 			nfc->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
438 		break;
439 	case UDP_V4_FLOW:
440 	case UDP_V6_FLOW:
441 		if (rss->flowkey_cfg & NIX_FLOW_KEY_TYPE_UDP)
442 			nfc->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
443 		break;
444 	case SCTP_V4_FLOW:
445 	case SCTP_V6_FLOW:
446 		if (rss->flowkey_cfg & NIX_FLOW_KEY_TYPE_SCTP)
447 			nfc->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
448 		break;
449 	case AH_ESP_V4_FLOW:
450 	case AH_V4_FLOW:
451 	case ESP_V4_FLOW:
452 	case IPV4_FLOW:
453 	case AH_ESP_V6_FLOW:
454 	case AH_V6_FLOW:
455 	case ESP_V6_FLOW:
456 	case IPV6_FLOW:
457 		break;
458 	default:
459 		return -EINVAL;
460 	}
461 	return 0;
462 }
463 
otx2_set_rss_hash_opts(struct otx2_nic * pfvf,struct ethtool_rxnfc * nfc)464 static int otx2_set_rss_hash_opts(struct otx2_nic *pfvf,
465 				  struct ethtool_rxnfc *nfc)
466 {
467 	struct otx2_rss_info *rss = &pfvf->hw.rss_info;
468 	u32 rxh_l4 = RXH_L4_B_0_1 | RXH_L4_B_2_3;
469 	u32 rss_cfg = rss->flowkey_cfg;
470 
471 	if (!rss->enable) {
472 		netdev_err(pfvf->netdev,
473 			   "RSS is disabled, cannot change settings\n");
474 		return -EIO;
475 	}
476 
477 	/* Mimimum is IPv4 and IPv6, SIP/DIP */
478 	if (!(nfc->data & RXH_IP_SRC) || !(nfc->data & RXH_IP_DST))
479 		return -EINVAL;
480 
481 	if (nfc->data & RXH_VLAN)
482 		rss_cfg |=  NIX_FLOW_KEY_TYPE_VLAN;
483 	else
484 		rss_cfg &= ~NIX_FLOW_KEY_TYPE_VLAN;
485 
486 	switch (nfc->flow_type) {
487 	case TCP_V4_FLOW:
488 	case TCP_V6_FLOW:
489 		/* Different config for v4 and v6 is not supported.
490 		 * Both of them have to be either 4-tuple or 2-tuple.
491 		 */
492 		switch (nfc->data & rxh_l4) {
493 		case 0:
494 			rss_cfg &= ~NIX_FLOW_KEY_TYPE_TCP;
495 			break;
496 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
497 			rss_cfg |= NIX_FLOW_KEY_TYPE_TCP;
498 			break;
499 		default:
500 			return -EINVAL;
501 		}
502 		break;
503 	case UDP_V4_FLOW:
504 	case UDP_V6_FLOW:
505 		switch (nfc->data & rxh_l4) {
506 		case 0:
507 			rss_cfg &= ~NIX_FLOW_KEY_TYPE_UDP;
508 			break;
509 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
510 			rss_cfg |= NIX_FLOW_KEY_TYPE_UDP;
511 			break;
512 		default:
513 			return -EINVAL;
514 		}
515 		break;
516 	case SCTP_V4_FLOW:
517 	case SCTP_V6_FLOW:
518 		switch (nfc->data & rxh_l4) {
519 		case 0:
520 			rss_cfg &= ~NIX_FLOW_KEY_TYPE_SCTP;
521 			break;
522 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
523 			rss_cfg |= NIX_FLOW_KEY_TYPE_SCTP;
524 			break;
525 		default:
526 			return -EINVAL;
527 		}
528 		break;
529 	case IPV4_FLOW:
530 	case IPV6_FLOW:
531 		rss_cfg = NIX_FLOW_KEY_TYPE_IPV4 | NIX_FLOW_KEY_TYPE_IPV6;
532 		break;
533 	default:
534 		return -EINVAL;
535 	}
536 
537 	rss->flowkey_cfg = rss_cfg;
538 	otx2_set_flowkey_cfg(pfvf);
539 	return 0;
540 }
541 
otx2_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * nfc,u32 * rules)542 static int otx2_get_rxnfc(struct net_device *dev,
543 			  struct ethtool_rxnfc *nfc, u32 *rules)
544 {
545 	struct otx2_nic *pfvf = netdev_priv(dev);
546 	int ret = -EOPNOTSUPP;
547 
548 	switch (nfc->cmd) {
549 	case ETHTOOL_GRXRINGS:
550 		nfc->data = pfvf->hw.rx_queues;
551 		ret = 0;
552 		break;
553 	case ETHTOOL_GRXFH:
554 		return otx2_get_rss_hash_opts(pfvf, nfc);
555 	default:
556 		break;
557 	}
558 	return ret;
559 }
560 
otx2_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * nfc)561 static int otx2_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *nfc)
562 {
563 	struct otx2_nic *pfvf = netdev_priv(dev);
564 	int ret = -EOPNOTSUPP;
565 
566 	switch (nfc->cmd) {
567 	case ETHTOOL_SRXFH:
568 		ret = otx2_set_rss_hash_opts(pfvf, nfc);
569 		break;
570 	default:
571 		break;
572 	}
573 
574 	return ret;
575 }
576 
otx2_get_rxfh_key_size(struct net_device * netdev)577 static u32 otx2_get_rxfh_key_size(struct net_device *netdev)
578 {
579 	struct otx2_nic *pfvf = netdev_priv(netdev);
580 	struct otx2_rss_info *rss;
581 
582 	rss = &pfvf->hw.rss_info;
583 
584 	return sizeof(rss->key);
585 }
586 
otx2_get_rxfh_indir_size(struct net_device * dev)587 static u32 otx2_get_rxfh_indir_size(struct net_device *dev)
588 {
589 	struct otx2_nic *pfvf = netdev_priv(dev);
590 
591 	return pfvf->hw.rss_info.rss_size;
592 }
593 
594 /* Get RSS configuration */
otx2_get_rxfh(struct net_device * dev,u32 * indir,u8 * hkey,u8 * hfunc)595 static int otx2_get_rxfh(struct net_device *dev, u32 *indir,
596 			 u8 *hkey, u8 *hfunc)
597 {
598 	struct otx2_nic *pfvf = netdev_priv(dev);
599 	struct otx2_rss_info *rss;
600 	int idx;
601 
602 	rss = &pfvf->hw.rss_info;
603 
604 	if (indir) {
605 		for (idx = 0; idx < rss->rss_size; idx++)
606 			indir[idx] = rss->ind_tbl[idx];
607 	}
608 
609 	if (hkey)
610 		memcpy(hkey, rss->key, sizeof(rss->key));
611 
612 	if (hfunc)
613 		*hfunc = ETH_RSS_HASH_TOP;
614 
615 	return 0;
616 }
617 
618 /* Configure RSS table and hash key */
otx2_set_rxfh(struct net_device * dev,const u32 * indir,const u8 * hkey,const u8 hfunc)619 static int otx2_set_rxfh(struct net_device *dev, const u32 *indir,
620 			 const u8 *hkey, const u8 hfunc)
621 {
622 	struct otx2_nic *pfvf = netdev_priv(dev);
623 	struct otx2_rss_info *rss;
624 	int idx;
625 
626 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
627 		return -EOPNOTSUPP;
628 
629 	rss = &pfvf->hw.rss_info;
630 
631 	if (!rss->enable) {
632 		netdev_err(dev, "RSS is disabled, cannot change settings\n");
633 		return -EIO;
634 	}
635 
636 	if (indir) {
637 		for (idx = 0; idx < rss->rss_size; idx++)
638 			rss->ind_tbl[idx] = indir[idx];
639 	}
640 
641 	if (hkey) {
642 		memcpy(rss->key, hkey, sizeof(rss->key));
643 		otx2_set_rss_key(pfvf);
644 	}
645 
646 	otx2_set_rss_table(pfvf);
647 	return 0;
648 }
649 
otx2_get_msglevel(struct net_device * netdev)650 static u32 otx2_get_msglevel(struct net_device *netdev)
651 {
652 	struct otx2_nic *pfvf = netdev_priv(netdev);
653 
654 	return pfvf->msg_enable;
655 }
656 
otx2_set_msglevel(struct net_device * netdev,u32 val)657 static void otx2_set_msglevel(struct net_device *netdev, u32 val)
658 {
659 	struct otx2_nic *pfvf = netdev_priv(netdev);
660 
661 	pfvf->msg_enable = val;
662 }
663 
otx2_get_link(struct net_device * netdev)664 static u32 otx2_get_link(struct net_device *netdev)
665 {
666 	struct otx2_nic *pfvf = netdev_priv(netdev);
667 
668 	/* LBK link is internal and always UP */
669 	if (is_otx2_lbkvf(pfvf->pdev))
670 		return 1;
671 	return pfvf->linfo.link_up;
672 }
673 
otx2_get_ts_info(struct net_device * netdev,struct ethtool_ts_info * info)674 static int otx2_get_ts_info(struct net_device *netdev,
675 			    struct ethtool_ts_info *info)
676 {
677 	struct otx2_nic *pfvf = netdev_priv(netdev);
678 
679 	if (!pfvf->ptp)
680 		return ethtool_op_get_ts_info(netdev, info);
681 
682 	info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
683 				SOF_TIMESTAMPING_RX_SOFTWARE |
684 				SOF_TIMESTAMPING_SOFTWARE |
685 				SOF_TIMESTAMPING_TX_HARDWARE |
686 				SOF_TIMESTAMPING_RX_HARDWARE |
687 				SOF_TIMESTAMPING_RAW_HARDWARE;
688 
689 	info->phc_index = otx2_ptp_clock_index(pfvf);
690 
691 	info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
692 
693 	info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
694 			   (1 << HWTSTAMP_FILTER_ALL);
695 
696 	return 0;
697 }
698 
699 static const struct ethtool_ops otx2_ethtool_ops = {
700 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
701 				     ETHTOOL_COALESCE_MAX_FRAMES,
702 	.get_link		= otx2_get_link,
703 	.get_drvinfo		= otx2_get_drvinfo,
704 	.get_strings		= otx2_get_strings,
705 	.get_ethtool_stats	= otx2_get_ethtool_stats,
706 	.get_sset_count		= otx2_get_sset_count,
707 	.set_channels		= otx2_set_channels,
708 	.get_channels		= otx2_get_channels,
709 	.get_ringparam		= otx2_get_ringparam,
710 	.set_ringparam		= otx2_set_ringparam,
711 	.get_coalesce		= otx2_get_coalesce,
712 	.set_coalesce		= otx2_set_coalesce,
713 	.get_rxnfc		= otx2_get_rxnfc,
714 	.set_rxnfc              = otx2_set_rxnfc,
715 	.get_rxfh_key_size	= otx2_get_rxfh_key_size,
716 	.get_rxfh_indir_size	= otx2_get_rxfh_indir_size,
717 	.get_rxfh		= otx2_get_rxfh,
718 	.set_rxfh		= otx2_set_rxfh,
719 	.get_msglevel		= otx2_get_msglevel,
720 	.set_msglevel		= otx2_set_msglevel,
721 	.get_pauseparam		= otx2_get_pauseparam,
722 	.set_pauseparam		= otx2_set_pauseparam,
723 	.get_ts_info		= otx2_get_ts_info,
724 };
725 
otx2_set_ethtool_ops(struct net_device * netdev)726 void otx2_set_ethtool_ops(struct net_device *netdev)
727 {
728 	netdev->ethtool_ops = &otx2_ethtool_ops;
729 }
730 
731 /* VF's ethtool APIs */
otx2vf_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * info)732 static void otx2vf_get_drvinfo(struct net_device *netdev,
733 			       struct ethtool_drvinfo *info)
734 {
735 	struct otx2_nic *vf = netdev_priv(netdev);
736 
737 	strlcpy(info->driver, DRV_VF_NAME, sizeof(info->driver));
738 	strlcpy(info->bus_info, pci_name(vf->pdev), sizeof(info->bus_info));
739 }
740 
otx2vf_get_strings(struct net_device * netdev,u32 sset,u8 * data)741 static void otx2vf_get_strings(struct net_device *netdev, u32 sset, u8 *data)
742 {
743 	struct otx2_nic *vf = netdev_priv(netdev);
744 	int stats;
745 
746 	if (sset != ETH_SS_STATS)
747 		return;
748 
749 	for (stats = 0; stats < otx2_n_dev_stats; stats++) {
750 		memcpy(data, otx2_dev_stats[stats].name, ETH_GSTRING_LEN);
751 		data += ETH_GSTRING_LEN;
752 	}
753 
754 	for (stats = 0; stats < otx2_n_drv_stats; stats++) {
755 		memcpy(data, otx2_drv_stats[stats].name, ETH_GSTRING_LEN);
756 		data += ETH_GSTRING_LEN;
757 	}
758 
759 	otx2_get_qset_strings(vf, &data, 0);
760 
761 	strcpy(data, "reset_count");
762 	data += ETH_GSTRING_LEN;
763 }
764 
otx2vf_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * data)765 static void otx2vf_get_ethtool_stats(struct net_device *netdev,
766 				     struct ethtool_stats *stats, u64 *data)
767 {
768 	struct otx2_nic *vf = netdev_priv(netdev);
769 	int stat;
770 
771 	otx2_get_dev_stats(vf);
772 	for (stat = 0; stat < otx2_n_dev_stats; stat++)
773 		*(data++) = ((u64 *)&vf->hw.dev_stats)
774 				[otx2_dev_stats[stat].index];
775 
776 	for (stat = 0; stat < otx2_n_drv_stats; stat++)
777 		*(data++) = atomic_read(&((atomic_t *)&vf->hw.drv_stats)
778 						[otx2_drv_stats[stat].index]);
779 
780 	otx2_get_qset_stats(vf, stats, &data);
781 	*(data++) = vf->reset_count;
782 }
783 
otx2vf_get_sset_count(struct net_device * netdev,int sset)784 static int otx2vf_get_sset_count(struct net_device *netdev, int sset)
785 {
786 	struct otx2_nic *vf = netdev_priv(netdev);
787 	int qstats_count;
788 
789 	if (sset != ETH_SS_STATS)
790 		return -EINVAL;
791 
792 	qstats_count = otx2_n_queue_stats *
793 		       (vf->hw.rx_queues + vf->hw.tx_queues);
794 
795 	return otx2_n_dev_stats + otx2_n_drv_stats + qstats_count + 1;
796 }
797 
798 static const struct ethtool_ops otx2vf_ethtool_ops = {
799 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
800 				     ETHTOOL_COALESCE_MAX_FRAMES,
801 	.get_link		= otx2_get_link,
802 	.get_drvinfo		= otx2vf_get_drvinfo,
803 	.get_strings		= otx2vf_get_strings,
804 	.get_ethtool_stats	= otx2vf_get_ethtool_stats,
805 	.get_sset_count		= otx2vf_get_sset_count,
806 	.set_channels		= otx2_set_channels,
807 	.get_channels		= otx2_get_channels,
808 	.get_rxnfc		= otx2_get_rxnfc,
809 	.set_rxnfc              = otx2_set_rxnfc,
810 	.get_rxfh_key_size	= otx2_get_rxfh_key_size,
811 	.get_rxfh_indir_size	= otx2_get_rxfh_indir_size,
812 	.get_rxfh		= otx2_get_rxfh,
813 	.set_rxfh		= otx2_set_rxfh,
814 	.get_ringparam		= otx2_get_ringparam,
815 	.set_ringparam		= otx2_set_ringparam,
816 	.get_coalesce		= otx2_get_coalesce,
817 	.set_coalesce		= otx2_set_coalesce,
818 	.get_msglevel		= otx2_get_msglevel,
819 	.set_msglevel		= otx2_set_msglevel,
820 	.get_pauseparam		= otx2_get_pauseparam,
821 	.set_pauseparam		= otx2_set_pauseparam,
822 };
823 
otx2vf_set_ethtool_ops(struct net_device * netdev)824 void otx2vf_set_ethtool_ops(struct net_device *netdev)
825 {
826 	netdev->ethtool_ops = &otx2vf_ethtool_ops;
827 }
828 EXPORT_SYMBOL(otx2vf_set_ethtool_ops);
829