1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
3 
4 #include <linux/module.h>
5 #include <linux/netdevice.h>
6 
7 #include "ionic.h"
8 #include "ionic_bus.h"
9 #include "ionic_lif.h"
10 #include "ionic_ethtool.h"
11 #include "ionic_stats.h"
12 
13 static const char ionic_priv_flags_strings[][ETH_GSTRING_LEN] = {
14 #define PRIV_F_SW_DBG_STATS		BIT(0)
15 	"sw-dbg-stats",
16 };
17 #define PRIV_FLAGS_COUNT ARRAY_SIZE(ionic_priv_flags_strings)
18 
19 static void ionic_get_stats_strings(struct ionic_lif *lif, u8 *buf)
20 {
21 	u32 i;
22 
23 	for (i = 0; i < ionic_num_stats_grps; i++)
24 		ionic_stats_groups[i].get_strings(lif, &buf);
25 }
26 
27 static void ionic_get_stats(struct net_device *netdev,
28 			    struct ethtool_stats *stats, u64 *buf)
29 {
30 	struct ionic_lif *lif;
31 	u32 i;
32 
33 	lif = netdev_priv(netdev);
34 
35 	memset(buf, 0, stats->n_stats * sizeof(*buf));
36 	for (i = 0; i < ionic_num_stats_grps; i++)
37 		ionic_stats_groups[i].get_values(lif, &buf);
38 }
39 
40 static int ionic_get_stats_count(struct ionic_lif *lif)
41 {
42 	int i, num_stats = 0;
43 
44 	for (i = 0; i < ionic_num_stats_grps; i++)
45 		num_stats += ionic_stats_groups[i].get_count(lif);
46 
47 	return num_stats;
48 }
49 
50 static int ionic_get_sset_count(struct net_device *netdev, int sset)
51 {
52 	struct ionic_lif *lif = netdev_priv(netdev);
53 	int count = 0;
54 
55 	switch (sset) {
56 	case ETH_SS_STATS:
57 		count = ionic_get_stats_count(lif);
58 		break;
59 	case ETH_SS_PRIV_FLAGS:
60 		count = PRIV_FLAGS_COUNT;
61 		break;
62 	}
63 	return count;
64 }
65 
66 static void ionic_get_strings(struct net_device *netdev,
67 			      u32 sset, u8 *buf)
68 {
69 	struct ionic_lif *lif = netdev_priv(netdev);
70 
71 	switch (sset) {
72 	case ETH_SS_STATS:
73 		ionic_get_stats_strings(lif, buf);
74 		break;
75 	case ETH_SS_PRIV_FLAGS:
76 		memcpy(buf, ionic_priv_flags_strings,
77 		       PRIV_FLAGS_COUNT * ETH_GSTRING_LEN);
78 		break;
79 	}
80 }
81 
82 static void ionic_get_drvinfo(struct net_device *netdev,
83 			      struct ethtool_drvinfo *drvinfo)
84 {
85 	struct ionic_lif *lif = netdev_priv(netdev);
86 	struct ionic *ionic = lif->ionic;
87 
88 	strlcpy(drvinfo->driver, IONIC_DRV_NAME, sizeof(drvinfo->driver));
89 	strlcpy(drvinfo->fw_version, ionic->idev.dev_info.fw_version,
90 		sizeof(drvinfo->fw_version));
91 	strlcpy(drvinfo->bus_info, ionic_bus_info(ionic),
92 		sizeof(drvinfo->bus_info));
93 }
94 
95 static int ionic_get_regs_len(struct net_device *netdev)
96 {
97 	return (IONIC_DEV_INFO_REG_COUNT + IONIC_DEV_CMD_REG_COUNT) * sizeof(u32);
98 }
99 
100 static void ionic_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
101 			   void *p)
102 {
103 	struct ionic_lif *lif = netdev_priv(netdev);
104 	unsigned int size;
105 
106 	regs->version = IONIC_DEV_CMD_REG_VERSION;
107 
108 	size = IONIC_DEV_INFO_REG_COUNT * sizeof(u32);
109 	memcpy_fromio(p, lif->ionic->idev.dev_info_regs->words, size);
110 
111 	size = IONIC_DEV_CMD_REG_COUNT * sizeof(u32);
112 	memcpy_fromio(p, lif->ionic->idev.dev_cmd_regs->words, size);
113 }
114 
115 static int ionic_get_link_ksettings(struct net_device *netdev,
116 				    struct ethtool_link_ksettings *ks)
117 {
118 	struct ionic_lif *lif = netdev_priv(netdev);
119 	struct ionic_dev *idev = &lif->ionic->idev;
120 	int copper_seen = 0;
121 
122 	ethtool_link_ksettings_zero_link_mode(ks, supported);
123 
124 	/* The port_info data is found in a DMA space that the NIC keeps
125 	 * up-to-date, so there's no need to request the data from the
126 	 * NIC, we already have it in our memory space.
127 	 */
128 
129 	switch (le16_to_cpu(idev->port_info->status.xcvr.pid)) {
130 		/* Copper */
131 	case IONIC_XCVR_PID_QSFP_100G_CR4:
132 		ethtool_link_ksettings_add_link_mode(ks, supported,
133 						     100000baseCR4_Full);
134 		copper_seen++;
135 		break;
136 	case IONIC_XCVR_PID_QSFP_40GBASE_CR4:
137 		ethtool_link_ksettings_add_link_mode(ks, supported,
138 						     40000baseCR4_Full);
139 		copper_seen++;
140 		break;
141 	case IONIC_XCVR_PID_SFP_25GBASE_CR_S:
142 	case IONIC_XCVR_PID_SFP_25GBASE_CR_L:
143 	case IONIC_XCVR_PID_SFP_25GBASE_CR_N:
144 		ethtool_link_ksettings_add_link_mode(ks, supported,
145 						     25000baseCR_Full);
146 		copper_seen++;
147 		break;
148 	case IONIC_XCVR_PID_SFP_10GBASE_AOC:
149 	case IONIC_XCVR_PID_SFP_10GBASE_CU:
150 		ethtool_link_ksettings_add_link_mode(ks, supported,
151 						     10000baseCR_Full);
152 		copper_seen++;
153 		break;
154 
155 		/* Fibre */
156 	case IONIC_XCVR_PID_QSFP_100G_SR4:
157 	case IONIC_XCVR_PID_QSFP_100G_AOC:
158 		ethtool_link_ksettings_add_link_mode(ks, supported,
159 						     100000baseSR4_Full);
160 		break;
161 	case IONIC_XCVR_PID_QSFP_100G_LR4:
162 		ethtool_link_ksettings_add_link_mode(ks, supported,
163 						     100000baseLR4_ER4_Full);
164 		break;
165 	case IONIC_XCVR_PID_QSFP_100G_ER4:
166 		ethtool_link_ksettings_add_link_mode(ks, supported,
167 						     100000baseLR4_ER4_Full);
168 		break;
169 	case IONIC_XCVR_PID_QSFP_40GBASE_SR4:
170 	case IONIC_XCVR_PID_QSFP_40GBASE_AOC:
171 		ethtool_link_ksettings_add_link_mode(ks, supported,
172 						     40000baseSR4_Full);
173 		break;
174 	case IONIC_XCVR_PID_QSFP_40GBASE_LR4:
175 		ethtool_link_ksettings_add_link_mode(ks, supported,
176 						     40000baseLR4_Full);
177 		break;
178 	case IONIC_XCVR_PID_SFP_25GBASE_SR:
179 	case IONIC_XCVR_PID_SFP_25GBASE_AOC:
180 		ethtool_link_ksettings_add_link_mode(ks, supported,
181 						     25000baseSR_Full);
182 		break;
183 	case IONIC_XCVR_PID_SFP_10GBASE_SR:
184 		ethtool_link_ksettings_add_link_mode(ks, supported,
185 						     10000baseSR_Full);
186 		break;
187 	case IONIC_XCVR_PID_SFP_10GBASE_LR:
188 		ethtool_link_ksettings_add_link_mode(ks, supported,
189 						     10000baseLR_Full);
190 		break;
191 	case IONIC_XCVR_PID_SFP_10GBASE_LRM:
192 		ethtool_link_ksettings_add_link_mode(ks, supported,
193 						     10000baseLRM_Full);
194 		break;
195 	case IONIC_XCVR_PID_SFP_10GBASE_ER:
196 		ethtool_link_ksettings_add_link_mode(ks, supported,
197 						     10000baseER_Full);
198 		break;
199 	case IONIC_XCVR_PID_UNKNOWN:
200 		/* This means there's no module plugged in */
201 		break;
202 	default:
203 		dev_info(lif->ionic->dev, "unknown xcvr type pid=%d / 0x%x\n",
204 			 idev->port_info->status.xcvr.pid,
205 			 idev->port_info->status.xcvr.pid);
206 		break;
207 	}
208 
209 	bitmap_copy(ks->link_modes.advertising, ks->link_modes.supported,
210 		    __ETHTOOL_LINK_MODE_MASK_NBITS);
211 
212 	ethtool_link_ksettings_add_link_mode(ks, supported, FEC_BASER);
213 	ethtool_link_ksettings_add_link_mode(ks, supported, FEC_RS);
214 	if (idev->port_info->config.fec_type == IONIC_PORT_FEC_TYPE_FC)
215 		ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_BASER);
216 	else if (idev->port_info->config.fec_type == IONIC_PORT_FEC_TYPE_RS)
217 		ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_RS);
218 
219 	ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
220 	ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
221 
222 	if (idev->port_info->status.xcvr.phy == IONIC_PHY_TYPE_COPPER ||
223 	    copper_seen)
224 		ks->base.port = PORT_DA;
225 	else if (idev->port_info->status.xcvr.phy == IONIC_PHY_TYPE_FIBER)
226 		ks->base.port = PORT_FIBRE;
227 	else
228 		ks->base.port = PORT_NONE;
229 
230 	if (ks->base.port != PORT_NONE) {
231 		ks->base.speed = le32_to_cpu(lif->info->status.link_speed);
232 
233 		if (le16_to_cpu(lif->info->status.link_status))
234 			ks->base.duplex = DUPLEX_FULL;
235 		else
236 			ks->base.duplex = DUPLEX_UNKNOWN;
237 
238 		ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
239 
240 		if (idev->port_info->config.an_enable) {
241 			ethtool_link_ksettings_add_link_mode(ks, advertising,
242 							     Autoneg);
243 			ks->base.autoneg = AUTONEG_ENABLE;
244 		}
245 	}
246 
247 	return 0;
248 }
249 
250 static int ionic_set_link_ksettings(struct net_device *netdev,
251 				    const struct ethtool_link_ksettings *ks)
252 {
253 	struct ionic_lif *lif = netdev_priv(netdev);
254 	struct ionic *ionic = lif->ionic;
255 	struct ionic_dev *idev;
256 	int err = 0;
257 
258 	idev = &lif->ionic->idev;
259 
260 	/* set autoneg */
261 	if (ks->base.autoneg != idev->port_info->config.an_enable) {
262 		mutex_lock(&ionic->dev_cmd_lock);
263 		ionic_dev_cmd_port_autoneg(idev, ks->base.autoneg);
264 		err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
265 		mutex_unlock(&ionic->dev_cmd_lock);
266 		if (err)
267 			return err;
268 	}
269 
270 	/* set speed */
271 	if (ks->base.speed != le32_to_cpu(idev->port_info->config.speed)) {
272 		mutex_lock(&ionic->dev_cmd_lock);
273 		ionic_dev_cmd_port_speed(idev, ks->base.speed);
274 		err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
275 		mutex_unlock(&ionic->dev_cmd_lock);
276 		if (err)
277 			return err;
278 	}
279 
280 	return 0;
281 }
282 
283 static void ionic_get_pauseparam(struct net_device *netdev,
284 				 struct ethtool_pauseparam *pause)
285 {
286 	struct ionic_lif *lif = netdev_priv(netdev);
287 	u8 pause_type;
288 
289 	pause->autoneg = 0;
290 
291 	pause_type = lif->ionic->idev.port_info->config.pause_type;
292 	if (pause_type) {
293 		pause->rx_pause = pause_type & IONIC_PAUSE_F_RX ? 1 : 0;
294 		pause->tx_pause = pause_type & IONIC_PAUSE_F_TX ? 1 : 0;
295 	}
296 }
297 
298 static int ionic_set_pauseparam(struct net_device *netdev,
299 				struct ethtool_pauseparam *pause)
300 {
301 	struct ionic_lif *lif = netdev_priv(netdev);
302 	struct ionic *ionic = lif->ionic;
303 	u32 requested_pause;
304 	int err;
305 
306 	if (pause->autoneg)
307 		return -EOPNOTSUPP;
308 
309 	/* change both at the same time */
310 	requested_pause = IONIC_PORT_PAUSE_TYPE_LINK;
311 	if (pause->rx_pause)
312 		requested_pause |= IONIC_PAUSE_F_RX;
313 	if (pause->tx_pause)
314 		requested_pause |= IONIC_PAUSE_F_TX;
315 
316 	if (requested_pause == lif->ionic->idev.port_info->config.pause_type)
317 		return 0;
318 
319 	mutex_lock(&ionic->dev_cmd_lock);
320 	ionic_dev_cmd_port_pause(&lif->ionic->idev, requested_pause);
321 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
322 	mutex_unlock(&ionic->dev_cmd_lock);
323 	if (err)
324 		return err;
325 
326 	return 0;
327 }
328 
329 static int ionic_get_fecparam(struct net_device *netdev,
330 			      struct ethtool_fecparam *fec)
331 {
332 	struct ionic_lif *lif = netdev_priv(netdev);
333 
334 	switch (lif->ionic->idev.port_info->config.fec_type) {
335 	case IONIC_PORT_FEC_TYPE_NONE:
336 		fec->active_fec = ETHTOOL_FEC_OFF;
337 		break;
338 	case IONIC_PORT_FEC_TYPE_RS:
339 		fec->active_fec = ETHTOOL_FEC_RS;
340 		break;
341 	case IONIC_PORT_FEC_TYPE_FC:
342 		fec->active_fec = ETHTOOL_FEC_BASER;
343 		break;
344 	}
345 
346 	fec->fec = ETHTOOL_FEC_OFF | ETHTOOL_FEC_RS | ETHTOOL_FEC_BASER;
347 
348 	return 0;
349 }
350 
351 static int ionic_set_fecparam(struct net_device *netdev,
352 			      struct ethtool_fecparam *fec)
353 {
354 	struct ionic_lif *lif = netdev_priv(netdev);
355 	u8 fec_type;
356 	int ret = 0;
357 
358 	if (lif->ionic->idev.port_info->config.an_enable) {
359 		netdev_err(netdev, "FEC request not allowed while autoneg is enabled\n");
360 		return -EINVAL;
361 	}
362 
363 	switch (fec->fec) {
364 	case ETHTOOL_FEC_NONE:
365 		fec_type = IONIC_PORT_FEC_TYPE_NONE;
366 		break;
367 	case ETHTOOL_FEC_OFF:
368 		fec_type = IONIC_PORT_FEC_TYPE_NONE;
369 		break;
370 	case ETHTOOL_FEC_RS:
371 		fec_type = IONIC_PORT_FEC_TYPE_RS;
372 		break;
373 	case ETHTOOL_FEC_BASER:
374 		fec_type = IONIC_PORT_FEC_TYPE_FC;
375 		break;
376 	case ETHTOOL_FEC_AUTO:
377 	default:
378 		netdev_err(netdev, "FEC request 0x%04x not supported\n",
379 			   fec->fec);
380 		return -EINVAL;
381 	}
382 
383 	if (fec_type != lif->ionic->idev.port_info->config.fec_type) {
384 		mutex_lock(&lif->ionic->dev_cmd_lock);
385 		ionic_dev_cmd_port_fec(&lif->ionic->idev, fec_type);
386 		ret = ionic_dev_cmd_wait(lif->ionic, DEVCMD_TIMEOUT);
387 		mutex_unlock(&lif->ionic->dev_cmd_lock);
388 	}
389 
390 	return ret;
391 }
392 
393 static int ionic_get_coalesce(struct net_device *netdev,
394 			      struct ethtool_coalesce *coalesce)
395 {
396 	struct ionic_lif *lif = netdev_priv(netdev);
397 
398 	/* Tx uses Rx interrupt */
399 	coalesce->tx_coalesce_usecs = lif->rx_coalesce_usecs;
400 	coalesce->rx_coalesce_usecs = lif->rx_coalesce_usecs;
401 
402 	return 0;
403 }
404 
405 static int ionic_set_coalesce(struct net_device *netdev,
406 			      struct ethtool_coalesce *coalesce)
407 {
408 	struct ionic_lif *lif = netdev_priv(netdev);
409 	struct ionic_identity *ident;
410 	struct ionic_qcq *qcq;
411 	unsigned int i;
412 	u32 coal;
413 
414 	ident = &lif->ionic->ident;
415 	if (ident->dev.intr_coal_div == 0) {
416 		netdev_warn(netdev, "bad HW value in dev.intr_coal_div = %d\n",
417 			    ident->dev.intr_coal_div);
418 		return -EIO;
419 	}
420 
421 	/* Tx uses Rx interrupt, so only change Rx */
422 	if (coalesce->tx_coalesce_usecs != lif->rx_coalesce_usecs) {
423 		netdev_warn(netdev, "only the rx-usecs can be changed\n");
424 		return -EINVAL;
425 	}
426 
427 	/* Convert the usec request to a HW useable value.  If they asked
428 	 * for non-zero and it resolved to zero, bump it up
429 	 */
430 	coal = ionic_coal_usec_to_hw(lif->ionic, coalesce->rx_coalesce_usecs);
431 	if (!coal && coalesce->rx_coalesce_usecs)
432 		coal = 1;
433 
434 	if (coal > IONIC_INTR_CTRL_COAL_MAX)
435 		return -ERANGE;
436 
437 	/* Save the new value */
438 	lif->rx_coalesce_usecs = coalesce->rx_coalesce_usecs;
439 	if (coal != lif->rx_coalesce_hw) {
440 		lif->rx_coalesce_hw = coal;
441 
442 		if (test_bit(IONIC_LIF_F_UP, lif->state)) {
443 			for (i = 0; i < lif->nxqs; i++) {
444 				qcq = lif->rxqcqs[i].qcq;
445 				ionic_intr_coal_init(lif->ionic->idev.intr_ctrl,
446 						     qcq->intr.index,
447 						     lif->rx_coalesce_hw);
448 			}
449 		}
450 	}
451 
452 	return 0;
453 }
454 
455 static void ionic_get_ringparam(struct net_device *netdev,
456 				struct ethtool_ringparam *ring)
457 {
458 	struct ionic_lif *lif = netdev_priv(netdev);
459 
460 	ring->tx_max_pending = IONIC_MAX_TXRX_DESC;
461 	ring->tx_pending = lif->ntxq_descs;
462 	ring->rx_max_pending = IONIC_MAX_TXRX_DESC;
463 	ring->rx_pending = lif->nrxq_descs;
464 }
465 
466 static int ionic_set_ringparam(struct net_device *netdev,
467 			       struct ethtool_ringparam *ring)
468 {
469 	struct ionic_lif *lif = netdev_priv(netdev);
470 	bool running;
471 	int err;
472 
473 	if (ring->rx_mini_pending || ring->rx_jumbo_pending) {
474 		netdev_info(netdev, "Changing jumbo or mini descriptors not supported\n");
475 		return -EINVAL;
476 	}
477 
478 	if (!is_power_of_2(ring->tx_pending) ||
479 	    !is_power_of_2(ring->rx_pending)) {
480 		netdev_info(netdev, "Descriptor count must be a power of 2\n");
481 		return -EINVAL;
482 	}
483 
484 	/* if nothing to do return success */
485 	if (ring->tx_pending == lif->ntxq_descs &&
486 	    ring->rx_pending == lif->nrxq_descs)
487 		return 0;
488 
489 	err = ionic_wait_for_bit(lif, IONIC_LIF_F_QUEUE_RESET);
490 	if (err)
491 		return err;
492 
493 	running = test_bit(IONIC_LIF_F_UP, lif->state);
494 	if (running)
495 		ionic_stop(netdev);
496 
497 	lif->ntxq_descs = ring->tx_pending;
498 	lif->nrxq_descs = ring->rx_pending;
499 
500 	if (running)
501 		ionic_open(netdev);
502 	clear_bit(IONIC_LIF_F_QUEUE_RESET, lif->state);
503 
504 	return 0;
505 }
506 
507 static void ionic_get_channels(struct net_device *netdev,
508 			       struct ethtool_channels *ch)
509 {
510 	struct ionic_lif *lif = netdev_priv(netdev);
511 
512 	/* report maximum channels */
513 	ch->max_combined = lif->ionic->ntxqs_per_lif;
514 
515 	/* report current channels */
516 	ch->combined_count = lif->nxqs;
517 }
518 
519 static int ionic_set_channels(struct net_device *netdev,
520 			      struct ethtool_channels *ch)
521 {
522 	struct ionic_lif *lif = netdev_priv(netdev);
523 	bool running;
524 	int err;
525 
526 	if (!ch->combined_count || ch->other_count ||
527 	    ch->rx_count || ch->tx_count)
528 		return -EINVAL;
529 
530 	if (ch->combined_count == lif->nxqs)
531 		return 0;
532 
533 	err = ionic_wait_for_bit(lif, IONIC_LIF_F_QUEUE_RESET);
534 	if (err)
535 		return err;
536 
537 	running = test_bit(IONIC_LIF_F_UP, lif->state);
538 	if (running)
539 		ionic_stop(netdev);
540 
541 	lif->nxqs = ch->combined_count;
542 
543 	if (running)
544 		ionic_open(netdev);
545 	clear_bit(IONIC_LIF_F_QUEUE_RESET, lif->state);
546 
547 	return 0;
548 }
549 
550 static u32 ionic_get_priv_flags(struct net_device *netdev)
551 {
552 	struct ionic_lif *lif = netdev_priv(netdev);
553 	u32 priv_flags = 0;
554 
555 	if (test_bit(IONIC_LIF_F_SW_DEBUG_STATS, lif->state))
556 		priv_flags |= PRIV_F_SW_DBG_STATS;
557 
558 	return priv_flags;
559 }
560 
561 static int ionic_set_priv_flags(struct net_device *netdev, u32 priv_flags)
562 {
563 	struct ionic_lif *lif = netdev_priv(netdev);
564 
565 	clear_bit(IONIC_LIF_F_SW_DEBUG_STATS, lif->state);
566 	if (priv_flags & PRIV_F_SW_DBG_STATS)
567 		set_bit(IONIC_LIF_F_SW_DEBUG_STATS, lif->state);
568 
569 	return 0;
570 }
571 
572 static int ionic_get_rxnfc(struct net_device *netdev,
573 			   struct ethtool_rxnfc *info, u32 *rules)
574 {
575 	struct ionic_lif *lif = netdev_priv(netdev);
576 	int err = 0;
577 
578 	switch (info->cmd) {
579 	case ETHTOOL_GRXRINGS:
580 		info->data = lif->nxqs;
581 		break;
582 	default:
583 		netdev_err(netdev, "Command parameter %d is not supported\n",
584 			   info->cmd);
585 		err = -EOPNOTSUPP;
586 	}
587 
588 	return err;
589 }
590 
591 static u32 ionic_get_rxfh_indir_size(struct net_device *netdev)
592 {
593 	struct ionic_lif *lif = netdev_priv(netdev);
594 
595 	return le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
596 }
597 
598 static u32 ionic_get_rxfh_key_size(struct net_device *netdev)
599 {
600 	return IONIC_RSS_HASH_KEY_SIZE;
601 }
602 
603 static int ionic_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
604 			  u8 *hfunc)
605 {
606 	struct ionic_lif *lif = netdev_priv(netdev);
607 	unsigned int i, tbl_sz;
608 
609 	if (indir) {
610 		tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
611 		for (i = 0; i < tbl_sz; i++)
612 			indir[i] = lif->rss_ind_tbl[i];
613 	}
614 
615 	if (key)
616 		memcpy(key, lif->rss_hash_key, IONIC_RSS_HASH_KEY_SIZE);
617 
618 	if (hfunc)
619 		*hfunc = ETH_RSS_HASH_TOP;
620 
621 	return 0;
622 }
623 
624 static int ionic_set_rxfh(struct net_device *netdev, const u32 *indir,
625 			  const u8 *key, const u8 hfunc)
626 {
627 	struct ionic_lif *lif = netdev_priv(netdev);
628 	int err;
629 
630 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
631 		return -EOPNOTSUPP;
632 
633 	err = ionic_lif_rss_config(lif, lif->rss_types, key, indir);
634 	if (err)
635 		return err;
636 
637 	return 0;
638 }
639 
640 static int ionic_set_tunable(struct net_device *dev,
641 			     const struct ethtool_tunable *tuna,
642 			     const void *data)
643 {
644 	struct ionic_lif *lif = netdev_priv(dev);
645 
646 	switch (tuna->id) {
647 	case ETHTOOL_RX_COPYBREAK:
648 		lif->rx_copybreak = *(u32 *)data;
649 		break;
650 	default:
651 		return -EOPNOTSUPP;
652 	}
653 
654 	return 0;
655 }
656 
657 static int ionic_get_tunable(struct net_device *netdev,
658 			     const struct ethtool_tunable *tuna, void *data)
659 {
660 	struct ionic_lif *lif = netdev_priv(netdev);
661 
662 	switch (tuna->id) {
663 	case ETHTOOL_RX_COPYBREAK:
664 		*(u32 *)data = lif->rx_copybreak;
665 		break;
666 	default:
667 		return -EOPNOTSUPP;
668 	}
669 
670 	return 0;
671 }
672 
673 static int ionic_get_module_info(struct net_device *netdev,
674 				 struct ethtool_modinfo *modinfo)
675 
676 {
677 	struct ionic_lif *lif = netdev_priv(netdev);
678 	struct ionic_dev *idev = &lif->ionic->idev;
679 	struct ionic_xcvr_status *xcvr;
680 
681 	xcvr = &idev->port_info->status.xcvr;
682 
683 	/* report the module data type and length */
684 	switch (xcvr->sprom[0]) {
685 	case 0x03: /* SFP */
686 		modinfo->type = ETH_MODULE_SFF_8079;
687 		modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
688 		break;
689 	case 0x0D: /* QSFP */
690 	case 0x11: /* QSFP28 */
691 		modinfo->type = ETH_MODULE_SFF_8436;
692 		modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
693 		break;
694 	default:
695 		netdev_info(netdev, "unknown xcvr type 0x%02x\n",
696 			    xcvr->sprom[0]);
697 		break;
698 	}
699 
700 	return 0;
701 }
702 
703 static int ionic_get_module_eeprom(struct net_device *netdev,
704 				   struct ethtool_eeprom *ee,
705 				   u8 *data)
706 {
707 	struct ionic_lif *lif = netdev_priv(netdev);
708 	struct ionic_dev *idev = &lif->ionic->idev;
709 	struct ionic_xcvr_status *xcvr;
710 	char tbuf[sizeof(xcvr->sprom)];
711 	int count = 10;
712 	u32 len;
713 
714 	/* The NIC keeps the module prom up-to-date in the DMA space
715 	 * so we can simply copy the module bytes into the data buffer.
716 	 */
717 	xcvr = &idev->port_info->status.xcvr;
718 	len = min_t(u32, sizeof(xcvr->sprom), ee->len);
719 
720 	do {
721 		memcpy(data, xcvr->sprom, len);
722 		memcpy(tbuf, xcvr->sprom, len);
723 
724 		/* Let's make sure we got a consistent copy */
725 		if (!memcmp(data, tbuf, len))
726 			break;
727 
728 	} while (--count);
729 
730 	if (!count)
731 		return -ETIMEDOUT;
732 
733 	return 0;
734 }
735 
736 static int ionic_nway_reset(struct net_device *netdev)
737 {
738 	struct ionic_lif *lif = netdev_priv(netdev);
739 	struct ionic *ionic = lif->ionic;
740 	int err = 0;
741 
742 	/* flap the link to force auto-negotiation */
743 
744 	mutex_lock(&ionic->dev_cmd_lock);
745 
746 	ionic_dev_cmd_port_state(&ionic->idev, IONIC_PORT_ADMIN_STATE_DOWN);
747 	err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
748 
749 	if (!err) {
750 		ionic_dev_cmd_port_state(&ionic->idev, IONIC_PORT_ADMIN_STATE_UP);
751 		err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
752 	}
753 
754 	mutex_unlock(&ionic->dev_cmd_lock);
755 
756 	return err;
757 }
758 
759 static const struct ethtool_ops ionic_ethtool_ops = {
760 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS,
761 	.get_drvinfo		= ionic_get_drvinfo,
762 	.get_regs_len		= ionic_get_regs_len,
763 	.get_regs		= ionic_get_regs,
764 	.get_link		= ethtool_op_get_link,
765 	.get_link_ksettings	= ionic_get_link_ksettings,
766 	.set_link_ksettings	= ionic_set_link_ksettings,
767 	.get_coalesce		= ionic_get_coalesce,
768 	.set_coalesce		= ionic_set_coalesce,
769 	.get_ringparam		= ionic_get_ringparam,
770 	.set_ringparam		= ionic_set_ringparam,
771 	.get_channels		= ionic_get_channels,
772 	.set_channels		= ionic_set_channels,
773 	.get_strings		= ionic_get_strings,
774 	.get_ethtool_stats	= ionic_get_stats,
775 	.get_sset_count		= ionic_get_sset_count,
776 	.get_priv_flags		= ionic_get_priv_flags,
777 	.set_priv_flags		= ionic_set_priv_flags,
778 	.get_rxnfc		= ionic_get_rxnfc,
779 	.get_rxfh_indir_size	= ionic_get_rxfh_indir_size,
780 	.get_rxfh_key_size	= ionic_get_rxfh_key_size,
781 	.get_rxfh		= ionic_get_rxfh,
782 	.set_rxfh		= ionic_set_rxfh,
783 	.get_tunable		= ionic_get_tunable,
784 	.set_tunable		= ionic_set_tunable,
785 	.get_module_info	= ionic_get_module_info,
786 	.get_module_eeprom	= ionic_get_module_eeprom,
787 	.get_pauseparam		= ionic_get_pauseparam,
788 	.set_pauseparam		= ionic_set_pauseparam,
789 	.get_fecparam		= ionic_get_fecparam,
790 	.set_fecparam		= ionic_set_fecparam,
791 	.nway_reset		= ionic_nway_reset,
792 };
793 
794 void ionic_ethtool_set_ops(struct net_device *netdev)
795 {
796 	netdev->ethtool_ops = &ionic_ethtool_ops;
797 }
798