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