Merge tag 'perf-urgent-2020-06-28' of git://git.kernel.org/pub/scm/linux/kernel/git...
[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 int ionic_set_ringparam(struct net_device *netdev,
472                                struct ethtool_ringparam *ring)
473 {
474         struct ionic_lif *lif = netdev_priv(netdev);
475         bool running;
476         int err;
477
478         if (ring->rx_mini_pending || ring->rx_jumbo_pending) {
479                 netdev_info(netdev, "Changing jumbo or mini descriptors not supported\n");
480                 return -EINVAL;
481         }
482
483         if (!is_power_of_2(ring->tx_pending) ||
484             !is_power_of_2(ring->rx_pending)) {
485                 netdev_info(netdev, "Descriptor count must be a power of 2\n");
486                 return -EINVAL;
487         }
488
489         /* if nothing to do return success */
490         if (ring->tx_pending == lif->ntxq_descs &&
491             ring->rx_pending == lif->nrxq_descs)
492                 return 0;
493
494         err = ionic_wait_for_bit(lif, IONIC_LIF_F_QUEUE_RESET);
495         if (err)
496                 return err;
497
498         running = test_bit(IONIC_LIF_F_UP, lif->state);
499         if (running)
500                 ionic_stop(netdev);
501
502         lif->ntxq_descs = ring->tx_pending;
503         lif->nrxq_descs = ring->rx_pending;
504
505         if (running)
506                 ionic_open(netdev);
507         clear_bit(IONIC_LIF_F_QUEUE_RESET, lif->state);
508
509         return 0;
510 }
511
512 static void ionic_get_channels(struct net_device *netdev,
513                                struct ethtool_channels *ch)
514 {
515         struct ionic_lif *lif = netdev_priv(netdev);
516
517         /* report maximum channels */
518         ch->max_combined = lif->ionic->ntxqs_per_lif;
519
520         /* report current channels */
521         ch->combined_count = lif->nxqs;
522 }
523
524 static int ionic_set_channels(struct net_device *netdev,
525                               struct ethtool_channels *ch)
526 {
527         struct ionic_lif *lif = netdev_priv(netdev);
528         bool running;
529         int err;
530
531         if (!ch->combined_count || ch->other_count ||
532             ch->rx_count || ch->tx_count)
533                 return -EINVAL;
534
535         if (ch->combined_count == lif->nxqs)
536                 return 0;
537
538         err = ionic_wait_for_bit(lif, IONIC_LIF_F_QUEUE_RESET);
539         if (err)
540                 return err;
541
542         running = test_bit(IONIC_LIF_F_UP, lif->state);
543         if (running)
544                 ionic_stop(netdev);
545
546         lif->nxqs = ch->combined_count;
547
548         if (running)
549                 ionic_open(netdev);
550         clear_bit(IONIC_LIF_F_QUEUE_RESET, lif->state);
551
552         return 0;
553 }
554
555 static u32 ionic_get_priv_flags(struct net_device *netdev)
556 {
557         struct ionic_lif *lif = netdev_priv(netdev);
558         u32 priv_flags = 0;
559
560         if (test_bit(IONIC_LIF_F_SW_DEBUG_STATS, lif->state))
561                 priv_flags |= IONIC_PRIV_F_SW_DBG_STATS;
562
563         return priv_flags;
564 }
565
566 static int ionic_set_priv_flags(struct net_device *netdev, u32 priv_flags)
567 {
568         struct ionic_lif *lif = netdev_priv(netdev);
569
570         clear_bit(IONIC_LIF_F_SW_DEBUG_STATS, lif->state);
571         if (priv_flags & IONIC_PRIV_F_SW_DBG_STATS)
572                 set_bit(IONIC_LIF_F_SW_DEBUG_STATS, lif->state);
573
574         return 0;
575 }
576
577 static int ionic_get_rxnfc(struct net_device *netdev,
578                            struct ethtool_rxnfc *info, u32 *rules)
579 {
580         struct ionic_lif *lif = netdev_priv(netdev);
581         int err = 0;
582
583         switch (info->cmd) {
584         case ETHTOOL_GRXRINGS:
585                 info->data = lif->nxqs;
586                 break;
587         default:
588                 netdev_err(netdev, "Command parameter %d is not supported\n",
589                            info->cmd);
590                 err = -EOPNOTSUPP;
591         }
592
593         return err;
594 }
595
596 static u32 ionic_get_rxfh_indir_size(struct net_device *netdev)
597 {
598         struct ionic_lif *lif = netdev_priv(netdev);
599
600         return le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
601 }
602
603 static u32 ionic_get_rxfh_key_size(struct net_device *netdev)
604 {
605         return IONIC_RSS_HASH_KEY_SIZE;
606 }
607
608 static int ionic_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
609                           u8 *hfunc)
610 {
611         struct ionic_lif *lif = netdev_priv(netdev);
612         unsigned int i, tbl_sz;
613
614         if (indir) {
615                 tbl_sz = le16_to_cpu(lif->ionic->ident.lif.eth.rss_ind_tbl_sz);
616                 for (i = 0; i < tbl_sz; i++)
617                         indir[i] = lif->rss_ind_tbl[i];
618         }
619
620         if (key)
621                 memcpy(key, lif->rss_hash_key, IONIC_RSS_HASH_KEY_SIZE);
622
623         if (hfunc)
624                 *hfunc = ETH_RSS_HASH_TOP;
625
626         return 0;
627 }
628
629 static int ionic_set_rxfh(struct net_device *netdev, const u32 *indir,
630                           const u8 *key, const u8 hfunc)
631 {
632         struct ionic_lif *lif = netdev_priv(netdev);
633         int err;
634
635         if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
636                 return -EOPNOTSUPP;
637
638         err = ionic_lif_rss_config(lif, lif->rss_types, key, indir);
639         if (err)
640                 return err;
641
642         return 0;
643 }
644
645 static int ionic_set_tunable(struct net_device *dev,
646                              const struct ethtool_tunable *tuna,
647                              const void *data)
648 {
649         struct ionic_lif *lif = netdev_priv(dev);
650
651         switch (tuna->id) {
652         case ETHTOOL_RX_COPYBREAK:
653                 lif->rx_copybreak = *(u32 *)data;
654                 break;
655         default:
656                 return -EOPNOTSUPP;
657         }
658
659         return 0;
660 }
661
662 static int ionic_get_tunable(struct net_device *netdev,
663                              const struct ethtool_tunable *tuna, void *data)
664 {
665         struct ionic_lif *lif = netdev_priv(netdev);
666
667         switch (tuna->id) {
668         case ETHTOOL_RX_COPYBREAK:
669                 *(u32 *)data = lif->rx_copybreak;
670                 break;
671         default:
672                 return -EOPNOTSUPP;
673         }
674
675         return 0;
676 }
677
678 static int ionic_get_module_info(struct net_device *netdev,
679                                  struct ethtool_modinfo *modinfo)
680
681 {
682         struct ionic_lif *lif = netdev_priv(netdev);
683         struct ionic_dev *idev = &lif->ionic->idev;
684         struct ionic_xcvr_status *xcvr;
685         struct sfp_eeprom_base *sfp;
686
687         xcvr = &idev->port_info->status.xcvr;
688         sfp = (struct sfp_eeprom_base *) xcvr->sprom;
689
690         /* report the module data type and length */
691         switch (sfp->phys_id) {
692         case SFF8024_ID_SFP:
693                 modinfo->type = ETH_MODULE_SFF_8079;
694                 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
695                 break;
696         case SFF8024_ID_QSFP_8436_8636:
697         case SFF8024_ID_QSFP28_8636:
698                 modinfo->type = ETH_MODULE_SFF_8436;
699                 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
700                 break;
701         default:
702                 netdev_info(netdev, "unknown xcvr type 0x%02x\n",
703                             xcvr->sprom[0]);
704                 modinfo->type = 0;
705                 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
706                 break;
707         }
708
709         return 0;
710 }
711
712 static int ionic_get_module_eeprom(struct net_device *netdev,
713                                    struct ethtool_eeprom *ee,
714                                    u8 *data)
715 {
716         struct ionic_lif *lif = netdev_priv(netdev);
717         struct ionic_dev *idev = &lif->ionic->idev;
718         struct ionic_xcvr_status *xcvr;
719         char tbuf[sizeof(xcvr->sprom)];
720         int count = 10;
721         u32 len;
722
723         /* The NIC keeps the module prom up-to-date in the DMA space
724          * so we can simply copy the module bytes into the data buffer.
725          */
726         xcvr = &idev->port_info->status.xcvr;
727         len = min_t(u32, sizeof(xcvr->sprom), ee->len);
728
729         do {
730                 memcpy(data, xcvr->sprom, len);
731                 memcpy(tbuf, xcvr->sprom, len);
732
733                 /* Let's make sure we got a consistent copy */
734                 if (!memcmp(data, tbuf, len))
735                         break;
736
737         } while (--count);
738
739         if (!count)
740                 return -ETIMEDOUT;
741
742         return 0;
743 }
744
745 static int ionic_nway_reset(struct net_device *netdev)
746 {
747         struct ionic_lif *lif = netdev_priv(netdev);
748         struct ionic *ionic = lif->ionic;
749         int err = 0;
750
751         /* flap the link to force auto-negotiation */
752
753         mutex_lock(&ionic->dev_cmd_lock);
754
755         ionic_dev_cmd_port_state(&ionic->idev, IONIC_PORT_ADMIN_STATE_DOWN);
756         err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
757
758         if (!err) {
759                 ionic_dev_cmd_port_state(&ionic->idev, IONIC_PORT_ADMIN_STATE_UP);
760                 err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
761         }
762
763         mutex_unlock(&ionic->dev_cmd_lock);
764
765         return err;
766 }
767
768 static const struct ethtool_ops ionic_ethtool_ops = {
769         .supported_coalesce_params = ETHTOOL_COALESCE_USECS,
770         .get_drvinfo            = ionic_get_drvinfo,
771         .get_regs_len           = ionic_get_regs_len,
772         .get_regs               = ionic_get_regs,
773         .get_link               = ethtool_op_get_link,
774         .get_link_ksettings     = ionic_get_link_ksettings,
775         .set_link_ksettings     = ionic_set_link_ksettings,
776         .get_coalesce           = ionic_get_coalesce,
777         .set_coalesce           = ionic_set_coalesce,
778         .get_ringparam          = ionic_get_ringparam,
779         .set_ringparam          = ionic_set_ringparam,
780         .get_channels           = ionic_get_channels,
781         .set_channels           = ionic_set_channels,
782         .get_strings            = ionic_get_strings,
783         .get_ethtool_stats      = ionic_get_stats,
784         .get_sset_count         = ionic_get_sset_count,
785         .get_priv_flags         = ionic_get_priv_flags,
786         .set_priv_flags         = ionic_set_priv_flags,
787         .get_rxnfc              = ionic_get_rxnfc,
788         .get_rxfh_indir_size    = ionic_get_rxfh_indir_size,
789         .get_rxfh_key_size      = ionic_get_rxfh_key_size,
790         .get_rxfh               = ionic_get_rxfh,
791         .set_rxfh               = ionic_set_rxfh,
792         .get_tunable            = ionic_get_tunable,
793         .set_tunable            = ionic_set_tunable,
794         .get_module_info        = ionic_get_module_info,
795         .get_module_eeprom      = ionic_get_module_eeprom,
796         .get_pauseparam         = ionic_get_pauseparam,
797         .set_pauseparam         = ionic_set_pauseparam,
798         .get_fecparam           = ionic_get_fecparam,
799         .set_fecparam           = ionic_set_fecparam,
800         .nway_reset             = ionic_nway_reset,
801 };
802
803 void ionic_ethtool_set_ops(struct net_device *netdev)
804 {
805         netdev->ethtool_ops = &ionic_ethtool_ops;
806 }