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